views:

1087

answers:

2

Hello,

Is there a way to use Rhino.Commons with Fluent Nhibernate, (in particular AutoMapping)?

Many thanks

fromano

+1  A: 

# # # # # # # # # #

UPDATE:
That answer was written in Feb of 2009. With the rate that the FluentNHibernate project was changing, it's very likely that the method has been renamed, or the mechanism for changing Conventions has been moved.

# # # # # # # # # #

I wanted to use DatabaseTextFixtureBase from Rhino.Commons.ForTesting with FluentNHibernate. The problem that I kept coming up against was the fact that FluentNHibernate adds it's magic to the NHibernate configuration in the PersistenceModel class, and getting your hands on the NHibernate configuration once you call DatabaseTestFixtureBase.InitializeNHibernateAndIoC() Rhino.Commons does a bunch of stuff privately and you no longer have access to the NHibernate config.

As it turns out, my last assumption was wrong. If you register an INHibernateInitializationAware service with the container, it will be picked up by the NHibernateUnitOfWorkTestContext when it calls CreatConfigs().

The INHibernateInitializationAware implementation that I am using looks like this:

public class FluentNHibernateInitializationAwareConfigurator : INHibernateInitializationAware
{
    public void BeforeInitialization(){}

    public void Configured(Configuration cfg)
    {
        var persistenceModel = new PersistenceModel
                                   {
                                       Conventions =
                                           {
                                               GetForeignKeyName = (prop => prop.Name + "Id"),
                                               GetForeignKeyNameOfParent = (prop => prop.Name + "Id")
                                           }
                                   };
        persistenceModel.addMappingsFromAssembly(typeof(OneOfMyMappingClasses).Assembly);
        persistenceModel.Configure(cfg);

    }

    public void Initialized(Configuration cfg, ISessionFactory sessionFactory){}
}

Of course the Conventions can be replaced by any that you happen to be using.

Enjoy!


If you are using the latest version of FluentNHibernate you may be using the Fluently class to configure your mappings. Here's another version of the FluentNHibernateInitializationAwareConfigurator:

public class FluentNHibernateInitializationAwareConfigurator : INHibernateInitializationAware  
{  
    public void BeforeInitialization(){}  

    public void Configured(Configuration cfg)  
    {
        Fluently.Configure().Mappings(m =>
        {
            m.FluentMappings.AddFromAssemblyOf<User>()
                .AlterConventions(convention =>
                    {
                        convention.GetForeignKeyName = (prop => prop.Name + "Id");
                        convention.GetForeignKeyNameOfParent = (prop => prop.Name + "Id");
                    }
                );
            m.Apply(cfg);
        });
    }  

    public void Initialized(Configuration cfg, ISessionFactory sessionFactory){}
}
RKitson
Thank you,I'll give it a try and let you know!
fromano
Hi there,I tried and it works great!Thanks
fromano
A: 

I am very sorry, but I can't find where I'm supposed to get 'AlterConventions' from? What assembly?

Am having the same problems really, but I guess I need more details. :)

Simply G.
That answer was written 17 months ago. With the rate that the FluentNHibernate project has changed, it's very likely that the method has been renamed, or the mechanism for changing Conventions has been moved.
RKitson