Because of the stateless nature of ASP.NET, traditional session management doesn't work so well. If we try to create a single session and use it across several pages or requests, then this statelessness makes it more difficult for us to maintain this than it would in say a Winforms application.
There are a few strategies that we can use to overcome this, including implementing a Singleton pattern, storing the session in the user's Context, or using another framework such as NHibernate.Burrow.
The Singleton is probably the easiest to implement. We simply create a sealed class (meaning all of the member methods and variables are declared static) and create a property called Instance
. Then, we create a non-static constructor that has the implementation details we want such as the SessionFactory
property.
In C#, this SessionProvider
class would look as follows:
public sealed class SessionProvider { static readonly SessionProvider instance = new SessionProvider(); public static...