Do you need a framework?

time to read 2 min | 319 words

I had a discussion about a session life style management, and the guy I was talking with mentioned a framework that would handle that for me. It made me think for a while, because my first instinct was to ask, what for?

Here is how I usually handle session life style management in web applications nowadays:

public class Global: System.Web.HttpApplication
{
	public static ISessionFactory SessionFactory = CreateSessionFactory();
	
	protected static ISessionFactory CreateSessionFactory()
	{
		return new Configuration()
			.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"))
			.BuildSessionFactory();
	}
	
	public static ISession CurrentSession
	{
		get{ return (ISession)HttpContext.Current.Items["current.session"]; }
		set { HttpContext.Current.Items["current.session"] = value; }
	}
	
	protected void Global()
	{
		BeginRequest += delegate
		{
			CurrentSession = SessionFactory.OpenSession();
		};
		EndRequest += delegate
		{
			if(CurrentSession != null)
				CurrentSession.Dispose();
		};
	}
}

And yes, I copy / paste it or recreate it from scratch whenever I need to handle this in a new application. To be frank, it is simpler to do so than to try to package a it in a resuable form that would make sense to use.

Now, if you wanted support for things like multiple session life styles, then you are starting to talk about enough complexity to justify using a framework. But for the most part, code such as the one above is all that you would require to get things done, because it most applications, you only require a single lifestyle.

The same is true for the other cases as well, not just session life style management. There is a reason that I don’t particularly like commons and util libraries. In order to make such things useful as libraries, you have to satisfy a wide variety of scenarios, which complicate your life. I find that single purpose, single use, “snippets” (for lack of a better word) work better for me for the simple infrastructure concerns.