tags:

views:

20

answers:

1

Hi,

I have a set of aspx pages which are made of up usercontrols. Each usercontrol inherits from a base class. In that base class I want to put some common data objects (via EF) that I need access to in my user controls. How can I do this efficiently so that each user controls accesses the same class instance or copy of the data?

Thanks!

A: 

You could use the Singleton Per Request pattern. It's useful if you only want a single instance of a object to be available during the lifetime of a request.

It does this by storing the item in the HttpContext.Items collection.

public class SingletonPerRequest
{
    public static SingletonPerRequest Current
    {
        get
        {
            return (HttpContext.Current.Items["SingletonPerRequest"] ??
                (HttpContext.Current.Items["SingletonPerRequest"] = 
                new SingletonPerRequest())) as SingletonPerRequest;

        }
    }
}
Greg