views:

126

answers:

2

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, using this to create my nHibernate session?

+3  A: 

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

snicker
nice one, thanks... is this suited to other apps other than asp.net?? - i plan to use my repository in a windows service also
alex
Yup. You should be fine. I use a similar repository pattern in all my apps.
snicker
thanks :-) In the example on that link though, there is getExistingOrNewSession - which checks the HttpContext - how will this work when using it in windows apps?
alex
@alex: I've edited my answer to explain session management by context a bit better.
snicker
+1  A: 

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.

Jamie Ide