R – Fluent NHibernate. Auto Mapping and Conventions

automappingconventionsfluent-nhibernate

Im new to NHibernate, the configuration aspect of it has always seemed overly onerous to me. Yesterday, I came across the Auto Mapping features of Fluent NHibernate and was suitably impressed.

To educate myself, I set myself the challenge of attempting the 'Getting Started First Project' (http://wiki.fluentnhibernate.org/show/GettingStartedFirstProject) using AutoMappings and Conventions. I have to admit to not having much luck.

To summise my steps:

I made some minor changes to the database schema in the example, mainly to remove the underscores from the foreign keys (i.e. Product_id to ProductId) as this would bring it more inline with our database naming standards.

Q1) I tried to write a IHasManyConvention implementation using the ForeignKeyConvention as an example but the WithKeyColumn method doesnt seem to exist (I presume this is because I downloaded yesterdays build so subsequently the design has changed since the article was written). How do I rectify this?

I also made some minor changes to the Entities layer in the example as I thought this would make it easier, they were to rename StoresStockedIn to Stores (Product.cs) and Staff to Employees (Store.cs).

My code looks like this:

var cfg = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005
    .ConnectionString(c => c
        .Is(connectionString)))
.Mappings(m => m.AutoMappings.Add(
                AutoPersistenceModel.MapEntitiesFromAssemblyOf<Store>()
                    .Where(t => t
                        .Namespace == "FluentNHibernateTesting.Entities")
                        .ConventionDiscovery.Setup(c => c.Add<ForeignKeyConvention>())
                ))
.BuildSessionFactory();

I then try and add the stores, products and employees to the database as per the getting started example. The exception I get happens on the Commit and is:

object references an unsaved transient instance – save the transient instance before flushing. Type: FluentNHibernateTesting.Entities.Employee, Entity: FluentNHibernateTesting.Entities.Employee

Q2) How do I resolve this problem? I understand (from my limited reading) that it has something to do with setting up the cascade elements of object relationships correctly but Im a bit stumped about how to go about this effectively using Auto Mapping.

Q3) Is there a target release date for the Fluent project? I (like I assume many others) feel this is an excellent initiative but am mindful of using it unless it is in a stable state.

Q4) Its mentioned in many of the blogs that they are working steadily towards parity of the features in Fluent with hbn.xml. Is there a list of these missing features somewhere in order to aid with the evaluation and usage of this tool?

Appreciate the help in advance.

Best Answer

I asked James Gregory the same questions in an email to him directly. He provided this excellent response.

'Hey Paul,

1) KeyColumnNames is what you're after.

2) You can do this one of 3 ways. Setup a IHasManyConvention to globally make all relationships cascade, use a ForTypesThatDeriveFrom call, or an automapping override.

3) We're in private testing of our 1.0 release now. Unsure whether we'll have a public beta or straight to release, but that should happen in the next few weeks.

4) It's very unlikely we'll ever be completely feature complete with NHibernate, we have no desire to be. Many of the NH features are extremely esoteric, and there is little benefit to us implementing them. As of 1.0 there will be very few features that we don't support that people use regularly. I can confidently say all common mapping scenarios are catered for, and if there's anything that isn't you can always drop back to hbm for that case.

To pre-empt any comments that our docs are out of date, documentation is going to be overhauled for 1.0 ;)

James'

Thanks James.