Search code examples
.netentity-frameworkunit-testingdependency-injectionconstructor-injection

Is there a way to combine dependency injection with IDisposable?


A colleague and I were just talking about unit testing when I realised that I write code differently in certain circumstances...

If I'm using Entity Frameworks on its own, I'll tend to use the IDisposable interface via a using block:

using (Context ctx = new Context()) { whatever(); }

But when I'm writing code that will be unit tested, I move the instantiation of the context into a constructor, so that I can pass in mocks to be used instead. I understand that this is a type of dependency injection, specifically "constructor injection".

Taking only a cursory look, I see this latter pattern recommended in a couple of articles:

The problem with this (as my colleague and I reflected) is that the dispose method is never called. I suppose these objects get finalised as they move out of scope, but it seems less than ideal.

So, my question: is there a way to combine dependency injection with use of IDisposable?

I can imagine three possible categories of answer:

  1. "Yes, there is a way and here's how..."
  2. "No, there isn't a way. This is something you'll have to live with."
  3. "Tom, you have completely misunderstood something."

Whichever it is, we'd be really grateful!


Solution

  • The latter. We're only using DI to inject mock DB contexts, so we're not using a full-blown DI container.

    You are correct, the DbContext instance will never have Dispose called on it (assuming you are not manually calling dispose in one of your methods). I recommend you select a good DI framework (my personal fav. is AutoFac). The framework will handle disposing anything that implements IDisposable, is registered with the framework, and is injected/supplied by the framework.