开发环境:vs201五、.net4.5.二、mvc五、ef6css
IOC控制反转(Inversion of Control,缩写为IOC),Autofac是一个开源的依赖注入框架,Autofac是asp.net中比较经常使用的IOC容器之一web
IOC的目标是消除代码中的new(实例化)语句,把实例化类的控制权转移到别的地方,这个地方一般会在一个程序加载时只执行一次的全局方法中,达到解耦的目的。api
DI依赖注入(Dependency Injection,缩写为DI),组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中。依赖注入的目的并不是为软件系统带来更多功能,而是为了提高组件重用的频率,并为系统搭建一个灵活、可扩展的平台。经过依赖注入机制,咱们只须要经过简单的配置,而无需任何代码就可指定目标须要的资源,完成自身的业务逻辑,而不须要关心具体的资源来自何处,由谁实现。架构
经过Nuget安装Autofac和Autofac.Mvc5mvc
一、App_Start文件夹里新建AutoFacConfig.cs框架
using System; using System.Reflection; using System.Web.Mvc; using Autofac; using Autofac.Integration.Mvc; namespace cms.Web { public class AutoFacConfig { /// <summary> /// 负责调用autofac框架实现业务逻辑层和数据仓储层程序集中的类型对象的建立 /// 负责建立MVC控制器类的对象(调用控制器中的有参构造函数),接管DefaultControllerFactory的工做 /// </summary> public static void Register() { //实例化一个autofac的建立容器 var builder = new ContainerBuilder(); //告诉Autofac框架,未来要建立的控制器类存放在哪一个程序集 (UI),从当前运行的bin目录下加载程序集 Assembly controllerAss = Assembly.Load("cms.Web"); builder.RegisterControllers(controllerAss); //告诉autofac框架注册数据仓储层所在程序集中的全部类的对象实例 Assembly respAss = Assembly.Load("cms.DAL"); //以接口形式保存被建立类的对象实例 builder.RegisterTypes(respAss.GetTypes()).AsImplementedInterfaces(); //告诉autofac框架注册业务逻辑层所在程序集中的全部类的对象实例 Assembly serpAss = Assembly.Load("cms.BLL"); //以接口形式保存被建立类的对象实例 builder.RegisterTypes(serpAss.GetTypes()).AsImplementedInterfaces(); //建立一个Autofac的容器 var container = builder.Build(); //移除本来的mvc的容器,使用AutoFac的容器,将MVC的控制器对象实例交由autofac来建立 DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); } } }
注意:UI层须要引用dal和bll层asp.net
二、Global.asax配置Autofac函数
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); BundleTable.EnableOptimizations = true;//js、css压缩 MiniProfilerEF6.Initialize();//MiniProfiler监控ef GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();//webapi默认JSON AutoFacConfig.Register();//autofac:控制反转,依赖注入配置 }
使用构造函数注入ui
using System; using System.Web.Mvc; using cms.Model; using cms.IBLL; //using cms.BLL; //不须要应用bll,但须要引用IBLL namespace cms.Web.Areas.Admin.Controllers { public class NewsController : BaseController { //未使用Autofac前直接实例化的写法 //public newsBLL bll = new newsBLL(); public InewsBLL bll { get; set; } public NewsController(InewsBLL _ibll) { bll = _ibll; } [HttpPost] [ValidateAntiForgeryToken] [ValidateInput(false)] public ActionResult Add(news vmodel,FormCollection forms) { news model = new news(); model.title = Request["title"]; model.times = DateTime.Now; model = bll.Add(model); if (model.ID > 0) { return RedirectToAction("list"); } ViewData["mess"] = "添加失败"; return View(vmodel); } public ActionResult Edit(int id) { news model = bll.Find(id); return View(model); } // GET: Admin/Admins/Delete/5 public ActionResult Delete(int id) { if (bll.Delete(id)) { return Redirect(Request.UrlReferrer.ToString()); } else { Common.JSHelper.AlertRedirect("操做失败", Request.UrlReferrer.ToString()); } return RedirectToAction("list"); } } }
//ui层再也不依赖于BLL,只依赖于IBLL,BLL能够随意变更spa
end
属性注入方法https://www.jianshu.com/p/f5d6346b0a7b