简体   繁体   English

将nHibernate包装器与流利的nHibernate结合使用

[英]Using a nHibernate wrapper with fluent nHibernate

Is it possible to use something like this wrapper with fluent configuration? 是否可以使用这种具有流畅配置的包装器?

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

If so, where would I add the fluent config? 如果是这样,我将在哪里添加流畅的配置?

Also, would this be suited to use in both asp.net and windows applications? 此外,这是否适合在asp.net和Windows应用程序中使用? I'm planning to use the repository pattern, using this to create my nHibernate session? 我打算使用存储库模式,以此来创建我的nHibernate会话?

In the GetConfiguration method in your SessionBuilder , instead of the SessionBuilderGetConfiguration方法中,而不是

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

shown in the page you linked, simply do something like this: 显示在您链接的页面中,只需执行以下操作:

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

Regarding the further inquiry about handling contexts, you'd have two classes inheriting ISessionBuilder , eg AspSessionBuilder and WinAppSessionBuilder , and inject the appropriate one for the current project. 关于对上下文的进一步查询,您将拥有两个继承ISessionBuilder类,例如AspSessionBuilderWinAppSessionBuilder ,并为当前项目注入适当的类。 You should use the strategy outlined by Jamie Ide also posted as an answer to this question to handle contexts instead of using HttpContext . 您应该使用Jamie Ide概述的策略(也作为对此问题的答案)发布来处理上下文,而不要使用HttpContext You just simply have to modify this line: 您只需要修改以下行:

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

to something like "call" or "thread_static" . 类似于"call""thread_static" See this page at the NHibernate Forge wiki for a good explanation of the different contextual session types: 有关不同上下文会话类型的详细说明,请参见NHibernate Forge Wiki上的此页面:

Contextual Sessions @ NHibernate Forge 上下文会话@ NHibernate Forge

Yes you can use it but it's better to use NHibernate's built-in contextual session management rather than handle it yourself. 是的,您可以使用它,但是最好使用NHibernate的内置上下文会话管理,而不是自己处理。 See my answer to this question . 请参阅我对这个问题的回答 In addition to less coding, it offers two other options in addition to HttpContext, Call and ThreadStatic. 除了减少编码外,它还提供了除HttpContext之外的两个其他选项:Call和ThreadStatic。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM