Singleton pattern in web applications

前端 未结 5 1158
萌比男神i
萌比男神i 2021-02-05 21:46

I\'m using a singleton pattern for the datacontext in my web application so that I dont have to instantiate it every time, however I\'m not sure how web applications work, does

相关标签:
5条回答
  • 2021-02-05 21:59

    Many people keep the DataContext around for the duration of the request by keeping it in the HttpContext.Current.Items Thereby it is also private to the request.

    Have a look at this blogpost by Steve Sanderson, and the UnitOfWork pattern.

    0 讨论(0)
  • 2021-02-05 22:00

    Static variables are visible to all users on the per app domain, not per session. Once created, the variable will sit in memory for the lifetime of the app domain, even if there are no active references to the object.

    So if you have some sort of stateful information in a web app that shouldn't be visible to other users, it should absolutely not be static. Store that sort of information in the users session instead, or convert your static var to something like this:

    public static Data SomeData
    {
        get
        {
            if (HttpContext.Session["SomeData"] == null)
                HttpContext.Session["SomeData"] = new Data();
            return (Data)HttpContext.Session["SomeData"];
        }
    }
    

    It looks like a static variable, but its session specific, so the data gets garbage collected when the session dies and its totally invisible to other users. There safety is not guaranteed.

    Additionally, if you have stateful information in a static variable, you need some sort of syncronization to modify it, otherwise you'll have a nightmare of race conditions to untangle.

    0 讨论(0)
  • 2021-02-05 22:14

    I'm using a singleton pattern for the datacontext in my web application

    "Singleton" can mean many different things in this context. Is it single-instance per request? Per session? Per thread? Per AppDomain (static instance)? The implications of all of these are drastically different.

    A "singleton" per request (stored in the HttpContext) is fine. A singleton per session is discouraged, but can be made to work. A singleton per thread may appear to work but is likely to result in unexpected and difficult-to-debug behaviour. A singleton per Application or AppDomain is a disaster waiting to happen.

    so that I dont have to instantiate it every time

    Creating a DataContext is very, very cheap. The metadata is globally cached, and connections aren't created until you actually execute a query. There is no reason to try to optimize away the construction of a DataContext instance.

    however I'm not sure how web applications work, does IIS open a thread for every user connected?

    IIS uses a different thread for every request, but a single request may use multiple threads, and the threads are taken from the Thread Pool, which means that ultimately the same user will have requests on many different threads, and conversely, different users will share the same thread over multiple requests and an extended period of time. That is why I mention above that you cannot rely on a Thread-Local Singleton.

    if so, what would happend if my singleton is not thread safe?

    Very bad things. Anything that you cache globally in an ASP.NET application either needs to be made thread safe or needs to be locked while it is in use.

    Also, is it OK to use a singleton pattern for the datacontext? Thanks.

    A DataContext is not thread-safe, and in this case, even if you lock the DataContext while it is in use (which is already a poor idea), you can still run into cross-thread/cross-request race conditions. Don't do this.

    DataContext instances should be confined to the scope of a single method when possible, using the using clause. The next best thing is to store them in the HttpContext. If you must, you can store one in the Session, but there are many things you need to be aware of (see this question I answered recently on the ObjectContext - almost all of the same principles apply to a DataContext).

    But above all, do not create "global" singleton instances of a DataContext in an ASP.NET application. You will deeply regret it later.

    0 讨论(0)
  • 2021-02-05 22:14

    @ryudice the web server creates a new thread for each request. I think the best approach is to have a datacontext bound to each request, meaning that you should create a new datacontext every time you serve a request. A good way of achieving this is by using a DI tool, such as StructureMap. These kind of tools allow you to setup the lifecycle of the instances you configure, so for example in your case you would configure your XDataContext class to be HttpContext scoped.

    Regards.

    0 讨论(0)
  • 2021-02-05 22:17

    here are Microsoft's examples on how to do multi-tier with LINQ-To-SQL.

    http://code.msdn.microsoft.com/multitierlinqtosql

    0 讨论(0)
提交回复
热议问题