fluent-nhibernaterhinorhino-commons

Rhino.Commons + Fluent NHibernate


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

Many thanks

fromano


Solution

  • # # # # # # # # # #

    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){}
    }