IOC Unity容器

 Unity容器 文件配置读取信息

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");//找配置文件的路径
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

IUnityContainer container = new UnityContainer();
section.Configure(container, "testContainer1");
IPhone android = container.Resolve<IPhone>("Android");
android.Call();

 

 

 1 <configuration>
 2   <configSections>
 3     <!--<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>-->
 4     <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
 5   </configSections>
 6   <unity>
 7     <!--<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>-->
 8     <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/>
 9     <containers>
10       <container name="testContainer1">
11         <extension type="Interception"/>
12         <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.ApplePhone, Ruanmou.Service"/>
13         <!--是dll名称,不是命名空间-->
14         <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.AndroidPhone, Ruanmou.Service" name="Android">
15           <!--别名-->
16           <interceptor type="InterfaceInterceptor"/>
17           <interceptionBehavior type="Ruanmou.Framework.AOP.LogBeforeBehavior, Ruanmou.Framework"/>
18           <interceptionBehavior type="Ruanmou.Framework.AOP.LogAfterBehavior, Ruanmou.Framework"/>
19           <interceptionBehavior type="Ruanmou.Framework.AOP.ParameterCheckBehavior, Ruanmou.Framework"/>
20           <lifetime type="transient" />
21         </register>
22 
23         <register type="Ruanmou.Interface.IMicrophone, Ruanmou.Interface" mapTo="Ruanmou.Service.Microphone, Ruanmou.Service"/>
24         <register type="Ruanmou.Interface.IHeadphone, Ruanmou.Interface" mapTo="Ruanmou.Service.Headphone, Ruanmou.Service"/>
25         <register type="Ruanmou.Interface.IPower, Ruanmou.Interface" mapTo="Ruanmou.Service.Power, Ruanmou.Service"/>
26         <register type="Ruanmou.Interface.AbstractPad, Ruanmou.Interface" mapTo="Ruanmou.Service.ApplePad, Ruanmou.Service"/>
27         <register type="Ruanmou.IBLL.IBaseBll, Ruanmou.IBLL" mapTo="Ruanmou.BLL.BaseBll, Ruanmou.BLL">
28           <constructor>
29             <param name="baseDAL" type="Ruanmou.IDAL.IBaseDAL, Ruanmou.IDAL"  />
30             <param name="id" type="System.Int32" value="3" />
31           </constructor>
32         </register>
33         <register type="Ruanmou.IDAL.IBaseDAL, Ruanmou.IDAL" mapTo="Ruamou.DAL.BaseDAL, Ruamou.DAL"/>
34         <register type="Ruanmou.IDAL.IDBContext`1, Ruanmou.IDAL" mapTo="Ruamou.DAL.DBContextDAL`1, Ruamou.DAL"/>
35       </container>
36 
37 
38       <!--<container name="testContainer">
39         <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.AndroidPhone, Ruanmou.Service.Extend"/>
40         <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.AndroidPhone, Ruanmou.Service.Extend" name="Android"/>
41         <register type="Ruanmou.Interface.IMicrophone, Ruanmou.Interface" mapTo="Ruanmou.Service.Microphone, Ruanmou.Service.Extend"/>
42         <register type="Ruanmou.Interface.IHeadphone, Ruanmou.Interface" mapTo="Ruanmou.Service.Headphone, Ruanmou.Service.Extend"/>
43         <register type="Ruanmou.Interface.IPower, Ruanmou.Interface" mapTo="Ruanmou.Service.Power, Ruanmou.Service.Extend"/>
44       </container>
45 
46       <container name="testContainerAOP">
47         <extension type="Interception"/>
48         <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.ApplePhone, Ruanmou.Service">
49           <interceptor type="InterfaceInterceptor"/>
50           <interceptionBehavior type="Ruanmou.Framework.LogBehavior, Ruanmou.Framework"/>
51           <lifetime type="transient" />
52           <constructor>
53             -->
54       <!--<param name="pubContext" type="IPubContext" />-->
55       <!--
56             <param name="id" type="System.Int32" value="3" />
57           </constructor>
58         </register>
59         <register type="Ruanmou.Interface.IMicrophone, Ruanmou.Interface" mapTo="Ruanmou.Service.Microphone, Ruanmou.Service"/>
60         <register type="Ruanmou.Interface.IHeadphone, Ruanmou.Interface" mapTo="Ruanmou.Service.Headphone, Ruanmou.Service"/>
61         <register type="Ruanmou.Interface.IPower, Ruanmou.Interface" mapTo="Ruanmou.Service.Power, Ruanmou.Service"/>
62         <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.AndroidPhone, Ruanmou.Service" name="android"/>
63         <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.ApplePhone, Ruanmou.Service" name="apple"/>
64       </container>
65 
66 
67       <container name="ruanmouContainer">
68         <register type="System.Data.Entity.DbContext, EntityFramework" mapTo="Ruanmou.EF.Model.JDDbContext, Ruanmou.EF.Model"/>
69         <register type="Ruanmou.Bussiness.Interface.IBaseService`1,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.BaseService`1, Ruanmou.Bussiness.Service"/>
70         <register type="Ruanmou.Bussiness.Interface.ICategoryService,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.CategoryService, Ruanmou.Bussiness.Service"/>
71         <register type="Ruanmou.Bussiness.Interface.ICommodityService,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.CommodityService, Ruanmou.Bussiness.Service"/>
72       </container>-->
73     </containers>
74   </unity>
75 </configuration>

 

 

 

posted @ 2021-03-04 22:34  YZM_Leo  阅读(89)  评论(0)    收藏  举报