Fluent NHibernate 映射测试需要很长时间

发布于 2024-08-18 00:11:00 字数 1361 浏览 1 评论 0原文

我最近开始学习Fluent NH,我在这个测试方法上遇到了一些麻烦。运行起来要花很长时间(现在已经运行了十多分钟了,没有任何进展的迹象......)。

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

    TestMapping<Entry>(testAction);
}

使用这个辅助方法(稍微简化 - 我也有几个 try/catch 块,以提供更好的错误消息):

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

DependencyFactory.CreateSessionFactory() 方法如下所示:

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给妤﹃绝世温柔 2024-08-25 00:11:00

我认为这与您尝试将会话与持久性规范一起使用的方式有关。创建一个如下所示的基本测试类,为您提供一个会话;如果整个测试花费的时间最多超过 3 - 4 秒,则说明有问题。

干杯,
贝里尔

[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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文