开发者

Shared data in base class in asp.net

I have a set of aspx pages which are made of up usercontrols. Each usercontr开发者_C百科ol 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?


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;

        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜