Apr 27

The S#arp framework: the SharpArch.Web – part I

Posted in S#arp      Comments Off on The S#arp framework: the SharpArch.Web – part I

In this post we’re going to start looking at the goodies available on the SharpArch.Web assembly introduced by the S#arp framework. This assembly contains several helpers which you can reuse in your ASP.NET MVC projects. In this post, we’re concentrating on the WebSessionStorage class.

I’m not sure if you remember it, but we talked about the ISessionStorage a few days ago. As we saw, this interface is used for managing the storage of a session along a specific time. Here’s the interface again, for those that missed the post or don’t remember it:

public interface ISessionStorage {
        ISession Session { get; set; }

The WebSessionStorage implements this interface and guarantees that for each request you’ll reuse the same ISession instance across all stages of the ASP.NET pipeline. In other words, it ensures that the recommended session-per-request pattern is applied to all the web requests. As you might expect, the class uses the HttpContext.Items collection for saving the ISession instance that will be reused across the request. It handles the HttpApplication.EndRequest for ensuring proper cleanup of the current session.

Using this class is simple, as you can see from the following snippet copied from the Northwind demo that accompanies the framework:

NHibernateSession.Init(new WebSessionStorage(this),
                           new string[] { Server.MapPath("~/bin/Northwind.Data.dll") },
                           new AutoPersistenceModelGenerator().Generate(),

As you can see, this type is passed to the NHIbernateSession’s Init method so that it gets used by the other classes that rely on the NHibernateSession.Current property for getting a valid instance of an ISession. Do keep in mind that this code isn’t thread safe and should be called only once during the lifetime of the application.

And that’s it. I guess that there’s not much more we can say about this class. Keep tuned for more on the S#arp framework.