I am working with asp.net mvc 5 project. suppose i am showing Customer data where i am showing customer detail and customer favorite products.
so i fetch data from customer repository, country repository and favorite repository.
many time people write article on injection repository by unity DI. when i am working with single repository then this concept make sense but when i have to fetch data from multiple repositories then how could i inject multiple repositories in mvc controller ctor by unity di?
see a small code for injecting repository by unity DI.
public class FooController : Controller
{
readonly IFooRepository _repository;
// Inject here
public ProductController(IFooRepository repository)
{
_repository = repository;
}
// Use it here
public ActionResult Bar()
{
var bar = _repository.DoSomething();
}
}
above code refer from https://forums.asp.net/t/2105895.aspx?Repository+inject+into+controller+by+Unity+DI
now tell me how could i refactor my code or what approach i should follow as a result i can work with multiple repositories and also can inject by Unity DI.
please give me best guidance. thanks
Just add any dependency you require into the controller's constructor.
public class FooController : Controller
{
readonly IFooRepository _repository;
readonly IOtherRepository _otherRepository;
public ProductController(IFooRepository repository, IOtherRepository otherRepository)
{
_repository = repository;
_otherRepository = otherRepository;
}