Ioc Autofac心得

对于这个容器注入,个人也不是很熟悉,很多还不懂,只会基本的操作,几天把它记录下来,说不定以后帮助就大了呢,这方面跟安卓差距还是挺大的

下面记录下应用的流程

步骤:

1.添加应用

2.重写工厂(这里讲的是常用的构造方法注入方式)

 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();//创建容器
27                 }
28                 return iContainer;
29             }
30         }
31     }
View Code

3.启动(更换)工厂

 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(服务器一启动就更换工厂)
13             ControllerBuilder.Current.SetControllerFactory(new CreateAutofacFactory());
14         }
15     }
View Code

4.添加配置文件

 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     <!--在配置文件中添加配置节点 name是配置节点的标签名 
 6         type是读取配置节点内容的类 格式:命名空间名.类名,程序集名-->
 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 Code

5.构造函数

1         IUserManager uMan = null;
2         public HomeController(IUserManager uMan)
3         {
4             this.uMan = uMan;
5         }    
View Code

五步走起,如果过程出错的话基本上都是配置文件配置出错,还有就是经常忘记更新dll类库,在这里要建议下把类库的输出路径改成到UI项目里,经常动手才会记忆深刻,一直在努力中,希望能走出自己的道路,虽然脆弱,却不退缩。

原文地址:https://www.cnblogs.com/LiuZhen/p/4023836.html