32

I am coming from an Entity Framework and LLBL background for my ORM tools.

I have been asked to build a new system based on NHibernate. I have never done so, so I am coming at it with fresh eyes.

What is the difference between mapping with Fluent NHibernate and "Mapping By Code" in NHibernate? Is there a preference?

Randy Burden
  • 2,611
  • 1
  • 26
  • 34
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • 2
    This is not too constructive I believe. Why won't you learn NH and see for yourself what are the differences? **By the way, I'm learning java and have background in C#, what are the differences? ...** – gdoron Nov 20 '12 at 19:25
  • 3
    I believe he's referring to the mapping specifically. +1 to offset the downvote and edited the question to be more clear. – Origin Nov 20 '12 at 19:31
  • 4
    I can see how this would be confusing to a new-comer but like @Origin stated in his answer below and what dotjoe hinted at in his answer, the "Mapping by Code" feature is relatively new in comparison to the project's age. For a newbie, I would recommend just going with the new "Mapping by Code" built into NHibernate. I myself will continue to stick with Fluent NHibenate though because I've got a lot of code that currently uses it and and it's syntax makes more sense to me personally. – Randy Burden Nov 20 '12 at 19:58
  • I find this topic confusing and documentation very minimal. While I understand that the difference is that one uses XML for mapping; other, does not, that is very vague, there is more underneath. Glad you ask this question, I am in the same boat and I think this question is very useful. – pixel Aug 16 '19 at 14:41

3 Answers3

22

Fluent NH

Fluent NHibernate offers an alternative to NHibernate's standard XML mapping files. Rather than writing XML documents, you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code.

vs.

NH's new mapping by code

It is an XML-less mapping solution being an integral part of NHibernate since 3.2, based on ConfORM library. Its API tries to conform to XML naming and structure. There's a strong convention in how the mapping methods are built. Its names are almost always equal to XML elements names.

dotjoe
  • 26,242
  • 5
  • 63
  • 77
12

Some of it is preference, some of it is existing codebase. There was a time when NHibernate did not have any built in non-XML mapping options. There were a few solutions out there to fix this - e.g. confORM, Fluent NHibernate, and others I'm probably not aware of.

As of 3.2 I believe (perhaps 3.0) NHibernate now has "Mapping by Code". The advantage to this over Fluent NHibernate is that it doesn't require an additional library, and it is supported by the same team as NHibernate. On the other hand, Fluent NHibernate is a bit more mature (From what I've heard) and can support a broader set of mapping functions.

I also mentioned "existing codebase". Obviously, if you have a project that is already mapped with Fluent NHibernate, it would be best to continue on with it. If you're starting fresh, perhaps it is worth a try to use the built-in "Mapping By Code"

Origin
  • 1,943
  • 13
  • 33
3

I know this post is old but, for anyone else interested on this matter, I would strongly recommend you to read this blog. It made it pretty clear to me and it even also gives you comparisons between the two approaches.

http://notherdev.blogspot.de/2012/01/nhibernates-mapping-by-code-first.html

C3PO
  • 147
  • 4
  • 15