專案中引入Autofac的目的是為了實現控制反轉,即IoC,Inversion of Control。控制反轉可以有效的降低類之間的相互依賴關係,增加架構的彈性,降低軟體複雜度。
示例程式碼:
IProvinceRepository.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Libing.Portal.Web.Models { public interface IProvinceRepository { List<Libing.Portal.Web.Models.Province> GetProvinceList(); } }
ProvinceRepository.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Libing.Portal.Web.Models { public class ProvinceRepository : IProvinceRepository { public List<Libing.Portal.Web.Models.Province> GetProvinceList() { using (var ctx = new PortalContext()) { return ctx.Provinces.ToList(); } } } }
ProvinceController.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Libing.Portal.Web.Controllers { public class ProvinceController : Controller { private Libing.Portal.Web.Models.IProvinceRepository _provinceRepository; public ProvinceController(Libing.Portal.Web.Models.IProvinceRepository provinceRepository) { _provinceRepository = provinceRepository; } public ActionResult Index() { return View(_provinceRepository.GetProvinceList()); } } }
ICityRepository.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Libing.Portal.Web.Models { public interface ICityRepository { List<Libing.Portal.Web.Models.City> GetCityList(); } }
CityRepository.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Libing.Portal.Web.Models { public class CityRepository : ICityRepository { public List<Libing.Portal.Web.Models.City> GetCityList() { using (var ctx = new PortalContext()) { return ctx.Cities.ToList(); } } } }
CityController.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Libing.Portal.Web.Controllers { public class CityController : Controller { private Libing.Portal.Web.Models.IProvinceRepository _provinceRepository; private Libing.Portal.Web.Models.ICityRepository _cityRepository; public CityController(Libing.Portal.Web.Models.IProvinceRepository provinceRepository, Libing.Portal.Web.Models.ICityRepository cityRepository) { _provinceRepository = provinceRepository; _cityRepository = cityRepository; } public ActionResult Index() { return View(_cityRepository.GetCityList()); } } }
Global.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Routing; using System.Reflection; using Autofac; using Autofac.Integration.Mvc; namespace Libing.Portal.Web { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); #region Autofac注入依賴 var builder = new ContainerBuilder(); SetupResolveRules(builder); builder.RegisterControllers(Assembly.GetExecutingAssembly()); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); #endregion WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); } private static void SetupResolveRules(ContainerBuilder builder) { builder.RegisterType<Libing.Portal.Web.Models.ProvinceRepository>().As<Libing.Portal.Web.Models.IProvinceRepository>(); builder.RegisterType<Libing.Portal.Web.Models.CityRepository>().As<Libing.Portal.Web.Models.ICityRepository>(); } } }
在Global.cs中的SetupResolveRules可以使用下面的方式來註冊:
private static void SetupResolveRules(ContainerBuilder builder) { builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) .Where(t => t.Name.EndsWith("Repository")) .AsImplementedInterfaces(); }
利用ContainerBuilder的RegisterAssemblyTypes()方法來註冊元件中的所有類,通過Where()篩選出全部以“Repository”結尾的類,並自動找出這些類所實現的介面,然後設定與其對應。