Answer the question
In order to leave comments, you need to log in
[DI] Resolve type inside controller method (ASP.MVC). How to implement?
Good day.
My project has a bunch of different repositories and their interfaces. In the controller method, I would like to receive the implementation of a specific repository as follows:
public ActionResult SomeMethod(){
var someRepo = ObjectFactory.Resolve<ISomeRepository>();
return Content(someRepo.SomeAction());
}
Answer the question
In order to leave comments, you need to log in
Are there any out-of-the-box solutions to resolve types inside a controller method (preferably customizable via XML like in Unity)?
private readonly ISomeRepository _someRepo;
public MyController(ISomeRepository someRepo)
{
if(someRepo == null)
throw new ArgumentNullException(nameof(someRepo));
_someRepo = someRepo;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question