I dont quite get when I should use a Singleton or OnePerThread pattern when building a web application.
Could anyone tell us some situations of when we should know to implement these patterns?
Familiar with ASP.NET MVC.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Generally, this is not the way to approach a problem: “What patterns should I use”. Patters should come naturally.
For a web-app:
one-per-thread is usually request data, because each request is handled by a separate thread – everything that needs to be specific for a given request is here. Example: A database connection. Each request is (generally) associated with one db connection. It cannot be shared among multiple requests.
singleton – this is everything that is stateless, or more properly stated – there is only one state and it does not depend on the thread currently using the object. Example: a service that processed payments. It does not store any state – it just calls the payment provider given some payment parameters.