How to create DbContext for long running process?

Oct 4, 2012 at 4:21 PM


Is it overhead with data from database if using constructor injection?


Why not use this?

using(var context = new DbContext)



Oct 4, 2012 at 9:29 PM


As far as I know, using construction injection will result in a small overhead compared to creating an instance manually, as the container needs to analyze the class through reflexion, resolve the dependencies, etc. However, this overhead only affects the construction of the object; the performance of the code in the object should not be affected.

If this overhead is significant or not will depend mostly each application. For most applications, it's not an issue as the performance loss experienced when using constructor injection is minimal compared to the benefits obtained. However, if your application requires to have a very high performance, using dependency injection might not be an useful approach.

As a side note, based on my understanding, it's not recommended to use the DbContext directly. Usually, the recommended approach is to implement a kind of service which would wrap the DbContext and manage it. Then, the application interacts with the service to consume data instead of interacting directly with the DbContext. For example, in MileageStats, the application interacts with the data through the implementation of repositories.

I hope you find this useful,

Damian Cherubini

Oct 4, 2012 at 10:01 PM

Hi DCherubini,

I found that the DbContext contain lots of data from db and get added overtime.