× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Singleton in Asp.NET application

James Jensen
Feb 10, 2015
<p>What you're describing is not a Singleton, but a Request-Scoped object. ASP.NET MVC has strong support for dependency injection, and you should allow your DI bindings to determine where the context comes from, rather than instantiating it yourself. Ninject has binding syntax to support this. I think it goes:</p> <pre><code>Bind&lt;DataContext&gt;().ToSelf().InRequestScope(); </code></pre> <p>As long as you are using good Dependency-Injection patterns consistently, this should cause the same <code>DataContext</code> instance to be passed to every dependency you have within the same request.</p> <p>The advantage to relying on Dependency Injection for construction of your context is that if you want to change details like disabling change tracking on the context, you can simply change your DI binding to use a custom method or factory, and the rest of your code doesn't have to change at all.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/27551038/Singleton%20in%20Asp.NET%20application/27551666">Stack Overflow</a>.</p>
comments powered by Disqus