I checked out the latest version of Fluent NHibernate today and noticed the AutoPersistenceModel. I looked at the unit-tests to see what it’s supposed to do and I loved what I saw…
Let’s say you have a 2 simple classes like this:
Now, in the global.asax’s init (assuming a web application), we can configure NHibernate using Fluent NHibernate like this:
It uses convention over configuration in lots of places, eg the Id property is always used as identity-column if a property with that name exists.
But you could also easily add your own conventions. If you want to have the Id colums to be ArtistId and AlbumId (and any other className + Id), you’d do:
Want to restrict the namespace as well? No problem!
Fantastic! Now, to see what mapping file this code automagically creates for our Artist class, we can run the following code:
which returns an XmlDocument. In this case, the InnerXml of that document looks like this:
Again: fantastic! See how it even generates the one-to-many relation with the correct end-class, because we defined a property IList<Album>!
Running the same code for the Album class gives:
If you want to use Fluent NHibernate the ‘old’ way, you can still add ClassMap<Artist> to your assembly. Hence, you can even mix it with the AutoPersistenceModel! If you have a classmap file, it’ll use that for your class, if it isn’t there it’ll automatically create a mapping for you!
I’m sold! This makes using NHibernate a breeze. You can get up and running with your NHibernate project so much faster now.
[...] Fluent NHibernate’s AutoPersistenceModel: I Love it! – Chris van de Steeg talks about Fluent NHibernate’s AutoPersistenceModel which takes the pain out of object relational mapping using NHibernate by generating the mappings for you based on common conventions – looks really useful for quick projects. [...]
everybody knows that you loved what you saw by looking at the unit tests…
but what about telling US what you saw?
not everyone on a plannet is a guru in nHibernate topic, and not everyone heard about Fluent mapping. I’ve used nHibernate a bit (and dropped it because of added complexity not needed in a web applications) and I’ve just heard about Fluent NHibernate but I really have no idea what your article was about, sorry…
next time please try to compare 2 situations and clearly show what is the win of using the new feature for beginners, I I would be an expert in Fluent NHibernate I wouldn’t be in a need of reading your article in a first place…
thanks for it anyway, at least I understand NHibernate isn’t as complex as it was…somehow
hope my _criticism_ will move your explanation to NHibernate beginners like me a little closer, thanks
Neat. I knew that auto mapping existed but I wasn’t sure how to customize it.
By the way, you can use
.ConnectionString.FromConnectionStringWithyKey(key)
while initializing the configuration instead of loading it from configuration.
Could you please post your database schema you have your entities mapping to.
@ChrisMarisic: you can easily create your database schema like so:
new NHibernate.Tool.hbm2ddl.SchemaExport(config).Execute(true, true, false, true);
I meant just the table diagrams for your examples here.
You’ve been knacked. Keep up the good work.
I’ve just come across another very similar blog on the same subject. I added the same comment to that blog too:
It’s weird how this stuff is new. Back in 2002 I was using a tool that did this. In fact, not only does it automap + create the DB, you can also change your model and ask it to generate the script required to “evolve” the DB structure, which is useful at the point of release when you need a SQL script to upgrade a customer’s database structure as part of your server upgrade installer.
It handles class renames, member renames, all sorts. I’ve linked it in the URL (which isn’t mine).
I’ll take a look at NHibernate though, just out of curiousity.
Hi, I tried using your code in my web application but I’m getting error bebow. Do you know what could possibly be causing this? thanks.
If not, Can you post the code so I can compare it to my code and see what I’m doing wrong.
Exception Details: System.InvalidOperationException: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.
Source Error:
Line 55: var peristenceModel = AutoPersistenceModel.MapEntitiesFromAssemblyOf();
Line 56:
Line 57: peristenceModel.Configure(config);
Line 58:
Line 59: var document = peristenceModel
Stack Trace:
[InvalidOperationException: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.]
System.Reflection.RuntimeMethodInfo.ThrowNoInvokeException() +316
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +2753966
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +17
FluentNHibernate.InvocationHelper.InvokeGenericMethodWithDynamicTypeArguments(T target, Expression`1 expression, Object[] methodArguments, Type[] typeArguments) +283
FluentNHibernate.AutoMap.AutoPersistenceModel.FindMapping(Type type) +252
FluentNHibernate.AutoMap.AutoPersistenceModel.Configure(Configuration configuration) +147
Hello Chris, Would you happen to have Visual Studio 2005 solution build for this test, If so can you provide a zip file for it. I tried testing it, but keep getting null pointer excpetion.
Thanks
[...] » Fluent NHibernate’s AutoPersistenceModel: I Love it! (tags: tools development .net c# configuration nhibernate fluent orm mapping fluentnhibernate) [...]
I’ve dowloaded latest of Fluent and I see no AutoPersistenceModel.Map***() methods, neither Where(), neither WithCondition().
Do I miss something crucial?