How to map the NHibernate Data Model to the Domain Model? - nhibernate

I started creating a domain model and now I asking myself, how can I map this domain model to a NHibernate Data Model ((using Fluent NHibernate)? Is there anywhere a good and simple example of how to do that?
With Data Model I didn't think about the physical/relational Database Model(!) What I meant was the Data Model in the Data Access Layer. So maybe I should change the term into Data Access Layer Data Model?! I hope some of you can follow me^^

I'd start reading the NHibernate manual, it's not very long and in chapter 5 covers many details about the different options that exist in the mappings. I find also important chapter 6 about collection mapping an 8 about inheritance.
Maybe you can start with the getting started page from fluent nhibernate, but to understand all the options you will have to lookup their meaning in the NH manual as Fluent NHibernate assumes that you are already familiar with the XML.
I don't have enough information about NHibernate in Action but the good old Hibernate in Action was also useful because the mapping examples for the Java version are in most cases valid on .NET and provides examples and detailed information on each parameter.

This is the easiest one for getting started in a step-by-step manner that I have come across
Your very first NHibernate application – Part 1
However, I would recommend that you simply download the latest binaries from the Fluent NHibernate download area rather than getting the source from Subversion, installing Ruby and building it locally on your machine but that's up to you.

Well, have you tried www.nhforge.org and http://fluentnhibernate.org/ ? Both have excellent introductory guides.

Take a look at the Getting started page on the Fluent NHibernate site. And definitely take a look at the Auto Mappings capabilities. I was up and running in a couple of hours. Good luck!

Related

PLINQO for NHibernate. Mappings and Entities.

I recently posted a question regarding NHibernate. This lead me to check Codesmith's framework PLINQO for NHibernate.
At the same time, i was reading a tutorial on nhibernate at their official site (as i was looking at the generated project by PLINQO).
I am not an expert on NHibernate but what i am looking at right now (PLINQO Generated NHibernate project) looks to me very sloppy, pretty much a recipe for disaster. It creates a set of "Entities" that i am guessing is the business object? A big mess to my eyes.
Does anyone has experience with Codesmith's NHibernate?
Can anyone point me to a mc daddy of NHibernate. I have found so many variants so far that i am already overwhelmed with all this.
Thanks you.
I work for CodeSmith Tools. How do you feel the generated project looks sloppy? How would you improve it? We try to separate out the entities (POCOs) / hbm's for consistency and easy of use (as Randy detailed above). The Entities are generated directly from what is defined in the hbm file, which can be customized without losing your changes.
The generated project follows the architecture of Linq-to-SQL and Entity Framework. We feel this allows developers on the Microsoft Stack to switch between backends quickly (NHibernate to EF or EF to NHibernatE) while minimizing the amount of code changes.
The generated datacontext manages the NHibernate sessions for you while giving you access to query extensions, caching and much more. Also, you don't lose any core NHibernate features as we just extend the core libraries. All of the core features are available on the DataContext under an advanced property.
P.S., The best place to post questions and get a timely response is via our forums.

Is this NHibernate sample still valid?

Or the concepts/best practices used there are now deprecated? I'm just starting to use NHibernate and I'm having a hard time finding good real life samples.
http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx
As you can see for the dates the author (Billy McCafferty) didn't update it in a long time. There is so much more on the NHibernate ground now but most of the concepts in that article still apply.
You may want to checkout additional sources like:
http://nhforge.org/
http://fabiomaulo.blogspot.com/
http://fluentnhibernate.org/
If you are using NHibernate in web applications, Billy McCafferty built an excelent framework on top of NHibernate and ASP.net MVC. http://www.sharparchitecture.net/
And this open source app is an awesome sample: http://whocanhelpme.codeplex.com/

learning nhibernate (with or without fluent)

I am going to try using NHibernate, the reason I stayed away from NHibernate so far was the xml-mapping part. Now that (I found out) there is fluent nhibernate, looks like we don't need to do xml mapping.
But I am wondering if starting with some xml mapping, would be more helpful in learning and getting comfortable with NHibernate, or should I start using it with fluent for nhibernate?
cheers,
hazim
I had similar concerns when I started with nHibernate, but having taken the road of learning the mappings and then learning Fluent, I have to say I wish I had just started with Fluent.
My reasoning is that there was enough to digest just learning nHibernate and Fluent helps make nHibernate more simple. Having said that I agree with David M, you'll need to understand the mappings at some point.
This series was really useful when I was learning nHibernate.
HTH
I would actually recommend attacking NHib from a totally Fluent perspective right now - FNH has matured just enough to not drive you nuts with inconsistencies, there are a lot of decent learning aids out there (try and look at anything posted since Sep 2009 on at first), as well as a decently active user group.
I found this amazingly good and very recent video tutorial Vacation of Fluent NHibernate, conceptually based on the Summer of NHib series everyone else has here. Unfortunately the author just got a new job and hasn't yet completed the series, but I will be shocked if you don't find it incredibly approachable and useful as a learning tool all the same.
You also can fairly easily learn to generate the HBM maps, and reading them is way more useful IMO, and certainly less painful, than writing them at first.
Lastly, give yourself a break and don't expect to master any of this by the end of the week! If you spend some time with FNH first, you will know which areas of NHib you want to dig into eventually and feel less overwhelmed by it when you do.
Cheers,
Berryl
Suggest you get started with a simple database schema using Fluent's auto mapping, so you can get used to using NHibernate Sessions and SessionFactories properly. Then you can branch out. But at some point you will need to fall back on XML mapping, so it's worth looking at eventually.
For understanding what is going I'd recommend starting with XML Mapping files. When learning NHibernate I found hand building configuration files to be useful. That said after I was comfortable using fluent and the occasional mapping file builder has been great.
For learning NHibernate though it's targeted at a previous version the Summer of NHibernate is incredibly useful.
Start with the XML mappings. Fluent is getting closer to being feature complete, but the terminology it uses still diverges from the terminology within the XML mappings (which is what is used by most NHibernate tutorials / documentation). Once you've done a mapping or two, it really isn't that hard especially with intellisense support from the schema files.
Fluent NHibernate is great, but for a beginner I think it might lead to some confusion as you cross reference your mappings with online research.

Tools for NHibernate

Can anyone throw some light on tools that can be used with NHibernate and which perform the below mentioned tasks:
Generates the Mapping File
Generates the Entity Class
Generates the Configuration file
And also provide information whether these tools are open source or licensed.
Thanks
One tool I've used that did a decent job was CodeSmith. It looked at the existing database and created the entities, mappings and configuration file. However, this is only to be used as a starting point as there are many tweaks you'll likely want to do to make it more usable and better performing. Things such as when to lazy load vs. eager load, creating subclasses or components. This tool really just mimics the database structure in your entities which is not always the best way to represent in code.
This is a licensed solution but it includes a fully functional demo you can use to gen your files initially. After that, you'll most likely just enhance just as you would your database.
Another is MyGeneration. Same thing, it'll get you 75% of the way there in most cases.
Just be cautious as to which version of NHibernate these tools are producing. They may be using an older version which may have some obsolete code.
I really like Visual NHibernate (http://www.slyce.com/VisualNHibernate/).
Not free, but has a nice set of features, including the definition of templates for the entities, and generating both HBM.XML as well as Fluent NHibernate mappings.
A similar question with some answers you might find useful is already on SO.
NHibernate Generators
As mentioned in that question's answers, fluent NHibernate is a good option. It just went RTM. You can find more info at http://fluentnhibernate.org/
Check out LLBLGen Pro. It's got a competing ORM but can also act as a code generator for nhibernate. Very good for DB first work.

Based on your experience, how many of you would recommend fluent NHibernate over Nhibernate way of doing things for my new project?

I just want to do a quick poll to see if Fluent Nhibernate is well received or if it is having lot of issues. I like Nhibernate but I definitely see the problem with xml to do mapping.
So, I am looking forward to community members for some insight and help me pick one over the other.
I am not considering either linq2sql or entity framework at this time.
I like Fluent NHibernate and I think it's mature enough if you are going to start a new project. Using it on a new project should allow the Fluent NHibernate project to continue to mature as yours progresses. There is a possibility for breaking changes (as happened recently with the convention mappings) but you should be able to deal with those. I've had a few issues with the mappings but the project is pretty responsive to bug reports and has mostly worked as expected.
The mapping options are:
Xml mappings - Standard of NHibernate. The maintenance headaches are well known but the advantage is that you have access to all of the configuration options provided by NHibernate. There are a few less-used configuration options are still being added to Fluent (at least last time I paid attention). So, if you are anticipating some crazy mappings, you may want to consider this option.
Standard Mapping - Provided by Fluent. You can create the mappings through code and is much better for refactoring and authoring. Not much to say about it, in my experience, other then that it works well and is a big improvement on the xml option.
Auto Mapping - Provided by Fluent. Allows you to map object properties by convention and it attempts to create the mappings automatically. It's a good idea but I think it still has some maturing to do. I'm currently using this mapping method and it works fine but I have ended up writing a large number of conventions and specifying the object relationships that it doesn't feel like it's saved much effort from the standard mappings.
Fluent NHibernate also provides nice test helpers for testing your mappings and some configuration APIs that can make it easier to configure NHibernate. Overall, it's a good project and it provides some nice additional functionality to NHibernate.
edit:
One additional thing to note: If you start off with Fluent NHibernate and decide it isn't going to work for your scenario, you can easily migrate back to the xml mappings. Fluent NHibernate allows you to export the mappings it creates and you can use those exports to not lose whatever mapping work you've already done.
One of the best advantages of using Fluent Nhibernate over vanilla NH is nice integration testing with PersistenceSpecification<T>:
[Test]
public void TestProductSave()
{
new PersistenceSpecification<Product>()
.CheckProperty(x => x.ProductName, "Wax")
.CheckProperty(x => x.Price, 20)
.VerifyTheMappings();
}
I've been using fluent on a new project of mine. The only minor bump I've hit so far is that it doesn't play so well with Castle Windsor out of the box, but it was quite easy to extend Windsor to do the job. Other than that I've been loving it. It's much more concise than the XML mappings.
The nice thing about fluent is that it isn't an all or nothing investment. You can write most of your mappings in fluent and if you find any issues you can map those classes in the standard XML mapping until the fluent issues are resolved.
Like any good answer, it depends.
I didn't get as much mileage out of the automapping features as I would have wanted to. I often have to work with preexisting databases.
I already had several projects using NHibernate under my belt, so using the hbm mapping wasn't difficult. After I figured out how to include in the hbm schema, it was much easier.
The one real advantage I gained from having fluent-nhibernate is being able to refactor my domain classes and have my mappings automatically change. I didn't really notice that much of a speed increase in writing the mappings.
Like anything, YMMV.
I am using FNH for a new project. What I like the best is the ability to generate and build the database directly from the entity classes.
I have had to write a few conventions for the properties but I'd rather that then maintain more than one list for each class.