views:

180

answers:

1

I've recently started to learn Fluent NH, and I'm having some trouble with this test method. It takes forever to run (it's been running for over ten minutes now, and no sign of progress...).

[TestMethod]
public void Entry_IsCorrectlyMapped()
{
    Action<PersistenceSpecification<Entry>> testAction = pspec => pspec
                                               .CheckProperty(e => e.Id, "1")
                                               .VerifyTheMappings();

    TestMapping<Entry>(testAction);
}

with this helper method (slightly simplified - i have a couple of try/catch blocks too, to provide nicer error messages):

public void TestMapping<T>(Action<PersistenceSpecification<T>> testAction) where T : IEntity
{
    using (var session = DependencyFactory.CreateSessionFactory(true).OpenSession())
    {
        testAction(new PersistenceSpecification<T>(session));
    }
}

The DependencyFactory.CreateSessionFactory() method looks like this:

public static ISessionFactory CreateSessionFactory(bool buildSchema)
{
    var cfg = Fluently.Configure()
        .Database(SQLiteConfiguration.Standard.InMemory())
        .Mappings(m => m.FluentMappings.AddFromAssembly(typeof(Entry).Assembly));

    if (buildSchema)
    {
        cfg = cfg.ExposeConfiguration(config => new SchemaExport(config).Create(false, true));
    }
    return cfg.BuildSessionFactory();
}

I've tried debugging, but I can't figure out where the bottleneck is. Why is this taking so long?

A: 

I would think it has to do with the way your trying to use the session together with the persistence spec. Make a base test class like the one below that provides you a session; if whole test takes longer than about 3 - 4 seconds max something is wrong.

Cheers,
Berryl

[TestFixture]
public class UserAutoMappingTests : InMemoryDbTestFixture
{
    private const string _nickName = "berryl";
    private readonly Name _name = new Name("Berryl", "Hesh");
    private const string _email = "[email protected]";

    protected override PersistenceModel _GetPersistenceModel() { return new UserDomainAutoMapModel().Generate(); }

    [Test]
    public void Persistence_CanSaveAndLoad_User()
    {
        new PersistenceSpecification<User>(_Session)
            .CheckProperty(x => x.NickName, _nickName)
            .CheckProperty(x => x.Email, _email)
            .CheckProperty(x => x.Name, _name)
            .VerifyTheMappings();
    }

}

public abstract class InMemoryDbTestFixture
{
    protected ISession _Session { get; set; }
    protected SessionSource _SessionSource { get; set; }
    protected Configuration _Cfg { get; set; }

    protected abstract PersistenceModel _GetPersistenceModel();
    protected PersistenceModel _persistenceModel;

    [TestFixtureSetUp]
    public void SetUpPersistenceModel()
    {
        _persistenceModel = _GetPersistenceModel();
    }

    [SetUp]
    public void SetUpSession()
    {
        NHibInMemoryDbSession.Init(_persistenceModel); // your own session factory
        _Session = NHibInMemoryDbSession.Session;
        _SessionSource = NHibInMemoryDbSession.SessionSource;
        _Cfg = NHibInMemoryDbSession.Cfg;
    }

    [TearDown]
    public void TearDownSession()
    {
        NHibInMemoryDbSession.TerminateInMemoryDbSession();
        _Session = null;
        _SessionSource = null;
        _Cfg = null;
    }
}

public static class NHibInMemoryDbSession
{
    public static ISession Session { get; private set; }
    public static Configuration Cfg { get; private set; }
    public static SessionSource SessionSource { get; set; }

    public static void Init(PersistenceModel persistenceModel)
    {
        Check.RequireNotNull<PersistenceModel>(persistenceModel);

        var SQLiteCfg = SQLiteConfiguration.Standard.InMemory().ShowSql();
        SQLiteCfg.ProxyFactoryFactory(typeof(ProxyFactoryFactory).AssemblyQualifiedName);

        var fluentCfg = Fluently.Configure().Database(SQLiteCfg).ExposeConfiguration(cfg => { Cfg = cfg; });
        SessionSource = new SessionSource(fluentCfg.BuildConfiguration().Properties, persistenceModel);
        Session = SessionSource.CreateSession();
        SessionSource.BuildSchema(Session, true);
    }

    public static void TerminateInMemoryDbSession()
    {
        Session.Close();
        Session.Dispose();
        Session = null;
        SessionSource = null;
        Cfg = null;
        Check.Ensure(Session == null);
        Check.Ensure(SessionSource == null);
        Check.Ensure(Cfg == null);
    }
}
Berryl
Thanks for the reply! I took the liberty of editing your response so the code looked nice all the way. I am unable to figure out where the class NHibernateInMemboryDbSession comes from. Is it part of some assembly, or do I have to define it myself?
Tomas Lycken
I had assumed you would create it, but I added it above to show you what it would look like. Cheers
Berryl
Ho Tomas. How did this work out for you?
Berryl
@Berryl: I ended up not using NHibernate for this project - too many other things didn't work either, and suddenly I found myself at the start of a new semester and thus at the end of spare time... - so I never actually got to try this out. Great thanks for the detailed reply, though! I know where to look whenever I pick this up again!
Tomas Lycken