Fluent NHibernate: Configuring Your Application
There’s been a grey area of how to actually configure your application to use Fluent NHibernate, and also how to configure some more complicated situations (such as mixing fluent and non-fluent mappings). After some thought I’ve committed a change that should make things clearer. What follows is a few examples of how this new API can be used.
I’m going to assume that you’ve got an application already set up, or you know how to structure a standard NHibernate application. If you don’t, I suggest you read up on that first.
All the examples that follow are tailored to directly replace your
SessionFactory instantiation code.
Introducing the configuration API
You can now
Fluently.Configure your application. The API is broken down into five main methods, three of which are required.
1 2 3 4 5
You can combine these methods in various ways to setup your application.
Fluently.Configurestarts the configuration process
Databaseis where you specify your database configuration
Mappingsis where you supply which mappings you’re using
ExposeConfigurationis optional, but allows you to alter the raw Configuration object
BuildSessionFactoryis the final call, and it creates the NHibernate SessionFactory instance from your configuration.
If you’re in the situation where your application is exclusively using fluent mappings, then this is the configuration for you.
1 2 3 4 5 6
This setup uses the SQLite database configuration, but you can substitute that with your own; it then adds any fluent mappings from the assebly that contains
If you’re using only auto mappings, then this config is for you.
1 2 3 4 5 6 7 8
Replace the code inside
AutoMappings.Add with your auto mapping configuration. You can see more about auto mappings in my automapping tag.
Mixed fluent mappings and auto mappings
If you’re using a combination of standard fluent mappings and auto mappings, then this example should show you how to get started.
1 2 3 4 5 6 7 8 9 10 11 12 13
You can see that this is a combination of the two previous examples, the
Mappings method can accept multiple kinds of mappings.
You’ve not yet got around to using Fluent NHibernate fully, but you are configuring your database with it; this configuration will let you configure your database and add your traditional hbm mappings.
1 2 3 4 5 6
HbmMappings property allows you to add HBM XML mappings in a few different ways, this example adds everything from an assembly which defines
YourEntity; however, you can add from an assembly instance, or just add single types.
Mixed HBM and fluent mappings
You’re migrating your entities to Fluent NHibernate but haven’t quite got them all across yet - this is for you.
1 2 3 4 5 6 7 8 9 10 11
The whole shebang: fluent, auto, and hbm mappings
You’re a crazy fool and map a bit of everything, then this is how you’d configure it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Exporting hbm.xml mappings
Mappings call, you can do the following:
1 2 3 4 5 6 7 8 9 10
That will export all of your fluent and automapped mappings in hbm.xml format to whatever location you specify.
Altering non-automapped conventions
If you want to override conventions that are used by your non-automapped classes, then you can use the
AlterConventions method on
1 2 3 4 5 6 7 8
If you forget to setup your database, or don’t add any mappings, instead of pulling out your hair over obscure NHibernate exceptions, the
BuildSessionFactory method will throw a more helpful exception to try to point you in the right direction. It’ll tell you whether you’ve forgot to add any entities, or not setup your database.
That’s it for now, I hope this helps to make configuring your application a little clearer.