Reuse Entity Objects vs Instantiating for Each Use

Posts   
 
    
Emmanuel
User
Posts: 167
Joined: 13-Jan-2006
# Posted on: 29-Mar-2006 17:06:52   

Wondering what the best practice is out there for reusing Entity objects (if at all).

I have some routines where it would seem to make sense to reuse an Entity object instead of instantiating a brand new one. Is there a method for 'clearing' out the old Entity so that I can reuse it? What properties should I also reset (e.g. IsNew, IsDirty, Is???)?

Or is this just asking for trouble and I should just use an object once, new() another one and let the CLR's garbage collection take care of the old one?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 29-Mar-2006 17:27:40   

The following is a thread on the benefits of reusing Entity Objects, specially in large data handlind scenarioes, and also on how to reuse them: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=5751

Emmanuel
User
Posts: 167
Joined: 13-Jan-2006
# Posted on: 29-Mar-2006 18:57:15   

From that thread I see that the poster suggested doing the following to 'reset' an existing, used Entity object so that it could be used again:

  1. entity1.Isnew = True
  2. set all fields of entity1
  3. mark all entity1' fields as Changed

I understand step 1 and step 2. And I think I understand step 3 although I didn't know that one had to do this to ensure that fields can persisted. Does one?

Also, what about related entities held by an Entity object. I gather I would need to clear out those collections. Is that as simple as just doing Entity.RelatedCollection.Clear() or do I need to also set some other properties too?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-Mar-2006 07:33:08   

what about related entities held by an Entity object. I gather I would need to clear out those collections. Is that as simple as just doing Entity.RelatedCollection.Clear() or do I need to also set some other properties too?

You'll just need to clear the related collection(s).

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 30-Mar-2006 15:17:19   

Emmanuel wrote:

From that thread I see that the poster suggested doing the following to 'reset' an existing, used Entity object so that it could be used again:

  1. entity1.Isnew = True
  2. set all fields of entity1
  3. mark all entity1' fields as Changed

I understand step 1 and step 2. And I think I understand step 3 although I didn't know that one had to do this to ensure that fields can persisted. Does one?

Also, what about related entities held by an Entity object. I gather I would need to clear out those collections. Is that as simple as just doing Entity.RelatedCollection.Clear() or do I need to also set some other properties too?

If you save your entity without refecht, then in step 1 you also have to set entity.Fields.State = New to avoid the OutSyncException.

Really step 3 should be replace as follow: Before set all entity's field to the new value, all the entity's field = defaultValue and set field as not changed. I would like the framework to have a routine to do all this.

I did step 3 because as I was migrating data from one database to other, if the value that you assign to a field is equal the last assigned value, then the field is not flaged as changed.

About the relatedCollection, I was getting a hard time trying to save an entity and its related entities automatically; but I did not get success. The first time the related entity get its fk assigned automatically from the parent entity pk (identity field); but after that the related entity did not get the fk assigned, I assigned 0 the the related entity's field that related it with the parent entity with the idea to break the link between entities; but that did not help. May be I was doing something wrong, may be I did not set the right property. At the end I saved the parent entity with refetch, then I assigned the parent primary key (identity field) to the related entitty and saved it.

May be Frans can help with a routine of the steps necesary to put an entity as new without creating it.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 30-Mar-2006 15:33:57   

Rogelio wrote:

May be Frans can help with a routine of the steps necesary to put an entity as new without creating it.

You mean, keep the same Entity object, but it's all clean and without any data?

Frans Bouma | Lead developer LLBLGen Pro
Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 30-Mar-2006 16:17:47   

Otis wrote:

Rogelio wrote:

May be Frans can help with a routine of the steps necesary to put an entity as new without creating it.

You mean, keep the same Entity object, but it's all clean and without any data?

Yes, like when the entity is created with New.

Emmanuel
User
Posts: 167
Joined: 13-Jan-2006
# Posted on: 30-Mar-2006 18:15:07   

Yes, Otis, I want to 'reset' an Entity to the state that it would be in when first constructed.

For routines where I am iterating quickly and instantiating several Entities per iteration, it would make sense to not burden the GC and reallocate Entity objects off the heap all the time.

I realize that it would probably involve following all the entity relationships and cleaning up the referred collections, etc. What would really be nice (in a future version) would be a Reset() method that would do this for us. I've got to believe it would make apps run faster. This would be good for llblgen since I suspect that one argument against using O/R mappers is that it adds overhead to apps.

What do people think?

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 30-Mar-2006 18:29:51   

Emmanuel wrote:

Yes, Otis, I want to 'reset' an Entity to the state that it would be in when first constructed.

For routines where I am iterating quickly and instantiating several Entities per iteration, it would make sense to not burden the GC and reallocate Entity objects off the heap all the time.

I realize that it would probably involve following all the entity relationships and cleaning up the referred collections, etc. What would really be nice (in a future version) would be a Reset() method that would do this for us. I've got to believe it would make apps run faster. This would be good for llblgen since I suspect that one argument against using O/R mappers is that it adds overhead to apps.

What do people think?

I agree with you Emmanuel.

loren
User
Posts: 14
Joined: 29-Jul-2006
# Posted on: 30-Jul-2006 01:11:53   

I just ran across this thread... This is what I'm looking for right now too. Here's my vote for adding this feature.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 30-Jul-2006 10:53:50   

loren wrote:

I just ran across this thread... This is what I'm looking for right now too. Here's my vote for adding this feature.

Noted simple_smile You can add it yourself in v2 though: Generate into an entity a routine Reset:

public void Reset()
{
    this.Fields = CreateEntityFactory().CreateFields();
}

you then of course have to generate code as well for the member variables referencing other entities, if you want that.

Frans Bouma | Lead developer LLBLGen Pro