Fluent NHibernate or NHibernate for complicated database with complicated relations - c#

I have a databse with complicated relations. I need to know if is better for my project use Fluent NHIbernet or only NHibernate. I know than fluent is better for auto mapping etc. . But will i have some problems in future when I have complicated databes with realy complicated relaations. Have Fluent NHibernate some limitations. Have Fluent NHibernate some problems during configuration? Will be Fluent NHibernate slower as NHibernate?

someone correct me if im wrong, but I think under the hood fluent nhibernate creates the xml mapping files that get loaded into nhibernate.
You can configure fluent-nhibernate to handle both the ClassMap<> and the xml based .hbm.xml mapping files. If there are situations you run into that fluent-nhibernate cannot handle then you can drop back to the xml mapping files if needed. The advantage of the fluent-nhibernate mapping is that it is easier to refactor your POCO classes as you have compile time checking, where as the xml mappings you will only know if there is a problem at runtime.

The question is not constructive. The only difference between NH and FluentNH is XML binding definitions vs. in-code binding definitions. FluentNH only defines the schema, it does nothing to the ORM logic itself - it's still NHibernate.

Related

Nhibernate mapping generator that supports mapping by code from 3.2

I am looking for an NHiberate mapping generator that can generate mapping by code rather than .xml or Fluent NHibernate.
I tried NHibernate Mapping Generator, but it has no validation. Thus, the existing mapping files might not 100% correct.
Devart Entity developer doesn't support this currently.
Any recommendations?
You could try nHibernate Designer from MindScape.
Well since you are asking for a suggestion, I can say by experience don't try to use a code generator. Such kind of approach fails when the project grow, you suddenly need to add some modifications and re-generating the entities would be a pain and so on. Try to read about ConventionalMapping. It is basically a strategy in building the mapping on the fly based on some conventions generally exists in a database table vs classes naming. I did this in past and with a little effort you will be able to concentrate just on the classes and completely forgot the mapping.

Fastest and simplest way to set up NHibernate/LINQ without HBM files

I would like to set up a very simple demo application that includes LINQ over NHibernate. While I wait for an answer, I'm doing other tasks :)
I don't like HBM files at all
I need to set up a very simple database schema (3 tables)
I wouldn't exactly like to start from the DB. Maybe class model could be a better beginning
I haven't chosen target DB: probably MySQL
I know how to create a data model from HBM files. I know how to extract the data model from DB to .NET's Entity Framework but I believe my demo could be better done with NHibernate.
Can you suggest me a quick-start tutorial? I knew there was an old NH plugin based on annotations: that could be a suitable starting point...
Like Adam suggested, one option for you is to use FluentNHibernate and it's Auto mapping. Although, since your setup is really small, you could use the Fluent mapping as well.
Apart from the FluentNHibernate wiki, here are some resources:
Your very first NHibernate application by Gabriel Scheker
Getting Started With Fluent NHibernate and Auto Mapping in C# by Jason Mitchell
Another alternative with more recent version of NHibernate is mapping by code. This SO question has more than enough links, so I won't repeat them here.
As for attributes mapping, I suggest that you avoid it. They aren't used that much and you kind of poison your domain / POCO classes with data access concerns.

Can you compare Fluent NHibernate with xml configuration mapping files?

What should I take into consideration if I need to choose between Fluent NHibernate and standard xml mapping files of NHibernate?
Can you compare the prominent differences that I should be aware of?
Several things and as mentioned this is possibly a duplicate question the main one I can think of is that you can change xml files without a code recompile but you cannot change mappings in fluent without recompiling something because they are by their nature compiled.
That said you can have a mappings assembly and then just patch that. Obviously whatever changes you make then need to be run through integration tests.
Aside from that Fluent Nhibernate can be confusing to NHibernate users because they mix some terms with their own so I always say you should start with XML file mappings until you totally understand whats going on then migrate to Fluent if you don't need to change the mappings without recompilation as it just reads better.
However don't forget you are then adding a layer of abstraction over the top with its own quirks which can make for interesting debugging.
Note: This is more of an opinion answer.
There's actually a 3rd option which you missed. NHibernate 3.2 shipped with it's own code mapping syntax.
http://nhforge.org/blogs/nhibernate/archive/2011/09/05/using-nh3-2-mapping-by-code-for-automatic-mapping.aspx
Personally I don't like the syntax, I'm a Fluent NHibernate fan and it works for me.
I think the only real thing to consider is what krystan already mentioned, which is the naming differences.
I personally threw in the towel with the XML mappings, and didn't pick NHibernate up again until Fluent NHibernate came out.
I don't believe you need to know anything about XML mappings, if you want to learn the XML mappings you can export Fluent mappings and look at them. But really the Fluent Mappings are dead simple.
Most problems from Fluent NHibernate steam from attempting to use Auto Mapping which maps a lot of assumptions, it's fine if you're planning on generating the DB schema, but if you've got an existing schema it's best to explicitly map them. I always explicitly write my maps tho.

How to convert Castle Activerecord to pure NHibernate or Fluent NHibernate?

I'm going to refactor a growing project from using Castle Activerecord to pure NHibernate or Fluent NHibernate with Service/Repository pattern and POCO.
What would be an easiest way to obtain hbm xml from existing Castle Activerecord model?
Another question, is it possible to convert hbm to Fluent NH and vice versa?
Remi already pointed out how to generate hbm from ActiveRecord. It's also possible to generate hbm mappings from Fluent NHibernate by using ExportTo(). It's not possible to convert hbm to FNH (that is, C#) code, but you can load them from FNH.
Using Castle ActiveRecord does not imply that you can't use repositories. In fact, Castle ActiveRecord does not force you to use the ActiveRecord pattern, and it doesn't even require you do inherit ActiveRecordBase in your persistent classes. You can use ActiveRecordMediator as a repository, or wrap it in a repository interface/implementation as Rhino.Commons does.
http://stw.castleproject.org/Active%20Record.Configuration%20Reference.ashx
You can also output nhibernate mapping
files to the AppDomain.BaseDirectory.
<activerecord isDebug="true"><config></config></activerecord>
I think that should give you the mapping file in your application directory
There is at least one open source project to convert HMB to FNH:
http://code.google.com/p/nhibernate-hbm-to-fluent-converter/
As others have already said, you can easily convert ActiveRecord to HBM and FluentNHibernate to HBM.

Generate C# entities from existing DB and Fluent NHibernate auto mapping

I'm working with an existing database that uses some really ugly conventions. I'd like to use NHibernate, and I think I can fix all these ugly DB conventions using Fluent NHibernate's auto mapping conventions. I'd like to avoid writing all the entity classes by hand. (This is where LINQ to SQL and SubSonic are appealing)
Is it possible to generate my C# entities based on my database schema and mappings specified in Fluent NHibernate?
Aside: Do you know of a designer for NHibernate?
I think, this is what you want: NHibernate Mapping Generator.
http://nmg.codeplex.com/
NHibernate Mapping Generator supports Fluent NHibernate and a variety of databases.
There are also plans to support the new NH 3.2 style fluent mappings.
Have a look.
LLBLGenPro V3 will work as a generic designer for a number of ORM frameworks, including LLBLGen's own framework, NHibernate,Linq2SQL and the Entity Framework.
It also includes template for generating code and mappings for all of the above frameworks.
Fluent NHibernate is under consideration at the moment :-)
I have not found anything that will generate Fluent NHibernate code based on an existing database schema.
Here's a similar question:
Using MyGeneration with Fluent NHibernate
As far as designer for NHibernate, there is ActiveWriter, but that is for Castle ActiveRecord, not for Fluent NHibernate. I remember seeing a reference to at least one other tool that could act as an NHibernate designer, but I can't find it at the moment.
Code warrior provides a bunch of templates for creating entities from database tables. I used it to generate my entities & services & unit tests, then used fluentNH automapping. This got my project up and running very quickly. the project needed a bit re-factoring but what code base doesn't.

Categories