Ioc Autofac心得
生活随笔
收集整理的這篇文章主要介紹了
Ioc Autofac心得
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
對于這個容器注入,個人也不是很熟悉,很多還不懂,只會基本的操作,幾天把它記錄下來,說不定以后幫助就大了呢,這方面跟安卓差距還是挺大的
下面記錄下應(yīng)用的流程
步驟:
1.添加應(yīng)用
2.重寫工廠(這里講的是常用的構(gòu)造方法注入方式)
1 public class CreateAutofacFactory : DefaultControllerFactory 2 { 3 public override IController CreateController(RequestContext requestContext, string controllerName) 4 { 5 //獲取容器 6 IContainer ic = CreateControllers.CreateController().Icontainer; 7 //獲取控制器類型 8 Type t = this.GetControllerType(requestContext, controllerName); 9 //從容器中解析出對象 10 return ic.Resolve(t) as IController; 11 } 12 } View Code 1 public class CreateControllers 2 { 3 private CreateControllers() { } 4 private static CreateControllers createController; 5 public static CreateControllers CreateController() 6 { 7 if (createController == null) 8 createController = new CreateControllers(); 9 return createController; 10 } 11 private IContainer iContainer { get; set; } 12 public IContainer Icontainer 13 { 14 get 15 { 16 if (iContainer == null) 17 { 18 ContainerBuilder cb = new ContainerBuilder(); 19 //用代碼注冊類型 20 //cb.RegisterType<StudentDao>().As<IStudentDao>(); 21 //cb.RegisterType<StudentManager>().As<IStudentManager>(); 22 //cb.RegisterType<HomeController>(); 23 24 //從配置文件注冊類型 25 cb.RegisterModule(new ConfigurationSettingsReader("autofac")); 26 iContainer = cb.Build();//創(chuàng)建容器 27 } 28 return iContainer; 29 } 30 } 31 } View Code3.啟動(更換)工廠
1 public class MvcApplication : System.Web.HttpApplication 2 { 3 protected void Application_Start() 4 { 5 AreaRegistration.RegisterAllAreas(); 6 7 WebApiConfig.Register(GlobalConfiguration.Configuration); 8 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 9 RouteConfig.RegisterRoutes(RouteTable.Routes); 10 BundleConfig.RegisterBundles(BundleTable.Bundles); 11 12 //換ControllerFactory(服務(wù)器一啟動就更換工廠) 13 ControllerBuilder.Current.SetControllerFactory(new CreateAutofacFactory()); 14 } 15 } View Code4.添加配置文件
1 <configSections> 2 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 3 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 4 5 <!--在配置文件中添加配置節(jié)點 name是配置節(jié)點的標簽名 6 type是讀取配置節(jié)點內(nèi)容的類 格式:命名空間名.類名,程序集名--> 7 <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/> 8 9 </configSections> 10 11 <autofac> 12 <components> 13 <component type="Winning.Tjgi.Backstage3G.UI.Controllers.HomeController,Winning.Tjgi.Backstage3G.UI" 14 /> 15 <component 16 type="test.Dal.UserDao,test.Dal" 17 service="test.IDal.IUserDao,test.IDal"/> 18 <component 19 type="test.Bll.UserManager,test.Bll" 20 service="test.IBll.IUserManager,test.IBll" /> 21 </components> 22 </autofac> View Code5.構(gòu)造函數(shù)
1 IUserManager uMan = null; 2 public HomeController(IUserManager uMan) 3 { 4 this.uMan = uMan; 5 } View Code五步走起,如果過程出錯的話基本上都是配置文件配置出錯,還有就是經(jīng)常忘記更新dll類庫,在這里要建議下把類庫的輸出路徑改成到UI項目里,經(jīng)常動手才會記憶深刻,一直在努力中,希望能走出自己的道路,雖然脆弱,卻不退縮。
轉(zhuǎn)載于:https://www.cnblogs.com/LiuZhen/p/4023836.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Ioc Autofac心得的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySql默认编码所造成的乱码麻烦1.2
- 下一篇: 深入学习Web Service系列---