简体   繁体   English

在没有任何 DI 容器的情况下,有没有一种在 ASP.NET MVC 应用程序中实现依赖注入的好方法?

[英]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.所以我使用 ASP.NET MVC(在 .NET Framework 中)创建了最简单的应用程序,我想使用它向初级演示依赖注入。 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.这是注入依赖项的好地方还是我必须将它注入MoviesController构造函数中。 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.这是一个 .NET Framework 4.5 应用程序,我不想通过引入 DI 容器来使课程复杂化,而只想使用 Pure DI 向我的后辈阐明这个概念。

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.这是一个 .NET Framework 4.5 应用程序,我不想通过引入 DI 容器来使课程复杂化,而只想使用 Pure DI 向我的后辈阐明这个概念。

That literally doesn't make sense.从字面上看是没有意义的。

Pure DI纯DI

Or Dependency Injection simply means using IOC to provide an Instance of a Dependency to the object (via Constructor or Property).或者依赖注入只是意味着使用 IOC 为对象提供依赖的实例(通过构造函数或属性)。

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.您可以构建您的控制器以使用 IOC。 You can't DI without something providing the instance.如果没有提供实例的东西,你就不能进行DI 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).使用堆栈以最少的工作量编写 DI 的最轻量级/半hacky 的方法是编写自己的控制器工厂来检查构造函数的依赖项并注入任何这些依赖项(或者只是假设它需要什么,传递它.. . 哎呀)。

Relevant SO Question of Mine: My MVC Custom ControllerFactory works but could it be better?我的相关 SO 问题: 我的 MVC 自定义 ControllerFactory 可以工作,但会更好吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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