MVC:依赖注入和AutoFac

么是依赖?
简单的说,如果A(对象)完成它的一个工作,需要B(对象)的帮助,我们就是A依赖于B。

比如,我们项目中,IndexModel的OnGet()方法需要ProblemRepository对象的Get()方法,这就可以说成:IndexModel依赖IProblemRepository。

public class IndexModel : PageModel
{

public void OnGet()
{
    //其他代码……
    Problems = new ProblemRepository().Get(pageIndex, pageSize);
}

}
依赖的另一种说法就是:耦合。

有两个非常著名的设计原则,同学们应该烂熟于心,张嘴就来:

高内聚,低耦合
依赖抽象,而不是依赖实现
1和2其实就是目标和方法的关系,即要做到1这种效果,可以采用2这种方法。

我们上面的代码就是依赖实现(具体的ProblemRepository类),要依赖抽象,我们需要抽象出一个接口:IProblemRepository

public interface IProblemRepository
{
    IList Get(int pageIndex, int pageSize);
}

然后由ProblemRepository实现它……


【延伸】

本来:是在PageModel的构造函数中确定着Service的生成,

    public IndexModel()
    {
        _repository = new MockProblemRepository();
    }现在是:

从外部“注入”到IndexModel中,这就被称之为:依赖注入

具体使用哪一个service由startup.cs控制,这就被称之为:控制反转(IoC:Inversion of Control)

AutoFac

依赖抽象,而不是依赖实现

同ASP.NET core内置的Service一样,是一种第三方依赖注入工具。因为其在MVC中非常流行,所以除了Autofac组件以外,还有专门的MVC integration组件:

通过nuget下载了上述两个Autofac类库之后:

注册
在Global.asax文件中,MvcApplication.cs的Application_Start()方法中:

        //1. 获得一个ContainerBuilder(容器建造器)对象 
        ContainerBuilder builder = new ContainerBuilder();

        //2. 注册Controller和FilterProvider
        builder.RegisterControllers(typeof(MvcApplication).Assembly);
        builder.RegisterFilterProvider();

        //配置:指示使用何种interface实例呢?
        //2.1 可以一个类一个类的注册
        //builder.RegisterType().As();

        //2.2 也可以整个程序集的注册
        builder.RegisterAssemblyTypes(typeof(ProdUserService).Assembly)
            .AsImplementedInterfaces();

        //3. 通过ContainerBuilder得到一个IContainer容器对象,
        //4. 并为MVC自动设定解析(resolve:获取“接口对象”)
        IContainer container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

注意上述代码,1/2/3/4都只需要原样照写就是了。需要开发者自定义设置的是:2.1和2.2。

per-request lifetime support.

how dependency lifetime scopes work in general

为什么还是要讲AutoFac?

兼顾“旧技术”,一看就是培训班出来的!
开始培养自己的学习能力

注入
可以通过构造函数注入,和ASP.NET core中一样。

Filter中没有构造函数,所以使用的是属性注入。https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...

你可能感兴趣的:(javascript)