I'm using ADO.NET EF in an MVC application. I'm considering putting the ObjectContext inside HttpContext.Current so that all logic in the same request can access to it without having to open/destroy each time. However, I'm really sure if it's a good way to manage ObjectContext instances. I have 2 questions regarding this need:
As HttpContext.Current property is backed by a thread-local field and ASP.NET uses threads from pool to handle requests, is it possible that an ObjectContext instance put into HttpContext.Current by a request will be visible to a subsequent request running on the same thread from the pool?
How do you think ObjectContext should be managed in ASP.NET MVC to both avoid lots of opening/disposing and prevent race conditions?
Use the Repository pattern. Override Controller.Dispose to dispose the Repository, which, in turn, disposes the DataContext.
I would use an IoC container like StructureMap, Autofac, Windosor, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With