mercredi 7 octobre 2015

Class which supports injectable mocked object but also using statement c#

How do I go about creating a class which wraps all EF repository calls in a Using statement whilst also supporting an injectable interface of the repository?

I can't seem to wrap my head around having this class support 2 different types of instantiation.

public class MyClass(IRepo repo)
{
    _repo = repo;
}

public void MyMethod()
{
   using ( var db = new DbContxt )
   {
        var repo = new Repo(db);
        repo.GetById(1);
   }
}

In essence, the life-time of the 'db' object is the lifetime of the method call. Whereas the lifetime of 'db' would be managed outside of the class if injected.

Aucun commentaire:

Enregistrer un commentaire