Code Generation for CSLA and NHibernate

Code Generation for CSLA and NHibernate

Old forum URL: forums.lhotka.net/forums/t/615.aspx


Steven Hawkes posted on Thursday, July 13, 2006

Hi,

 

Just wondering if anyone has done any work on CLSO code generation using NHibernate for the persisting the business objects.   Just done some reading on both CSLA and NHibernate and it seems like a really good combination especially with the properties option in NHibernate.Contrib. 

DavidDilworth replied on Friday, July 14, 2006

Yes, CSLA and NHibernate is a combination we are using.  We are doing some code generation for our BO classes as well using our own in-house CodeSmith templates to fit in with our framework.

I know other people on this forum use different code generation tools as well.

If you've got any ASP.NET experience, then writing CodeSmith templates will seem quite straightforward.

Steven Hawkes replied on Wednesday, July 19, 2006

Hi

Thanks for the response.

I hope I dont upset the CLSA experts by continuing the theme of NHibernate on this forum but my justification is that I am trying to integrate NHibernate into CSLA and appreciate the help of people like yourself who have trodden the same path.   It it offends then I am most happy to continue off line. 

I have been some playing with NHibernate and CSLA.   I have my business object working but am now trying to add the NHibernate to the Data Portal code and have a few further questions which I was hoping you could answer.   I tried to use the Attributes Contrib in my business objects, but with not a lot of success.

The first problem is that the examples in the NHibernate doc dont really give a good example of how to decorate the class and associated properties.  Do you have an example Business Object class that you would be prepared to share with others on this forum.   It complains that my attribute properties are not correct. 

The second issue was that when I tried to run cfg.configure() it complains about a missing config file.  I guess that uisng the Attribute appriach and the use of reflection to stream the class info into the config I no longer need to make this call or is there another reason why this line was left in the example in the Contrib section of the user manuel.

TIA for any help

 

 

DavidDilworth replied on Friday, July 21, 2006

Steven,

Have a look at this code fragment on another thread.  That may help you identify the kind of code you should be aiming for inside your Data Portal code.

As for the NHibernate documentation, it is poor compared to the Hibernate documentation.  I actually prefer the Hibernate documentation and use that more often.

Use the NHibernate test code and examples, they do have some good stuff in there.

Here's an example of some fields marked up with NHMA attributes:

[Id(0, Name = "RoomId", Column = "room_id")]
[Generator(1, Class = "assigned")]
private int _roomId = 0;

[Property(Name = "RoomName", Column = "room_name")]
private string _roomName = String.Empty;

[Property(Name = "AreaId", Column = "area_id")]
private NullableInt32 _areaId = null;

[Property(Name = "RoomLength", Column = "room_length")]
private NullableDecimal _roomLength = null;

Notice that we're using NHibernate.Nullables here as well.

As for the missing config file, we've set our solution up to use a named configuration file, which can be different for different DBs.  So in our singleton NHibernate factory "manager" class we have the following:

Configuration cfg = new Configuration();
cfg.Configure(factoryCfg);
cfg.AddInputStream(stream); // Use the stream here
stream.Close();

where factoryCfg is the name of the physical configuration file (e.g. Nfs.hibernate.cfg.xml) that we want to use to configure this particular instance of a factory.  This allows us the flexibility of having different factory configurations via different NHibernate configuration files.

What I really want to do is convert the ProjectTracker example to use NHibernate and submit it to the new CodePlex site for everyone to share.  I think that's the best solution going forward.  I just need to find the time to do it.

I'm on holiday next week, so that will have to wait a little bit longer.  So I won't be posting for a while.

HTH

 

kvleeuw replied on Thursday, August 03, 2006

DavidDilworth:

What I really want to do is convert the ProjectTracker example to use NHibernate and submit it to the new CodePlex site for everyone to share.  I think that's the best solution going forward.  I just need to find the time to do it.

David,

We are currently evaluting the combination of CSLA and NHibernate and it would surely help to have the ProjectTracker app available in NHibernate format.

Regards,

Koen

bercht-a replied on Thursday, January 25, 2007

Hi

Is the ProjectTracker NHibernateVersion available?

Thanks

DavidDilworth replied on Thursday, January 25, 2007

The short answer is no.

For a fuller answer see my response on this thread.

bercht-a replied on Friday, January 26, 2007

Do you have some example code where i can see the mapping of a collection with nhibernate?

You wrote something about a second private member for the nhibernate list... do you have a specific example?

Thanks

DavidDilworth replied on Tuesday, February 06, 2007

As it happens, we've finally got round to doing an NHibernate version of Project Tracker.  We're working on it right now.

So I'll post again when the code is available for public viewing.

Fingers crossed, it should be available sometime in the next week or two.

ajj3085 replied on Tuesday, February 06, 2007

David,

I would like to investigate NHibernate more, but I don't like going through online FAQs and samples.  I prefer a book to walk though start to end (with online, I usually end up in the middle).

Do you know of any books that focus on NHibernate?  There's plenty on Hibernate, but 'NHibernate 1.0.1 is like version 2.1 of Hibernate, except missing some parts, and some parts of 3 have made it in' which isn't very comforting to me and seems more confusing that what NUnit has done... keep the Java and .Net projects and documentation seperate.

DavidDilworth replied on Tuesday, February 06, 2007

The short answer is "no".  All the books I've seen are focused on Hibernate, rather than NHibernate.  And to be honest, as I've mentioned before, the documentation for Hibernate is actually much better than the NHibernate version.

We've picked up lots of information from the code examples, some blogs and the forums, which are quite well supported.

So, if you're looking for a quick fix - there isn't one.

But....if you wait around just a little bit, then you might just see the ProjectTracker NHibernate example. Wink [;)]

ajj3085 replied on Tuesday, February 06, 2007

That's a shame.  I've noticed there really isn't any documentation on NHibernate, you just get refered to Hibernate.

And what would be a problem, except that the version of NHibernate doesn't exactly match any one version of Hiberate... and it would seem you need to remember that feature X of NHibernate is documented under 2.1 of Hibernate, but feature Y is documented under version 3...

Do you find you need to keep track of that, or is it actually easier than I'm thinking it is?

Yes, I do intend on looking at the PTracker NHibernate example.  Smile [:)]

DavidDilworth replied on Tuesday, February 06, 2007

We've used a version of NHibernate that hasn't changed (v1.0.2) so all the documentation is in the Hibernate 2.1 documentation.

It is easier than you think it is, once you get your head into it.

Fellmer Lloyd replied on Wednesday, February 07, 2007

DavidDilworth:

But....if you wait around just a little bit, then you might just see the ProjectTracker NHibernate example. Wink [;)]


Hi David

Such an example would be great!
Thanks for the work in advance.

Regards

DavidDilworth replied on Monday, March 12, 2007

Ok, for those of you that are interested in NHibernate I have some good news.

We have completed an NHhibernate version of the ProjectTracker example project.

I am currently corresponding with Rocky to see if I can get it released as part of the CSLAContrib project on CodePlex.

Watch this space!

RichardETVS replied on Monday, March 12, 2007

My question is not maybe related to this thread, but is there an automatic generation of tables from objects, with NHibernate? I mean, do we have to explicitly create the structure of tables? I saw that some DAL tools let the developer works with objects only, and they generate the database without an explicit mapping.

 

I do not know if I am clear ;) .

 

DavidDilworth replied on Monday, March 12, 2007

Yes, you can automatically generate a database schema from an NHibernate mapping file.  Can I suggest that you watch episode #51 from dnrTV.

http://www.dnrtv.com/default.aspx?showID=51

Oren Eini demonstates exactly how to do this.

RichardETVS replied on Monday, March 12, 2007

DavidDilworth:
Yes, you can automatically generate a database schema from an NHibernate mapping file. 

Thanks for the link. No easy for me, as English is not my first language, it is easier to read it than to understand it when someone spoke.

 

Well, I was not clear enough, in fact, it is the mapping I would like to generate automatically. I have classes, and he mapping and the database are generated. Is that possible with NHibernate?

 

Thank :)

 

 

 

DavidDilworth replied on Monday, March 12, 2007

Yes, I think you probably can.

We use the NHibernate.Mapping.Attributes.Dll, which allows you to markup your class with standard Attibutes that describe the mapping you want.

These attributes are reflected from the class code to build a mapping file.  This is done in memory as a one-off task.  Then as a debugging aid we write out the mapping file to a physical file as well.  This technique is demonstrated in our ProjectTracker.NHibernate example code.

If I understand correctly, the schema generation API works off a mapping file.  So I guess if you have a mapping file either in memory or on disk, then you might be able to use that to create the schema.

You'll need to go digging into the NHibernate API documentation for some help on that part though - not my area of expertise I'm afraid.

RichardETVS replied on Monday, March 12, 2007

Ok, thanks for the answer :) Time to stop to ask NHibernate questions on a CSLA forum :)

DavidDilworth replied on Thursday, May 03, 2007

I can (at last) announce that there is now a ProjectTracker.NHibernate example solution on the CSLAcontrib site on CodePlex.

Can I suggest that all future CSLA / NHibernate questions (especially those related to the ProjectTracker.NHibernate solution) are posted on the CslaContrib forum.

Thank you all for your patience and support.

burmajam replied on Monday, March 12, 2007

Hey, it's not good ... it's a great news! I'm so excited to see is.

Tnx David

Copyright (c) Marimer LLC