InMemoryDatabaseTest using FluentNHibernate

Ayende posted this wonderful idea before, Billy McCafferty also mentioned this to me during our NH training.

I created the FN syntax InMemoryDatabaseTestFixture based on Ayende’s code.

public class InMemoryDatabaseTestFixture : IDisposable
    {
        private static Configuration Configuration;
        private static ISessionFactory SessionFactory;
        protected ISession session;

        public InMemoryDatabaseTestFixture(Assembly assemblyContainingMapping)
        {
            if (Configuration == null)
            {

                FluentConfiguration config = Fluently.Configure()
                    .Database(FluentNHibernate.Cfg.Db.SQLiteConfiguration.Standard.InMemory()
                    )
                    .Mappings(m =>
                                  {
                                      m.FluentMappings.AddFromAssembly(assemblyContainingMapping);
                                      m.AutoMappings.Add(new AutoPersistenceModelGenerator(assemblyContainingMapping).Generate());
                                  }

                    );

                Configuration = config.BuildConfiguration();
                SessionFactory = config.BuildSessionFactory();
            }

            session = SessionFactory.OpenSession();

            new SchemaExport(Configuration).Execute(true, true, false, session.Connection, Console.Out);
        }

        public void Dispose()
        {
            session.Dispose();
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s