简体   繁体   中英

Is there a good way to achieve dependency injection in ASP.NET MVC application without any DI Container?

So I have created the simplest of the applications using ASP.NET MVC (in .NET Framework) and I want to demo dependency injection to a junior using this. it is very simple code, even a newbie can understand it.

public interface IMovieRepository
{
    MyViewModel GetDetails(string movie);
}

public class MoviesController : Controller
{
    [HttpPost]
    public async Task<ActionResult> Search(string movie)
    {
        // Confusion here! Is the below line a good place to inject dependency
        IMovieRepository repository = new DBMovieRepository(); // <-- it can be DBMovieRepository or WebServiceMovieRepository or MockMovieRepository
        MovieFinder finder = new MovieFinder(repository);
        MyViewModel model = await finder.Find(movie);       
        return View(model);
    }
}

public class MovieFinder
{
    private IMovieRepository _repository;

    public MovieFinder(IMovieRepository repository)
    {
        _repository = repository;
    }

    public async Task<MyViewModel> Find(string movie)
    {
        // Connect to database and return a detailed object
        var myViewModelObj = _repository.GetDetails(movie);
        return myViewModelObj;
    }
}

public class DBMovieRepository : IMovieRepository
{
    public MyViewModel GetDetails()
    {
        // Code for fetching data from a database
    }
}

public class WebServiceMovieRepository : IMovieRepository
{
    public MyViewModel GetDetails()
    {
        // Code for fetching data from a IMDB webservice
    }
}

public class MockMovieRepository : IMovieRepository
{
    public MyViewModel GetDetails()
    {
        // Code for returning a mock object here
    }
}

There is an inline comment in the code where I asked that my confusion is here. Is that a good place to inject dependencies or do I have to inject it in the MoviesController constructor. If yes, then how will I pass it to the constructor? This is a .NET Framework 4.5 application and I don't want to complicate the lesson by introducing a DI Container and just want to use Pure DI to clarify the concept to my junior.

This is a .NET Framework 4.5 application and I don't want to complicate the lesson by introducing a DI Container and just want to use Pure DI to clarify the concept to my junior.

That literally doesn't make sense.

Pure DI

Or Dependency Injection simply means using IOC to provide an Instance of a Dependency to the object (via Constructor or Property).

Inversion of Control simply means that instead of the dependent object creating the Dependency, you invert control to something else to provide the instance.

You can Architect your Controller to use IOC. You can't DI without something providing the instance. The most lightweight/semi-hacky way to write your DI with minimum effort using your stack is to write your own Controller Factory to inspect your constructor for its dependencies and inject whatever those dependencies are (or just assume what it needs, pass it... yikes).

Relevant SO Question of Mine: My MVC Custom ControllerFactory works but could it be better?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM