3

When using entity framework, is it bad practice to inject the repository into a controller?

For example if I have a service:

public class DogService
{
    IMyDbContext _myDbContext;
    public DogService(IMyDbContext myDbContext)
    {
        _myDbContext = myDbContext;
    } 


    public void CreateDog(string name)
    {
        //create a dog using the myDbContext
    }
}

Would the above be bad practice since we are not explicitly disposing of the repository, and would it be better to do:

public void CreateDog(string name, IMyDbContext myDbContext)
{
     using(myDbContext)
     {
          //create a dog using the myDbContext
     }
}

the stucture of mydbcontext:

public class MyDbContext : DbContext, IMyDbContext {}

How do I dispose of the myDbContext?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • 2
    You don't typically "inject" a context. A context is created, used, and destroyed. More commonly, a _repository_ or something else is injected that then _uses_ the context. – D Stanley Sep 26 '16 at 18:19
  • thank you very much ive updated the question – Alex Gordon Sep 26 '16 at 18:21
  • 2
    To answer the other question, whatever _creates_ the disposable object is typically responsible for _disposing_ of it. When you inject it into another class it complicates things. – D Stanley Sep 26 '16 at 18:22
  • @DStanley does that mean that my connection is disposed of as long as the object that creates the repository is disposed? – Alex Gordon Sep 26 '16 at 18:24
  • Only if the object that creates it disposes of it in the `Dispose` method. – D Stanley Sep 26 '16 at 18:43
  • You could use a factory to create the context and then wrap the factory call in a `using` if you don't want your DI framework to handle this for you. – stephen.vakil Sep 26 '16 at 18:49
  • you could also pass the context in as a factory such as Func... though you could just make your repository IDisposable and handle its lifetime from your IOC container. – Matthew Whited Sep 26 '16 at 19:49
  • _How_ do you inject it? Because that is where the answer will come from. – H H Sep 26 '16 at 21:06

3 Answers3

2

Main question - Will it be a good idea to inject the DBcontext using Dependency Injection, if Yes, then how does it get disposed

Yes it can be injected, if using Ninject IOC, following would help:

kernel.Bind<DBContext>().ToSelf().InRequestScope();

check out the following link, it describes both the patterns and also provides details regarding how to create a single DBcontext per HttpRequest, no matter how many controllers get invoked in the process.

Another useful link, How to handle DBContext when using Ninject

Community
  • 1
  • 1
Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74
  • Not only with Ninject of course. Lifetime management becomes the responsibility of the DI container. – H H Sep 26 '16 at 21:05
  • @HenkHolterman agreed Ideally that shall be the case but with so many DI frameworks floating around, I am little vary of a blanket claim, Popular one like Ninject I have used, so is 100 % sure – Mrinal Kamboj Sep 28 '16 at 05:22
1

Yes, you can inject the repository interface when you use the repository pattern. I mean you can inject it using controller's constructor.

Lifetime of a Repository :

All repository instances are Transient. It means, they are instantiated per usage.Hence you don't need to worry about the dispose of the Context.

Here is an example : this is a repository pattern.you have to inject the Interface of the repository.On this example where it uses a service layer.But you can do it on your controller as well.

public class PersonAppService : IPersonAppService
{
    private readonly IRepository<Person> _personRepository;

    public PersonAppService(IRepository<Person> personRepository)
    {
        _personRepository = personRepository;
    }

    public void CreatePerson(CreatePersonInput input)
    {        
        person = new Person { Name = input.Name, EmailAddress = input.EmailAddress };
        _personRepository.Insert(person);
    }
}

You can read more about it here : Repositories

Sampath
  • 63,341
  • 64
  • 307
  • 441
  • Please don't use `inline code` to highlight random programming-related terms. It doesn't make your posts more readable, on the contrary even. See also [When should code formatting be used for non-code text?](http://meta.stackoverflow.com/questions/254990/when-should-code-formatting-be-used-for-non-code-text). – CodeCaster Sep 26 '16 at 18:55
  • 1
    @CodeCaster OK sure, Thanks.I think this is the 2nd time where you have given this advice.I'll follow it here after :) – Sampath Sep 26 '16 at 18:57
-3

You could let DogService implement IDisposable and call myDbContext.Dispose() from DogService's Dispose method.

user1796440
  • 366
  • 3
  • 11