-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Session-per-request implementation in global.asax
PostPosted: Sat Dec 13, 2008 10:49 am 
Newbie

Joined: Sat Nov 29, 2008 6:59 pm
Posts: 19
Location: Burlington, VT
Is the code snippet below a good implementation of the session-per-request pattern in an ASP.NET application? If so, why do people bother creating a custom IHttpModule for this task?

The SessionManager is a custom helper class that stores session factories in a thread-safe singleton.

Code:
    public class Global : System.Web.HttpApplication
    {

        public static ISession CurrentSession { get; private set; }

        protected void Application_Start(object sender, EventArgs e)
        {
            // Initialize default session factory
            SessionManager.ConfigureFactory("ConnectStringKey", "AssemblyName");
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            CurrentSession = SessionManager.OpenSession();
        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {
            if (CurrentSession != null && CurrentSession.IsOpen)
            {
                CurrentSession.Flush();
                CurrentSession.Close();
            }
            CurrentSession = null;
        }
etc.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2008 5:36 pm 
Newbie

Joined: Sat Nov 29, 2008 6:59 pm
Posts: 19
Location: Burlington, VT
This has gone a week without a reply, so I'm going to assume that it's a valid session-per-request implementation. It's working fine in the application I'm currently building.

But I am still interested to understand why most of the examples I've seen use a custom HttpModule instead of this simple approach. Any opinions?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 7:07 am 
Newbie

Joined: Wed Oct 01, 2008 10:59 am
Posts: 6
Hi,

Have you considered using Spring.NET? It has excellent NHibernate support built in and can manage session as well as transaction scope.

Best,

Dirk Louwers


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 1:26 pm 
Newbie

Joined: Sat Nov 29, 2008 6:59 pm
Posts: 19
Location: Burlington, VT
I did take a look at Spring.NET and I must be missing the point. I was able to accomplish everything I wanted to do with session factory configuration and storage in a few hundred lines of code, including multiple database support and an interceptor for maintaining audit fields. I was planning to add an HttpModule to it but why bother if I can achieve the same thing with a few lines of code in global.asax.

I also didn't like the DAO examples. I strongly believe that the ISession should be injected into repository classes since it represents a conversation and a repository cannot know which conversation it is participating in. This is important for Windows Forms applications where, for example, I might open two forms with different ISessions and make requests to the same repository.

I'm very skeptical of frameworks in general and I try to avoid the accidental complexity they often introduce. I'm also negative about additional XML configuration.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 2:23 pm 
Newbie

Joined: Wed Oct 01, 2008 10:59 am
Posts: 6
Ok cool.

Well you could inject the SessionFactory into whatever class you'd like. Peronally I am enamoured with the functionality Spring.NET provides without forcing you to use all. Even configuration can be done without XML if you would prefer that. I would happily discuss the merits of Spring, but this is hardly the place for it.

Good luck!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.