session state variables and singleton class

I have a situation where I need to retrieve data from a query which executes for almost half a minute and bring it to a web page. (There is no way to reduce this time because the maximum amount of optimization has been performed on it) I use a four layer architecture along with Entity Framework ( EF, Data Access Layer, Biz Logic Layer, UI) for my application. I'm trying to use the singleton method when an instance to the DAL is created (The DAL in turn retrieves data from the database) so that I will be able to re-use this instance and hence additional instances wont be created within the same session. How do I go about setting the session state and checking the availability of the instance in the State Server?

public static Singleton getInstance() {
     if (**instance == null**)
       instance = new Singleton();
     return instance;
   }

What should reside within the if block? What condition should I check for in the if block? I'm really unsure as to what I must do.

PS: This session must have a timeout of 5 mins. I hear this can be specified in the Web.config file. is it true?

Answers


To be honest you should rather use Entity Framework context and create it every time you need access to database, i.e. in each method. It is optimized to be used that way. Connection pooling will make sure there is not penalty in recreating EF context each time. This is the best practice.

But your DAL might be more than just simple DB access. If you want to have it as a singleton separate for each session you must create the instance on the first request, store it into the Session and check if it's there before using. With thread safety the code could look like that:

class DALClass
{
    private static object instanceLock = new object();

    public static DALClass Instance
    {
        get
        {
            if (Session["DALInstance"] == null)
            {
                lock (instanceLock)
                {
                    if (Session["DALInstance"] == null)
                    {
                        Session["DALInstance"] = new DALClass();
                    }
                }
            }

            return (DALClass)Session["DALInstance"];
        }
    }
}

It sounds to me like you have a well defined architecture which would suit dependency injection. Using DI you could just get your IOC container to return you a singleton object or a transient one. However, be very careful using singletons in a web environment as they often cause more trouble than they are worth.

If the query you are running contains user specific data then I would probably place the results of that query into session within the code which composes the UI part of your application if you are using a pattern like MVC that would be in the controller or MVP in the presenter.

If these patterns aren’t in use then you could consider placing the information into session inside the business layer but only if you wrap up the session and inject in that dependency into your business object, e.g. something like “IUserSession”. The business project should not contain a reference to “system.Web” or anything like that.


Need Your Help

Displaying an image from Local Network to JSF Web Application

networking jsf-2 datatable local

I've some image file in my Local Network and I want to display them in the DataTable in JSF. I'm using JSF2.0 and Tomahawk 1.1.13. Below is the JSF code.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.