c# - 使用带有 Fluent nHibernate 的 nHibernate 包装器

标签 c# nhibernate fluent-nhibernate repository-pattern

是否可以使用像这样的包装器和流畅的配置?

http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/

如果是这样,我应该在哪里添加流畅的配置?

另外,这是否适合在 ASP.NET 和 Windows 应用程序中使用? 我打算使用存储库模式,用它来创建我的 nHibernate session ?

最佳答案

SessionBuilder 中的 GetConfiguration 方法中,而不是

    public Configuration GetConfiguration()
    {
        var configuration = new Configuration();
        configuration.Configure();
        return configuration;
    }

如您链接的页面所示,只需执行以下操作:

    public Configuration GetConfiguration()
    {
        return Fluently.Configure()
            .Database(/* your database settings */)
            .Mappings(/* your mappings */)
            .ExposeConfiguration(/* alter Configuration */) // optional
            .BuildConfiguration();
    }

关于处理上下文的进一步查询,您将有两个继承ISessionBuilder的类,例如AspSessionBuilderWinAppSessionBuilder,并为当前项目注入(inject)适当的一个。您应该使用 Jamie Ide 概述的策略还有posted as an answer对于这个问题来处理上下文而不是使用HttpContext。您只需修改这一行:

.ExposeConfiguration(x => x.SetProperty("current_session_context_class", "web")

类似于“call”“thread_static”。请参阅 NHibernate Forge wiki 上的此页面,了解不同上下文 session 类型的详细说明:

Contextual Sessions @ NHibernate Forge

关于c# - 使用带有 Fluent nHibernate 的 nHibernate 包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2596376/

相关文章:

c# - ASP.NET Web API - 如何反序列化 JSON

c# - 事件记录/NHibernate : Update error on Query

c# - nHibernate 在多个线程上枚举相同的集合

c# - Fluent NHibernate - 从 SQL 转换为 C# 类型时如何进行附加处理?

c# - Windows Phone 数据绑定(bind)列表选择器到字符串列表

c# - 可以动态接受控制台命令的 Windows 服务应用程序

c# - 什么时候应该使用 using 语句?

fluent-nhibernate - 流畅的 NHibernate - HasMany().WithKeyColumnName

asp.net-mvc - 我需要一些非常好的博客来关注 ASP.NET MVC、Fluent NHibernate 和 Spark View Engine

nhibernate - 为什么是 Fluent NHibernate 与 hbm XML 文件?