Hybrid NHibernate DAL / BLL

I have an existing asp.net web forms project that uses Microsoft Enterprise DAAB for DAL, I need to implement some extensive functionality and I would like to use NHibernate to keep things simple.

Are there any design patterns / architectures that allow a hybrid DAAL / NHibernate DAL? is this a good idea?

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

Also, what is the best way to set NHibernate DAL / BLL for an asp.net web forms application? I have read the tutorial on NHibernate site and several others, there seems to be no consensus on starting / ending nhib session. I'm just looking for a best practice example.

thank

+1


source to share


2 answers


One approach I've heard about is using nHibernate when you interact with your domain model. Be aware that nHibernate can call stored procedures, so if you want to avoid generating SQL you can, but I would suggest.

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

As far as best practice goes, I found that I create a session when you need it and then store it in an httpContext, you will close the session on every reqyuest.



The only thing to do is specifically create and complete the transaction, which I would do in your service layer or application layer if your logic were to span multiple services (I am defining a service not as a web service, but as part of high-level code. which protects your application from your domain).

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

There is a bug / limitation in his code. Check my blog if you are using his method: http://jberke.blogspot.com/2008/10/nhibernate-transaction-session-mgmt.html

+2


source


You have two questions, you or the moderator should split this in 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 question which is to use the HttpModule in ASP.NET to set up the context for 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()
        {

        }
    }

      

+1


source







All Articles