码迷,mamicode.com
首页 > 其他好文 > 详细

IOC使用

时间:2018-12-14 23:01:30      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:接口   static   star   config   auto   ace   tpc   protected   tin   

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;
}

 

 

IOC使用

标签:接口   static   star   config   auto   ace   tpc   protected   tin   

原文地址:https://www.cnblogs.com/MenBe/p/10121698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!