AutoFac是.net平台下的IOC容器產品。今天學習一下它的使用方法。
1.最簡單的使用。
public interface ITestDao { string SayHello(); } public class TestDao : ITestDao { public string SayHello() { return "hello world!"; } } var builder = new ContainerBuilder(); builder.RegisterType<TestDao>().As<ITestDao>(); IContainer container = builder.Build(); var tdao = container.Resolve<ITestDao>(); string saystr = tdao.SayHello();
builder.RegisterType<TestDao>().As<ITestDao>();注冊TestDao是ITestDao的實現。
然后下面進行調用就行。
2.跟mvc一起使用
public interface ITestDao { string SayHello(); } public class TestDao : ITestDao { public string SayHello() { return "hello world!"; } } public class HomeController : Controller { private ITestDao _testDao; public HomeController(ITestDao testDao) { this._testDao = testDao; } public ActionResult Index() { string saystr = _testDao.SayHello(); return View(); } }
在Global.asax里需要添加幾行代碼
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { //依賴注入 var builder = new ContainerBuilder(); SetupResolveRules(builder); builder.RegisterControllers(Assembly.GetExecutingAssembly()); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } private void SetupResolveRules(ContainerBuilder builder) { builder.RegisterType<TestDao>().As<ITestDao>(); } }
3.將注冊代碼放進配置文件。
public interface ITestDao { string SayHello(); } public class TestDao : ITestDao { public string SayHello() { return "hello world!"; } } public class HomeController : Controller { private ITestDao _testDao; public HomeController(ITestDao testDao) { this._testDao = testDao; } public ActionResult Index() { string saystr = _testDao.SayHello(); return View(); } }
在Global.asax里需要添加幾行代碼
protected void Application_Start() { //依賴注入 var builder = new ContainerBuilder(); builder.RegisterModule(new ConfigurationSettingsReader("autofac")); builder.RegisterControllers(Assembly.GetExecutingAssembly()); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
在Web.config添加配置文件
<configSections> <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration" /> </configSections> <autofac defaultAssembly="MvcApplication11"> <components> <component type="MvcApplication11.Models.TestDao" service="MvcApplication11.Models.ITestDao" /> </components> </autofac>
4.系統自動注冊,省略掉手工的配置和xml文件配置。(2014-07-16補充)
寫一個基礎接口,讓你所有的接口都實現此接口,此接口起到的作用僅僅是個標識作用。
public interface IAutofac//標識接口 { } public interface ITestDao : IAutofac { string SayHello(); } public class TestDao : ITestDao { public string SayHello() { return "hello world!"; } } public class HomeController : Controller { private ITestDao _testDao; public HomeController(ITestDao testDao) { this._testDao = testDao; } public ActionResult Index() { string saystr = _testDao.SayHello(); return View(); } }
然后更改Global.asax里面的代碼,AppDomain.CurrentDomain.GetAssemblies()獲取當前程序域里所有的程序集,下面讓RegisterAssemblyTypes自動注冊所有繼承了IAutofac的接口。
protected void Application_Start() { //依賴注入 var builder = new ContainerBuilder(); //builder.RegisterModule(new ConfigurationSettingsReader("autofac")); var assemblys = AppDomain.CurrentDomain.GetAssemblies().ToList(); builder.RegisterAssemblyTypes(assemblys.ToArray()) .Where(e => typeof(IAutofac).IsAssignableFrom(e) && e != typeof(IAutofac)) .AsImplementedInterfaces() .InstancePerLifetimeScope(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }