Question

my controllers in my ASP.NET MVC application are dependent on the IDataContext which is a wrapper around the NHibnerate session so that i can easily substitute this later. I use the Microsoft Unity IOC container to inject my dependencies within the constructors of my classes.

I first tried created a FakeDataContext within my Test project and setup the correct dependencies as follows:

public class BaseControllerTest {
    [TestInitialize]
    public void Init() {
        // Create the ioc container
        var container = new UnityContainer();

        // Setup the common service locator (must come before any instance registered that use the common service locator such as membership provider)
        ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));

        // Configure the container
        container.RegisterType<IDataContext, FakeDataContext>();
    }
}

Now all i have to do is inherit from this class and everything works fine but i feel that the FakeDataContext is not the most accurate way to carry out my tests so i'm trying to create an in memory session using SQLite. I have modified the above to:

public class BaseControllerTest {
    private static Configuration _configuration;

    [TestInitialize]
    public void Init() {
        // Create the ioc container
        var container = new UnityContainer();

        // Configure the container
        container.RegisterType<ISessionFactory>(new ContainerControlledLifetimeManager(), new InjectionFactory(c => {
            return CreateSessionFactory();
        }));
        container.RegisterType<ISession>(new InjectionFactory(c => {
            var sessionFactory = container.Resolve<ISessionFactory>();
            var session = sessionFactory.OpenSession();
            BuildSchema(session);
            return session;
        }));
        container.RegisterType<IDataContext, NHibernateDataContext>();
    }

    private static ISessionFactory CreateSessionFactory() {
        return Fluently.Configure()
            .Database(SQLiteConfiguration.Standard.InMemory())
            .Mappings(m => m.FluentMappings
                .AddFromAssembly(Assembly.GetExecutingAssembly())
                .Conventions.AddFromAssemblyOf<EnumConvention>())
            .ExposeConfiguration(c => _configuration = c)
            .BuildSessionFactory();
    }

    private static void BuildSchema(ISession session) {
        var export = new SchemaExport(_configuration);
        export.Execute(true, true, false, session.Connection, null);
    }
}

However this throws the error "An invalid or incomplete configuration was used while creating a SessionFactory.". I thought this might be because the Entities the mappings are looking for are in a different project from the Test project so i tried saying Assembly.LoadFile("C:..\MyAssembly.dll") but this still did not work.

Please note that i used the following article http://www.mohundro.com/blog/CommentView,guid,fa72ff57-5c08-49fa-979e-c732df2bf5f8.aspx to help but it is not exactly what i'm looking for.

I'd appreciate it if someone could help. Thanks

Was it helpful?

Solution

Problem solved. I needed to add a reference to the SQLite dll. I also changed the session to use the ContainerControlledLifetimeManager aswell as the session factory. Please correct me if there's a more efficient way to do this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top