Clear related entities

Posts   
 
    
Fabrice
User
Posts: 180
Joined: 25-May-2004
# Posted on: 08-Mar-2006 18:37:56   

Hi

I would like, for an entity, to clear all relation it could have with other entities. So I've make this code (this method is in the entity itself - adapter scenario) :


        /// <summary>
        /// Clear all relations with other entities
        /// </summary>
        public virtual void ClearLinksWithOtherEntities() 
        {
            EntityCollection coll = this.GetDependentRelatedEntities() as EntityCollection;
            if (coll != null)
                coll.Clear();

            coll = this.GetDependingRelatedEntities() as EntityCollection;
            if (coll != null)
                coll.Clear();

            foreach (IEntityCollection mycoll in this.GetMemberEntityCollections()) 
            {
                coll = mycoll as EntityCollection;
                if (coll != null)
                    coll.Clear();
            }
        }

I would like to know - If it will loose the foreign key field ? - If someone don't have a better way to do it ? I've modified the template to allow my entities to inherit from a custom base class, so I also can access protected members if it can be usefull.

Many thanks for any ideas!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 08-Mar-2006 19:14:22   

Fabrice wrote:

Hi

I would like, for an entity, to clear all relation it could have with other entities. So I've make this code (this method is in the entity itself - adapter scenario) :


        /// <summary>
        /// Clear all relations with other entities
        /// </summary>
        public virtual void ClearLinksWithOtherEntities() 
        {
            EntityCollection coll = this.GetDependentRelatedEntities() as EntityCollection;
            if (coll != null)
                coll.Clear();

            coll = this.GetDependingRelatedEntities() as EntityCollection;
            if (coll != null)
                coll.Clear();

            foreach (IEntityCollection mycoll in this.GetMemberEntityCollections()) 
            {
                coll = mycoll as EntityCollection;
                if (coll != null)
                    coll.Clear();
            }
        }

I would like to know - If it will loose the foreign key field ?

No simple_smile .

  • If someone don't have a better way to do it ? I've modified the template to allow my entities to inherit from a custom base class, so I also can access protected members if it can be usefull.

Many thanks for any ideas!

You should use an include template, this would solve the fact you had to alter an existing template.

Though I'd do this: - create a new entity instance - inject the Fields object into this new instance. - throw away the old entity instance.

Frans Bouma | Lead developer LLBLGen Pro
Fabrice
User
Posts: 180
Joined: 25-May-2004
# Posted on: 08-Mar-2006 23:04:49   

In fact I maybe should explain my problem instead, maybe you'll have a better idea than mine.

I've a process that can be run thousand of time (20 000 or more) with different input. This process usually take between 50 and 500 ms. When running, it fetch quite a lot of data, analyze, and save results.

To avoid the fetch of same entities several times I use an entity cache. This cache is not so big, maybe 80 or 100 entities.

Example :

MyEntity myEntity = new MyEntity(3);
myAdapter.FetchEntity(myEntity);
myEntity.OtherEntity = myCache.GetOtherEntity(entity.otherEntityId);

The problem is : The memory increase indefinitely. Ie after 4000 runs, I reach about 500Mb ram used by the thread. After 5000 run, it become instable an begin to take more time, sometime it take 45sec for 1 process frowning

What I think: OtherEntity have now a reference to myEntity. When the process will be finished (and it'll loop into another process), the memory took by myEntity will not be released because there is still a reference from the OtherEntity (still alive in the cache). Do you think it's true ?

So I'll try to have a small process that will run ie each 500 process and will parse the cache and break all links with other entities, so that these entities will be released.Your idea about injecting the Field info into a new object seem to be fine. But I don't think the cache is the only memory leak...

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 09-Mar-2006 09:14:05   

Instead of referencing an object from the cache, Clone it (copy or deep copy ()).

Fabrice
User
Posts: 180
Joined: 25-May-2004
# Posted on: 09-Mar-2006 13:26:08   

It could be a good idea yes. But I don't see any Clone method on entity ? I see it on EntityField, but not on entity (IEntity2 or event EntityBase2).

[edit] I've a question Entity A <--- Entity B (B have a reference to A) What can I do on Entity A to remove the reference that Entity B --> Entity A ?

Example: Employee e = new ... e.Contract = myContract

Now, myContract have a reference to the employee What can I do on the employee object to remove this reference ?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 09-Mar-2006 14:44:55   
CustomerEntity sourceCustomer = new CustomerEntity("CHOPS");
CustomerEntity cloneCustomer = new CustomerEntity();
cloneCustomer.Fields = ((EntityFields)sourceCustomer.Fields).Clone();

for(int i=0;i<cloneCustomer.Fields.Count;i++)
{
     cloneCustomer.Fields[i].IsChanged=true;
}

The above sample was copied from the follwoing thread: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=1368

Fabrice
User
Posts: 180
Joined: 25-May-2004
# Posted on: 09-Mar-2006 15:48:56   

Thank you, I've coded it It solve the memory problem, but the the process take 4 or 5x more time with the clone (~150 ms -> 750 ms) confused