app.config
<?xml version="1.0"?> <configuration> <configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/> </startup> <unity> <containers> <!--容器--> <container name="defaultContainer"> <!--映射关系--> <register type="ThreeKindIocByUnity.IA, ThreeKindIocByUnity" mapTo="ThreeKindIocByUnity.A, ThreeKindIocByUnity"/> <register type="ThreeKindIocByUnity.IB, ThreeKindIocByUnity" mapTo="ThreeKindIocByUnity.B, ThreeKindIocByUnity"/> <register type="ThreeKindIocByUnity.IC, ThreeKindIocByUnity" mapTo="ThreeKindIocByUnity.C, ThreeKindIocByUnity"/> <register type="ThreeKindIocByUnity.ID, ThreeKindIocByUnity" mapTo="ThreeKindIocByUnity.D, ThreeKindIocByUnity"/> <register type="ThreeKindIocByUnity.IE, ThreeKindIocByUnity" mapTo="ThreeKindIocByUnity.E, ThreeKindIocByUnity"/> </container> </containers> </unity> </configuration>
控制台:
/* * 关于IoC/DI 所谓控制反转(IoC: Inversion Of Control)就是应用本身不负责依赖对象的创建和维护,而交给一个外部容器来负责。这样控制权就由应用转移到了外部IoC容器,控制权就实现了所谓的反转。比如,在类型A中需要使用类型B的实例,而B实例的创建并不由A来负责,而是通过外部容器来创建。 有时我们又将IoC成为依赖注入(DI: Dependency Injection)。所谓依赖注入,就是由外部容器在运行时动态地将依赖的对象注入到组件之中。具体的依赖注入方式又包括如下三种典型的形式。 •构造器注入(Constructor Injection):IoC容器会智能地选择选择和调用适合的构造函数以创建依赖的对象。如果被选择的构造函数具有相应的参数,IoC容器在调用构造函数之前会自定义创建相应参数对象; •属性注入(Property Injection):如果需要使用到被依赖对象的某个属性,在被依赖对象被创建之后,IoC容器会自动初始化该属性; • 方法注入(Method Injection):如果被依赖对象需要调用某个方法进行相应的初始化,在该对象创建之后,IoC容器会自动调用该方法。 在开源社区,具有很有流行的IoC框架,比如Castle Windsor、Unity、Spring.NET、StructureMap、Ninject等。 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration; namespace ThreeKindIocByUnity { public interface IA { } public interface IB { } public interface IC { } public interface ID { } public interface IE { } public class A : IA { public IB B { get; set; } [Dependency] public IC C { get; set; } public ID D { get; set; } public A(IB b) { this.B = b; } [InjectionMethod] public void Initialize(ID d) { this.D = d; } } public class B : IB { } public class C : IC { } public class D : ID { } public class E : IE{ } class Program { static void Main(string[] args) { IUnityContainer container = new UnityContainer(); UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName); configuration.Configure(container, "defaultContainer"); A a = container.Resolve<IA>() as A; if (null != a) { Console.WriteLine("a.B == null ? {0}", a.B == null ? "Yes" : "No"); Console.WriteLine("a.C == null ? {0}", a.C == null ? "Yes" : "No"); Console.WriteLine("a.D == null ? {0}", a.D == null ? "Yes" : "No"); } E e1 = container.Resolve<IE>() as E; Console.WriteLine("e == null ? {0}", e1 == null ? "Yes" : "No"); //E e2 = container.Resolve<IE>("ClassE") as E; //Console.WriteLine("e == null ? {0}", e2 == null ? "Yes" : "No"); Console.Read(); } } }