Fluent NHibernate
A while ago I mentioned that as it stood at the time, I didn't see any major benefits for the Fluent NHibernate and that you might as well use the XML directly.
What I wanted is NHibernate auto mapper. Couple of days ago I had the chance to take a look at Fluent NHibernate again. What I saw was almost exactly the dream that I had in mind. Here is my NHibernate mapping configuration for the whole application:
public virtual void ConfigureNHibernate(Configuration configuration) { var model = new AutoPersistenceModel { Conventions = { GetForeignKeyNameOfParent = (type => type.Name + "Id"), GetForeignKeyName = (info => info.Name + "Id"), GetTableName = (type => Inflector.Pluralize(type.Name)), GetManyToManyTableName = ((child, parent) => Inflector.Pluralize(child.Name) + "To" + Inflector.Pluralize(parent.Name)) } }; model .AddEntityAssembly(Assembly) .Where(entity => entity.Namespace.EndsWith("Model") && entity.GetProperty("Id") != null && entity.IsAbstract == false ) .Configure(configuration); }
After that, you are done. Just create an entity in the proper place, hit the /database/create and have a lot of fun.
I am loving it.
I still have some concerns about how I can surgically modify some configuration as needed, but I think that I can manage, overall.
Comments
Wow, I didn't know it can do that!
Overall, what I respect the people who work on it for, is that they do not try to create english out of the API. That's the common mistake I see in most fluent API (it has become another buzzword BTW), and they're really unusable, illogical and plain ugly.
+1 for sanity.
Is it eventually going to be integrated into actual NH?
Amazing this looks very very very very Cool, did I mention cool? I will give this a shot very soon.
Cheers
I finally got around to messing with Fluent NHibernate this week and am seriously loving it but I had no idea this behavior existed. Thanks a lot for the heads up dude.
Does this mean no xml mapping is required?
XML is created behind the scenes, for you you only write c#
@Tuna
Really?
That is... well surprising. Why create mapping serialize it to xml, deserialize it, analyze and load, instead of just create, analyze and load?
@Krzysztof Kozmic
Fluent Nhibernate is addon to normal nhibernate.
It works like (C# model descsription) => xml config => NHibernate because xml config => Nhibernate was checked and debugged a lot, so that people use a good foundation instead of writing their own.
Also, there is a chance that your PM will ask you to through out fluent nhibernate - and you will be able to use only nhibernate itself with generated xml's
fluent nhibernate is only a tool which knows how to generate XML mapping for nhibernate and nothing more. I would like to use it in my projects
Really interesting stuff, the conventions are the only bit of Fluent NHibernate that really interests me as well. Interested to see how you plug in more complex mappings, where impedance mismatch does lead to non-trivial differences between domain and database but I'm sure they'll have a way of handling it.
Also fabio had a related post recently ( fabiomaulo.blogspot.com/.../...-class-without.html) and the Oslo approach immediately sounded interesting ( http://fabiomaulo.blogspot.com/2008/12/light.html).
Is it possible to load some entities with Xml and some with the fluent interface?
I would be crazy to refactor my project (I think).
Also if the fluent interface is generating the xml files. Is it possible for it to generate 1 large Xml file? That would be a big performance in improvement for start up.
You would be only validating 1 xml file as opposed to many.
@Ayende I'm glad you're finding Fluent NHibernate more useful now, the automapping stuff is very nice. We're open to suggestions for improvements too, if there are any sticking points for you.
@Paul It is possible to use a mix of fluent mappings and xml. If you look at Ayende's example you can see that he's passing a Configuration (NHibernate config) instance into the Configure fluent method, you can treat that config instance just as you would any other NHibernate config and add regular xml mappings to it.
The idea of one big mapping is an interesting one, it isn't currently supported though.
Hi Guys,
Checkout this great post from Chris, www.chrisvandesteeg.nl/.../fluent-nhibernates-a...
Thanks James, something I might investigate. I work on a product and the number of entities is getting scary.
I am at 50 seconds for an application start which I have profiled. A lot of this time is down to the xsd validation
I'm looking to use it on next project instead of plain ActiveRecord, but have to check with your NH/ActiveRecord integration because I still need some of the ActiveRecord facilities/facilitators.
Sokun,
Yes, it means just that.
We have been using ActiveRecord at first but replaced it with Fluent NHibernate because ActiveRecord is putting to much logic in the entities. And the auto mapper is great, it has surprised us a few times about what it is able to do automatically. There are still some issues the main one for me is that it is not able to work with interfaces on top of the entities yet (where the relations are also interfaces). But sins we are talking about dumb entities that is something I feel is not so bad.
Really a great way to build you application TDD, basically we don't care about persistence while developing.
One question I have is (more NHibernate related) is what is a good strategy for session management when working with a windows application instead of a web based application. Not a lot of references about that.
-Mark
Is it posible to use that configuration with NHibernate Burrow ?
How?
Outstanding, this is exactly what i was waiting for!
@Paul Cowan: My ground-up fluent nhibernate rewrite branch supports having it all in one mapping file. Unfortunately it doesn't support a great deal ELSE at the moment. Hopefully you will hear some noise once this branch is close to matching the trunk in terms of functionality.
This is a part of my old mail in fluent-nh mailing list:
"what I have in my mind is ORuM: use NH as OODB.
Register your class and NH will auto-map it using ORM best-practices
(with the option to define what mean best-practices for you)."
ORuM is the anachronism of: Object Relational unMapping
Sub-Title: Mapping by difference.
What i find most annoying is to make the xml files by hand only to discover that a colleague has changed the model. We use NHibernate.Mapping.Attributes and that solves alot of problems as the person changing the model also changes the mapping. At startup NHibernate creates the xml files and Voila we are done.
However typo's always come up when you least expect them, so this might be a good option as well
Comment preview