zoukankan      html  css  js  c++  java
  • IOC使用

    1.public static void InitAutoFac()

    {
    //得到你的HttpConfiguration.
    var configuration = GlobalConfiguration.Configuration;
    var builder = new ContainerBuilder();
    //注册控制器
    //builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
    ////可选:注册Autofac过滤器提供商.
    //builder.RegisterWebApiFilterProvider(configuration);
    var webapiAssembly = Assembly.Load("SpecialtyTests.API");
    builder.RegisterAssemblyTypes(webapiAssembly);
    var bll = Assembly.Load("SpecialtyTests.BLL");
    builder.RegisterAssemblyTypes(bll);

    var _interfaceType = typeof(IDALBase);
    /*注册的DAL*/
    builder.RegisterAssemblyTypes(Assembly.Load("SpecialtyTests.DAL")).Where(m => _interfaceType.IsAssignableFrom(_interfaceType)).AsImplementedInterfaces().InstancePerLifetimeScope();
    IContainer container = builder.Build();
    //将依赖关系解析器设置为Autofac。
    var resolver = new AutofacWebApiDependencyResolver(container);
    configuration.DependencyResolver = resolver;


    }
    }

    2.

    public class WebApiApplication : System.Web.HttpApplication
    {
    protected void Application_Start()
    {
    AutoFacConfig.InitAutoFac();//调用InitAutoFac()方法执行
    GlobalConfiguration.Configure(WebApiConfig.Register);
    }
    }

    using Autofac;

    using Autofac.Integration.WebApi;

    3.把方法写接口里

      

    UserInterFace dal;
    public UserLoginBll(UserInterFace user)
    {
    dal = user;
    }

     

  • 相关阅读:
    linux基础命令1
    linux下nginx搭建
    linux 对外开放端口
    linux下mysql 登录修改密码与数据库备份
    linux下mysql部署
    linux下mysql启动 Starting MySQL. ERROR! The server quit without updating PID file(xxx/x.pid)
    aptitude软件状态标志i、v、p
    GNU各软件版本历史站点
    glibc库和glib库
    禁用ipv6的两种方法
  • 原文地址:https://www.cnblogs.com/MenBe/p/10121698.html
Copyright © 2011-2022 走看看