Singleton pattern in web applications

前端 未结 5 1203
萌比男神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 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.

提交回复
热议问题