Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I listen for entities being added/deleted from an ObjectContext?
    primarykey
    data
    text
    <p>As far as I can tell, <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx" rel="nofollow noreferrer"><code>ObjectContext</code></a> doesn't expose any direct way of listening for items being added/removed. You <em>could</em> hook up to the <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.savingchanges.aspx" rel="nofollow noreferrer"><code>SavingChanges</code></a> event, and query the <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.objectstatemanager.aspx" rel="nofollow noreferrer"><code>ObjectStateManager</code></a> for which items have been added or removed. But this has several problems:</p> <ul> <li>The notification will occur before the items are actually added to the context.</li> <li>The notification will occur any time <em>any</em> changes are saved (even if no items are added/removed).</li> <li>It doesn't appear to be how <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.savingchanges.aspx" rel="nofollow noreferrer"><code>SavingChanges</code></a> was intended to be used--it is more for validating data than notifying of changes.</li> <li>This is subjective, but it just <em>feels</em> wrong to me.</li> </ul> <p>I've worked around this by implementing a limited version of the Repository Pattern--it handles creates, reads, and deletions, but updates are still performed by directly manipulating the properties of the entities themselves (and then saving changes from my repository).</p> <p>I'm slightly uneasy about this for a few reasons:</p> <ul> <li>I thought <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx" rel="nofollow noreferrer"><code>ObjectContext</code></a> was supposed to replace the repository from the repository pattern. It feels like I am duplicating work that the Entity Framework should be doing for me.</li> <li>Using an incomplete version of the Repository Pattern (the C, R, and D from CRUD) seems a little strange. Shouldn't a repository either be used for all 4 CRUD operations, or none?</li> <li>There must be a reason that the Entity Framework doesn't support add/delete notifications. By choosing to implement them anyway, am I creating a design headache that the Entity Framework developers chose to avoid?</li> </ul> <p>For reference, here is a rough outline of my repository:</p> <pre><code>public class EntityArgs&lt;T&gt; : EventArgs where T : EntityObject { .... public T Entity { get { return this.entity; } } } public delegate void EntityEventHandler&lt;T&gt;(object sender, EntityArgs&lt;T&gt; args) where T: EntityObject; public class EntityRepository { public event EntityEventHandler&lt;Foo&gt; FooAdded; public event EntityEventHandler&lt;Foo&gt; FooDeleted; ... public EntityRepository() { this.entities = new Entities(); } public IEnumerable&lt;Foo&gt; Foos { get { return this.entities.Foos; } } public void AddFoo(Foo foo) { this.entities.Foos.AddObject(foo); this.entities.SaveChanges(); this.OnFooAdded(foo); } public void DeleteFoo(Foo foo) { this.entities.Foos.DeleteObject(foo); this.entities.SaveChanges(); this.OnFooDeleted(foo); } public void SaveChanges() { this.entities.SaveChanges(); } ... } </code></pre> <p>I'm quite new to the Entity Framework, so let me know if my approach is totally wrong.</p> <p>EDIT: In response to Erix's suggestion to use <code>ObjectStateManager</code>, it <em>almost</em> addresses my issue, but not quite. The problem is that I want to be notified when <code>entities.Foos</code> will have the changes reflected in it. <code>ObjectStateManager.ObjectStateManagerChanged</code> occurs when <code>AddObject</code> is called, but before <code>SaveChanges</code> is called. So:</p> <pre><code>entities.ObjectStateManager.ObjectStateManagerChanged += (s, e) =&gt; Console.WriteLine("Action: {0}, Object: {1}, Count: {2}", e.Action, e.Element, entities.Foos.Count()); var foo = ... Console.WriteLine("Count: {0}", entities.Foos.Count()); entities.Foos.AddObject(foo); entities.Foos.SaveChanged(); Console.WriteLine("Count: {0}", entities.Foos.Count()); // Output: // Count: 0 // Action: Add, Object: ..., Count: 0 // Count: 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload