Hi,

I'd like to register on an EntitySet.CollectionChanged event I think I should register it like this :

   protected override void OnInitialize()
   {
        base.OnInitialize();
        Children.CollectionChanged += ChildrenOnCollectionChanged;
   }

But then, where should I unregister from CollectionChanged to avoid memory leaks ?

What do I want to do ? I want to update a Field of an entity when a collection in this entity has changed

class MyEntity
{
...
[Field]
public string AggregatedValues {get;set;}
[Field]
public EntitySet<MyValue> MyValues {get;set;}
}

=> i want AggregatedValues to be updated each time the MyValues collection is changed

Thanks for your help, Regards, Benoit

asked Feb 01 '18 at 05:18

Benoit%20Nesme's gravatar image

Benoit Nesme
43202024

edited Feb 01 '18 at 07:57


One Answer:

Hello Benoit Nesme,

You can actually leave it until session disposed because

  1. Garbage collector collects everything later after session disposing.
  2. Highly probable that queried from database entities, changed entities or new ones will be used until session is disposed so there is.

You can unsubscribe from EntitySet<t>.CollectionChanged event if an entity is removed because in most cases you can't even access its fields and removed entity won't be used once it was deleted.

answered Feb 06 '18 at 06:18

Alexey%20Kulakov's gravatar image

Alexey Kulakov
77225

Thanks, indeed, on session disposing, the entity will be removed from memory by the GC, including the event handler

(Feb 06 '18 at 07:28) Benoit Nesme Benoit%20Nesme's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA