开发者

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/

If so, where would I add the fluent config?

Also, would this be suited to use in both asp.net and windows applications? I'm planning to use the repository pattern, 开发者_JAVA技巧using this to create my nHibernate session?


In the GetConfiguration method in your SessionBuilder, instead of the

    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, e.g. AspSessionBuilder and WinAppSessionBuilder, and inject the appropriate one for the current project. You should use the strategy outlined by Jamie Ide also posted as an answer to this question to handle contexts instead of using 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". See this page at the NHibernate Forge wiki for a good explanation of the different contextual session types:

Contextual Sessions @ 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. See my answer to this question. In addition to less coding, it offers two other options in addition to HttpContext, Call and ThreadStatic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜