Initializing generated entity class [NHibernate]

Posts   
 
    
Robert.W
User
Posts: 79
Joined: 19-Jun-2007
# Posted on: 12-Nov-2010 11:11:37   

Hi,

I'm trying to find a way to initialize my entity's state (CreationDate, LastModificationDate etc). I can't use ctor in a generated class which would be my first choice and I definitely don't want to do that with properties every time I need a new instance. A compromise would be a partial Init method you would call from the generated ctor. Is this something you can include in the templates? Any reason that's a bad idea?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 15-Nov-2010 06:37:14   

The key question is: How would you do it if you don't have LLBLGen?

David Elizondo | LLBLGen Support Team
Robert.W
User
Posts: 79
Joined: 19-Jun-2007
# Posted on: 15-Nov-2010 07:13:42   

I would put all state initialization in the default constructor - that's what it's for. So it would be:

public MyEntity() { CreationDate = DateTime.UtcNow; LastModificationDate = CreationDate; }

but I can't do that since it's generated and will be overwritten.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 15-Nov-2010 08:34:53   

I guess David ment what would you do if you were generating NHibernate classes with another tool.

Calling an Init method in the CTor means the base class should have an Init method defined that you can overwrite in a partial class, is such method exists in NHibernate?

Robert.W
User
Posts: 79
Joined: 19-Jun-2007
# Posted on: 15-Nov-2010 08:51:42   

I see your point. How about partial methods?

Entity.cs //------------------------------------------------------------------------------ // <auto-generated>This code was generated by LLBLGen Pro v3.0.</auto-generated> //------------------------------------------------------------------------------ public partial class Entity { public Entity() : base() { // LLBLGen generated initialization Init(); // Suggested addition }

            partial void Init();

}

Entity.custom.cs

// My additions to Entity public partial class Entity { partial void Init() { Property = "initial value"; } }

Seems to work fine based on a quick test.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 15-Nov-2010 09:37:06   

Partial Init() is a good idea (or Created(), similar to the EF classes). We'll look into adding that.

In NH land it's also a good practice to use a factory instead of 'new', like in the form of structuremap. This can also help, but IMO leads to a lot of plumbing code without real value.

Frans Bouma | Lead developer LLBLGen Pro
Robert.W
User
Posts: 79
Joined: 19-Jun-2007
# Posted on: 15-Nov-2010 10:06:24   

Thanks for your answers. I will probably steer clear of factories to build entities unless I absolutely have to inject something into them simple_smile Never done this but a static Entity.New() looks tempting for where I have to manually instantiate an entity, at least for the time being, or I'll set up NH interceptors to put default values before saving. It seems however that having that partial init is best for the kind of simple scenarios I have.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 15-Nov-2010 16:57:28   

There's a tiny problem.... to support .NET 2.0 (which we do with NHibernate), we can't use partial methods, as they're added in .NET 3.5's compilers.

I think we can test for the target platform and if not .net 2.0, we're safe, and can emit the OnCreated() method (which is in line with what we have in linq to sql and EF).

There's also a bug on line 179, the include id is wrong, it should be SD_NHibernate_CustomEntityInclude. This fix will be released today.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 15-Nov-2010 17:34:01   

Added in next build (released within an hour) (OnCreated() partial method)

Frans Bouma | Lead developer LLBLGen Pro
Robert.W
User
Posts: 79
Joined: 19-Jun-2007
# Posted on: 15-Nov-2010 18:02:00   

Thanks, that was fast simple_smile

Robert.W
User
Posts: 79
Joined: 19-Jun-2007
# Posted on: 16-Nov-2010 15:25:14   

Hmm, it seems it's only done for entities, not value types. Could use that in both simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 16-Nov-2010 16:55:57   

Robert.W wrote:

Hmm, it seems it's only done for entities, not value types. Could use that in both simple_smile

And the other classes I presume? I already thought after adding the methods, if the other classes should get the addition too, but it was already running late, so I couldn't add it before the new build release. I'll add them for the next build. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Robert.W
User
Posts: 79
Joined: 19-Jun-2007
# Posted on: 16-Nov-2010 17:34:23   

I guess entities and value types are the most important for me - but obviously if there are other places you can put this tweak in I won't complain simple_smile Will be waiting for a tweet about the next build - thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 16-Nov-2010 17:39:31   

Robert.W wrote:

I guess entities and value types are the most important for me - but obviously if there are other places you can put this tweak in I won't complain simple_smile Will be waiting for a tweet about the next build - thanks.

We now have an RSS feed for new releases as well btw wink (it's in the customer area)

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Nov-2010 10:06:11   

Added to typedlist/view/valuetype classes. (next build)

Frans Bouma | Lead developer LLBLGen Pro