Database Context and Singleton injection with IoC
- by zaitsman
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?