views:

789

answers:

2

Hi,

I have an existing asp.net webforms project that uses Microsoft's Enterprise DAAB for the DAL, I need to implement some extensive features, and I would like to use NHibernate to make things easier.

Is there any design patterns/architectures out there that allow a hybrid DAAB/NHibernate DAL ? is it a good idea ?

My thinking is: if I had a hybrid DAL I could still pass high traffic/un-dynamic queries through the DAAB side and save the overhead of the dynamic sql genereation. But still have nhibernate for more complex qureies.

Additionally what is the best way to setup NHibernate DAL/BLL for an asp.net webforms application ? I have read the tutorial on the NHibernate site and several others there dosen't seem to be a consensus on starting/ending the nhib session. I'm just looking for best practice example.

Thanks

+2  A: 

One approach I've heard about is use nHibernate for when your interact with your domain model. Keep in mind nHibernate can call stored procedures so if you want to avoid the SQL Generation you can but I'd advise against.

If you have queries for reporting or displaying data that you don't want to create custom entities from then you could use DAAB, althought I would not recommend doign this if your going to create your entities.

As for best practice, I've found that creating the Session when you need it, and then store it in httpContext, you will close the session on every reqyuest.

The only other thing then is to specificly create and end a transaction which I would do in your service layer or application tier if your logic would span multiple services (I define a service not as a webservice but as a high level piece of code which shields your application from your domain layer).

A good starting point is from Billy McCafferty's article here: http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx

There is a bug / limitation within his code check out my blog post if you use his method: http://jberke.blogspot.com/2008/10/nhibernate-transaction-session-mgmt.html

JoshBerke
+1  A: 

You have two questions - you or a moderator should split this into two: 1) What's the best way to use DAAB and NHibernate together?

2) What's the best way to set up NHibernate in a Web Forms application?

I can answer the second one, which is to use an HttpModule in ASP.NET to configure the context on each request. An example module can be found here: http://tinyurl.com/b5am9b

public class NHibernateSessionModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.EndRequest += new System.EventHandler(context_EndRequest);
        }

        void context_EndRequest(object sender, System.EventArgs e)
        {
            HybridSessionBuilder builder = new HybridSessionBuilder();
            ISession session = builder.GetExistingWebSession();
            if (session != null)
            {
                                Log.Debug(this, "Disposing of ISession " + session.GetHashCode());
                session.Dispose();
            }
        }

        public void Dispose()
        {

        }
    }
ssmith