关于javascript:MVC依赖注入和AutoFac

30次阅读

共计 18626 个字符,预计需要花费 47 分钟才能阅读完成。

么是依赖?
简略的说,如果 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<Problem> 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<MockUserService>().As<IUserService>();

        //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/…

正文完
 0