zoukankan      html  css  js  c++  java
  • 在MVC5和webAPI下是用Autofac依赖注入

    很多书本中都提到依赖注入,控制反转等概念,这些都是为了实现松耦合层、组件和类目的。

    常见的是使用Repository类分离Controller和Model的直接联系。而为了解除Repository类和Controller的紧密联系,通常不是直接定义Repository类并实例化,而是通过Controller的构造方法注入指定的Repository.

     

    1 public class ValuesController : ApiController
    2 {
    3 
    4 private IOneServices _oneServices;
    5 
    6 public ValuesController(IOneServices oneServices)
    7 
    8 {
    9 _oneServices = oneServices;
    10 
    11 }
    12      //..........
    13 }



    流行的IoC容器有:Ninject,Autofac,Unity.

    现就Autofac注入MVC5和Webapi2的使用方法做出简要讲解。

    1、使用nupkg引用Autofac,Autofac.Mvc5和Autofac.Webapi2

    PM> install-package autofac -version 3.5.0

    PM> install-package autofac.mvc5

    PM> install-package autofac.webapi2 (注意:您的项目中如果使用的是webapi2,此处必须为webapi2而不是webapi,否则在运行时将出现“重写成员“Autofac.Integration.WebApi.AutofacWebApiDependencyResolver.BeginScope()”时违反了继承安全性规则。重写方法的安全可访问性必须与所重写方法的安全可访问性匹配。”错误。)

    2.注册组件.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Http;
    using System.Reflection;
    using Autofac;
    using Autofac.Integration.Mvc;
    using Autofac.Integration.WebApi;
    using System.Web.Compilation;
    
    ......
    
    var builder=new ContainerBuilder();
    
    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());//注册api容器的实现
    
    builder.RegisterControllers(Assembly.GetExecutingAssembly());//注册mvc容器的实现
    
    //如果为Winform类型,请使用以下获取Assembly方法
    builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())//查找程序集中以services结尾的类型
    .Where(t => t.Name.EndsWith("Services"))
    .AsImplementedInterfaces();
    builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())//查找程序集中以Repository结尾的类型
    .Where(t => t.Name.EndsWith("Repository"))
    .AsImplementedInterfaces();
    
    //如果有web类型,请使用如下获取Assenbly方法
    
    var assemblys = BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList();
    
    builder.RegisterAssemblyTypes(assemblys.ToArray())//查找程序集中以Repository结尾的类型
    .Where(t => t.Name.EndsWith("Repository"))
    .AsImplementedInterfaces();




    3.创建一个Container以备后用.

    var container=builder.Build();

    4.从Container创建一个 lifetime scope .

    5.使用这个Lifetime Scope 来解析组件的实例.

    config.DependencyResolver = new AutofacWebApiDependencyResolver(container);//注册api容器需要使用HttpConfiguration对象
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));//注册MVC容器
    6.在WebApiConfig类的Register方法中,调用上述步骤代码,并传入HttpConfiguration对象。

    autofac的优点是可一次性解除耦合,而不需要配置;autofac更好的实现了MVC中“约定大于配置”的概念。

  • 相关阅读:
    Java实现 LeetCode 833 字符串中的查找与替换(暴力模拟)
    Java实现 LeetCode 833 字符串中的查找与替换(暴力模拟)
    Java实现 LeetCode 833 字符串中的查找与替换(暴力模拟)
    Java实现 LeetCode 832 翻转图像(位运算)
    Java实现 LeetCode 832 翻转图像(位运算)
    Java实现 LeetCode 832 翻转图像(位运算)
    Java实现 LeetCode 831 隐藏个人信息(暴力)
    Java实现 LeetCode 831 隐藏个人信息(暴力)
    Java实现 LeetCode 831 隐藏个人信息(暴力)
    how to use automapper in c#, from cf~
  • 原文地址:https://www.cnblogs.com/xujie520/p/5200815.html
Copyright © 2011-2022 走看看