简体   繁体   English

C#类库 - 单例设计模式

[英]C# Class Library - Singleton Design Pattern

Background/Question: 背景/问题:

I'm fairly new to the singleton design pattern. 我对单身设计模式相当新颖。 I've used it once in a web application (with the help of the SO community): 我在Web应用程序中使用过一次(在SO社区的帮助下):

public static AppGlobal Instance
{
    get
    {
        if (HttpContext.Current.Session != null)
        {
            HttpSessionState session = HttpContext.Current.Session;

            if (session["AppGlobalInstance"] == null)
            {
                session["AppGlobalInstance"] = new AppGlobal();
            }

            return (AppGlobal)session["AppGlobalInstance"];
        }
        else
        {
            return null;
        }
    }
}

The above implementation makes sense to me because the instance of the AppGlobal is stored in the session. 上面的实现对我有意义,因为AppGlobal的实例存储在会话中。 When the session dies, AppGlobal dies. 当会话终止时, AppGlobal就会死亡。 What happens if I use the same design pattern in a class library that is called by a web application ? 如果我在Web应用程序调用的类库中使用相同的设计模式会发生什么? For example, the users requests a page that calls methods in a DLL that doesn't know about the session. 例如,用户请求调用DLL中不知道会话的方法的页面。 Will the data stored in the singleton instance be persisted through multiple calls? 存储在单例实例中的数据是否会通过多次调用持久化?

private static readonly Singleton instance = new Singleton();
private Singleton() { }

public static Singleton Instance
{
    get
    {
        return instance;
    }
}

Additional Information: 附加信息:

Here's what I'm trying to accomplish: I have a web application that is going to receive XML requests from a third party application. 这就是我要完成的任务:我有一个Web应用程序将从第三方应用程序接收XML请求。 This XML will tell my web application to do one of three things (or all three of them). 此XML将告诉我的Web应用程序执行以下三种操作之一(或全部三种操作)。 I would like to have a singleton instance of a class that stores data that can be accessed by multiple classes. 我想有一个类的单例实例,用于存储可由多个类访问的数据。 I want the singleton instance to DIE after each request. 我想在每次请求后将单例实例发送到DIE If the above doesn't accomplish this, what's the best way to accomplish it? 如果上面没有达到这个目的,那么实现它的最佳方法是什么?

Note: This web application runs on a single server and will never run on a farm. 注意:此Web应用程序在单个服务器上运行, 永远不会在服务器场上运行。

EDIT 1: 编辑1:

Based on the suggestion below, I've used System.Web.HttpContext.Current.Session to store my class instance. 基于下面的建议,我使用System.Web.HttpContext.Current.Session来存储我的类实例。 Does this look like the correct approach for a singleton that will be unique to each session (remember I'm in a class library)? 对于每个会话都是唯一的单例,这看起来是否正确(请记住我在类库中)?

    public static Ariba Instance
    {
        get
        {
            if (HttpContext.Current.Session != null)
            {
                HttpSessionState session = HttpContext.Current.Session;

                if (session["AribaInstance"] == null)
                {
                    session["AribaInstance"] = new Ariba();
                }

                return (Ariba)session["AribaInstance"];
            }
            else
            {
                return null;
            }
        }
    }

It will be persisted through multiple calls, but there is one caveat. 它将通过多次调用持久化,但有一点需要注意。 The static variables are scoped to the AppDomain, so any time the IIS worker process is recycled, any data stored in a static variable will be lost. 静态变量的范围限定为AppDomain,因此每当IIS工作进程被回收时,存储在静态变量中的任何数据都将丢失。 The same is true of session data, if you're storing it "in proc." 会话数据也是如此,如果你将它存储在“proc”中。

If you want an object that will only exist for the duration of the HTTP request, you can use the HttpContext.Items property. 如果您想要一个仅在HTTP请求期间存在的对象,则可以使用HttpContext.Items属性。

Because the singleton is static, your data will be available for all the requests in your web application, so it will not be available only for the session. 由于单例是静态的,因此您的数据将可用于Web应用程序中的所有请求,因此它不会仅用于会话。

But in ASP.NET applications, you should avoid using Singletons. 但在ASP.NET应用程序中,您应该避免使用Singletons。 Instead you should use the Application object. 相反,您应该使用Application对象。 Main reason for that is that if you will use a web farm then your singleton is no longer singelton for the application scope but only on the machine. 主要原因是,如果您将使用Web场,那么您的单例不再是应用程序范围的singelton,而只是在计算机上。

Oh! 哦! If you want to use the instance PER REQUEST, why don't you pass it as a parameter to the methods you are calling or as a constructor parameter for the classes that requires the xml. 如果要使用实例PER REQUEST,为什么不将它作为参数传递给要调用的方法,或者作为需要xml的类的构造函数参数。 This will be the best design approach, I think. 我认为这将是最好的设计方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM