/// <summary> /// 商品 /// </summary> public interface IProduct { string ClassName { get; set; } string ShowInfo(); } /// <summary> /// 牛奶 /// </summary> public class Milk : IProduct { public string ClassName { get; set; } public void ShowInfo() { return string.Format("牛奶:{0}", ClassName); } } /// <summary> /// 糖 /// </summary> public class Sugar : IProduct { public string ClassName { get; set; } public void ShowInfo() { return string.Format("糖:{0}", ClassName); } }/// <summary> /// 代码注入 /// </summary> public string ContainerCode() { IUnityContainer container = new UnityContainer(); container.RegisterType<IProduct, Milk>(); //默认注册(无命名),如果后面还有默认注册会覆盖前面的 container.RegisterType<IProduct, Sugar>("Sugar"); //命名注册 IProduct _product = container.Resolve<IProduct>(); //解析默认对象 _product.ClassName = _product.GetType().ToString(); string str1 = _product.ShowInfo();<br> IProduct _sugar = container.Resolve<IProduct>("Sugar"); //指定命名解析对象 _sugar.ClassName = _sugar.GetType().ToString(); string str2 = _sugar.ShowInfo();<br><br> StringBuilder strs = new StringBuilder();<br> strs.Append(str1);<br> strs.Append(str2);<br> IEnumerable<IProduct> classList = container.ResolveAll<IProduct>(); //获取容器中所有IProduct的注册的已命名对象 foreach (var item in classList) { item.ClassName = item.GetType().ToString(); strs.Append(item.ShowInfo()); }return strs.ToString(); }</pre>结果:牛奶:UnityTest.Milk 糖:UnityTest.Sugar 糖:UnityTest.Sugar
2、配置文件方式通过配置文件配置Unity信息需要有以下几个步骤:A、在配置文件<configSections> 配置节下注册名为unity的sectionB、在<configuration> 配置节下添加Unity配置信息C、在代码中读取配置信息,并将配置载入到UnityContainer中配置文件内容如下:<configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <!--声明容器--> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/> </configSections> <unity> <!--定义类型别名--> <aliases> <add alias="IProduct" type="UnityTest.IProduct,UnityTest" /> <add alias="Milk" type="UnityTest.Milk,UnityTest" /> <add alias="Sugar" type="UnityTest.Sugar,UnityTest" /> </aliases> <!--容器--> <container name="MyContainer"> <!--映射关系--> <register type="IProduct" mapTo="Milk"></register> <register type="IProduct" mapTo="Sugar" name="Sugar"></register> </container> </unity></pre>添加引用:using System.Configuration;using Microsoft.Practices.Unity.Configuration;<pre class="code" data-lang="javascript" style="box-sizing: border-box; margin: 0px; padding: 0px; white-space: pre-wrap;">/// <summary> /// 配置文件注入 /// </summary> public string ContainerConfiguration() { //加载容器配置 IUnityContainer container = new UnityContainer(); container.LoadConfiguration("MyContainer"); UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");//获取指定名称的配置节 section.Configure(container, "MyContainer");//获取特定配置节下已命名的配置节<container name='MyContainer'>下的配置信息 IProduct classInfo = container.Resolve<IProduct>("Sugar"); classInfo.ClassName = classInfo.GetType().ToString(); return classInfo.ShowInfo(); }</pre>结果:糖:UnityTest.Sugar
如果系统比较庞大,那么对象之间的依赖关系可能就会很复杂,最终导致配置文件变得很大,所以我们需要将Unity的配置信息从App.config或web.config中分离出来到某一个单独的配置文件中,比如Unity.config,实现方式可以参考如下代码:<?xml version="1.0" encoding="utf-8"?><configuration> <system.web> <compilation debug="true" targetFramework="4.5.2" /> <httpRuntime targetFramework="4.5.2" /> </system.web> <configSections> <!--声明容器--> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/> </configSections> <unity> <!--定义类型别名--> <aliases> <add alias="IProduct" type="UnityTest.IProduct,UnityTest" /> <add alias="Milk" type="UnityTest.Milk,UnityTest" /> <add alias="Sugar" type="UnityTest.Sugar,UnityTest" /> </aliases> <!--容器--> <container name="MyContainer"> <!--映射关系--> <register type="IProduct" mapTo="Milk"></register> <register type="IProduct" mapTo="Sugar" name="Sugar"></register> </container> </unity></configuration></pre>
注册代码:/// <summary> /// 配置文件注入 /// </summary> public string ContainerConfiguration2() { IUnityContainer container = new UnityContainer(); string configFile = "Unity.config"; var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile }; //从config文件中读取配置信息 Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); //获取指定名称的配置节 UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); //载入名称为FirstClass 的container节点 container.LoadConfiguration(section, "MyContainer"); IProduct classInfo = container.Resolve<IProduct>("Sugar"); classInfo.ClassName = classInfo.GetType().ToString(); return classInfo.ShowInfo(); }</pre>结果:糖:UnityTest.Sugar
(图片来源网络,侵删)
0 评论