I have been using option 2 below to refactor some legacy code. However, I have seen a lot of people doing injection using option 1. Can someone help me review the code below and let me know if there is any issue with what am doing? Both of these options make the "repository" available to all Action Results within the controller. Your input is appreciated.
Code:
Public interface ICustomerRepository
{
void Insert(Customer obj);
...
}
Public class CustomerRepository:ICustomerRepository
{
Public void Insert(Customer obj)
{
...
}
}
Controller usage - option 1:
Public class CustomerController : Controller
{
private ICustomerRepository repository = null;
public CustomerController()
{
this.repository = new CustomerRepository();
}
public CustomerController(ICustomerRepository repository)
{
this.repository = repository;
}
public ActionResult Insert(Customer obj)
{
repository.Insert(obj);
...
}
}
Controller usage - option 2:
Public class CustomerController : Controller
{
ICustomerRepository repository = new CustomerRepository();
public ActionResult Insert(Customer obj)
{
repository.Insert(obj);
...
}
}