tags:

views:

96

answers:

4

I was under the impression, when using nHibernate, after you create the mapping xml files etc., all the plumbing for basic CRUD operations is done for you, along with the class files i.e.

public class Employee
{
       public virtual string Name {get; set; }

}

But I must be wrong here because there doesn't seem to be anything generated, or is it generated internally when the session/config gets built on the first page load?

+1  A: 

We are creating the class files first, then the mapping and after that we generate the db.

Using the mapping attributes directly makes this style (IMO easier) The code looks pretty much like this:

public class Base {
    [Id(..., Column="{{Id.Column}}")]
    [AttributeIdentifier(Name="Id.Column", Value="ID")] // Default value
    public int Id { ... }
}
[AttributeIdentifier(Name="Id.Column", Value="SUB_ID")]
[Class] public class MappedSubClass : Base { }

I think the generated db model is a little bit different than it was if I would start with the db design - but I am happy with the result.

Just make sure that lazy loading is working if you are nesting a lot of classes into each other.

bernhardrusch
do you find the db generated is what you would have done if you did it by hand?
Blankman
I think it is a pretty good design - I think I would start a little bit different if I would start with the db design - but I really like it
bernhardrusch
I think this is the best thing using NHibernate -> you start thinking about your class model first and in a second step about the database (which has its own caveats but works pretty well for us)
bernhardrusch
we are even using the "Attribute style" (we are defining the NHibernate mapping directly in our domain models - I really like this style (and it keeps me XML free)
bernhardrusch
+1  A: 

I don't believe nHibernate will generate the classes for you, but you can use a tool like CodeSmith or the next version of LLBLGen to generate them for you.

Look under the "Helpful Tools" section of hibernates website for some tools which may be... helpful. Some of them will generate both mapping and class files. You can give one of them a try and only take the class files as a basis for your classes.

https://www.hibernate.org/365.html

Bob
https://www.hibernate.org/365.html is quite out of date and is not maintained anymore. For more actual reference and documentation, go to http://nhforge.org
Stefan Steinegger
Thanks for the updated link. The links on the old page still seem to be active, but probably just don't feature the latest tools.
Bob
+1  A: 

There is a tool to generate classes (only java I think), but I recommend to write the classes yourself. This are your domain classes and you should take special care of them. It's not like a typed recordset or something which is only used for db interactions.

On the other hand, the tool that creates the db schema is highly recommendable.

Stefan Steinegger
A: 

As far as I am aware, NHibernate doesn't create classes for you. Two very good tools I have came across on this regard are

  1. MyGeneration - Free

  2. CodeSmith - Licensed.

Sorry, couldn't add Hyperlinks due to some restriction in stackoverflow.

Sree