John John
John John

Reputation: 1

Why do i need to Initiate the DBContext object inside the Controller class constructor & inside the Repository class constructor

I am reading the following tutorial about asp.net mvc + Entity framework:-

link

and i find that the author is initiating the DbContext object inside the repository class's constructor as follow:-

public class StudentRepository : IStudentRepository, IDisposable
    {
        private SchoolContext context;

        public StudentRepository(SchoolContext context)
        {
            this.context = context;
        }
    }

and also the author will be initiating the DBContext object inside the Controller class constructor as follow:-

 public class StudentController : Controller
   {
      private IStudentRepository studentRepository;

      public StudentController()
      {
         this.studentRepository = new StudentRepository(new SchoolContext());
      }

      public StudentController(IStudentRepository studentRepository)
      {
         this.studentRepository = studentRepository;
      }

      //

First Question. can anyone advice what is the purpose of initiating the DBContext object inside the Repository & Controller classes Constructor ? i mean i can replace the repository to be as follow:-

public class StudentRepository : IStudentRepository, IDisposable
    {
        private SchoolContext context = new SchcooleContext
//No need for this constructor !!!
      //  public StudentRepository(SchoolContext context)
     //   {
     //       this.context = context;
      //  }

and keep the controller class as is ...

second question . do i need to explicitly mention that the repository implements the IDisposable as follow:-

public class StudentRepository : IStudentRepository, IDisposable

now if i remove IDisposable the code should work well.. so what is the purpose of explicitly implementing the IDisposable inside the Repository class ? i mean since i will be calling the StudentRepository.Dispose() method from the Controller class itself,, and the base Controller class by default implement the Idisposable object.. so is there any valid reason to explicitly implement the IDisposable object inside the StudentRepository ?

Upvotes: 1

Views: 1917

Answers (1)

hatcyl
hatcyl

Reputation: 2352

To answer your questions:

can anyone advice what is the purpose of initiating the DBContext object inside the Repository & Controller classes Constructor ?

The practice of passing in dependencies in the constructor to the classes that need them is called Dependency Injection. It is generally considered good practice and has great advantages.

Actually, he is not! If you notice, the StudentRepository just accepts a SchoolContext. It never instantiates anything. That means that StudentRepository is happy taking any class that derives from SchoolContext and it can do its work. A common use case is, in production you will pass it the real thing, in testing you may pass it a Mock SchoolContext which never saves to the database but maybe just an in memory list.

You'll notice in StudentController he has 1 constructor which takes an IStudentRepository. This follows the same as above. Any ol' IStudentRepository will do. However, you also notice a constructor which instantiates both classes new StudentRepository(new SchoolContext());. This is actually referred to as Poor Mans Dependency Injection. It's a shortcut. In the real world you would typically see something in some startup config file that says: When a class needs an IStudentRepository give it new StudentRepository(new SchoolContext()).

do i need to explicitly mention that the repository implements the IDisposable as follow:-

IDisposable

Yes. DbContext implements IDisposable, and so anything that wraps it should as well. The idea is that when you are done with DbConext it needs to be closed. IDisposable communicates that. It is typically used for "unmanaged resources" such as files and connections that need to be "released".

Upvotes: 1

Related Questions