Database Context and Singleton injection with IoC

Posted by zaitsman on Programmers See other posts from Programmers or by zaitsman
Published on 2013-12-29T22:27:41Z Indexed on 2014/05/29 9:41 UTC
Read the original article Hit count: 142

Filed under:
|
|
|

All of the below relates to a ASP.NET c# app. I have a Singleton Settings MemoryCache that reads values from database on first access and caches these, then invalidates them using SQL Service Broker message and re-reads as required.

For the purposes of standard controllers, i create my Db Context in a request scope. However, this obviously means that i can't use the same context in the Settings Cache class, since that is a singleton and we have a scope collision.

At the moment, i ended up with two db contexts - the Controllers get it via IoC container, whereas a Singleton just creates it's own.

However, i am not satisfied with this approach (mostly due to the way i feel about two contexts, the cache doesn't set anything on the db hence concurrency is not an issue as much).

What is a better way to do it?

© Programmers or respective owner

Related posts about c#

Related posts about ASP.NET