参考文档
http://www.cnblogs.com/zhangchenliang/archive/2013/01/08/2850970.html
http://www.tuicool.com/articles/yiUzQj
IUnityContainer container = new UnityContainer();#region 1.0 注册类型container.RegisterType<IKiss, Boy>();var boy = container.Resolve<IKiss>();Girl gril = new Girl(boy); #endregion#region 2.0 注册实例Boy boy2 = new Boy();container.RegisterInstance<IKiss>(boy2);var boy3 = container.Resolve<IKiss>();Girl gril1 = new Girl(boy3); #endregion#region 3.0 单例模式/*boy4和boy5的属性dt相同,* 可以证明是单例*/container.RegisterType<IKiss, Boy>(new ContainerControlledLifetimeManager());var boy4 = container.Resolve<IKiss>();var boy5 = container.Resolve<IKiss>(); #endregion#region 4.0 配置文档配置注册和解析IUnityContainer container1 = new UnityContainer();UnityConfigurationSection configuration = ConfigurationManager.GetSection(UnityConfigurationSection.SectionName)as UnityConfigurationSection;configuration.Configure(container1, "defaultContainer");var boy6 = container1.Resolve<IKiss>(); #endregion#region 5.0 带名称的注册和解析container.RegisterType<IKiss, Boy>("boy");container.RegisterType<IKiss, Boy1>("boy1");var boy7 = container.Resolve<IKiss>("boy1"); var list=container.ResolveAll<IKiss>();#endregion
public interface IKiss{void Kiss();}public class Girl {public IKiss boy;public Girl(IKiss boy){ this.boy = boy; }public void Kiss(){this.boy.Kiss();Console.WriteLine("girl is kissing");}}public class Boy : IKiss{public Boy(){this.dt = DateTime.Now;}public DateTime dt { get; set; }public void Kiss(){Console.WriteLine("boy is kissing");}}public class Boy1 : IKiss{public Boy1(){this.dt = DateTime.Now;}public DateTime dt { get; set; }public void Kiss(){Console.WriteLine("boy1 is kissing");}}
<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.5" /></startup><unity><containers><container name="defaultContainer"><register type="DI.IKiss,DI" mapTo="DI.Boy,DI"/></container></containers></unity> </configuration>
四种注入方式
public interface IPeople { void Live();}public interface IEat { void Eat();}public interface ISleep { void Sleep(); }public interface IPlay { void Play();}public class PlayBoy : IPeople{//构造函数注入public IEat PEat { get; set; }//属性注入 [Dependency]public ISleep PSleep { get; set; }public IPlay PPlay { get; set; }public PlayBoy(IEat eat){ this.PEat = eat; }[InjectionMethod]public void Initialize(IPlay play){ this.PPlay = play; }public void Live(){this.PEat.Eat();this.PSleep.Sleep();this.PPlay.Play();Console.WriteLine("playboy is walking");}}public class BoyEat : IEat{public void Eat(){Console.WriteLine("eat rice and meat");}}public class BoySleep : ISleep{public void Sleep(){Console.WriteLine("sleep at 12:00 and wake up at 8:00");}}public class BoyPlay : IPlay{public void Play(){Console.WriteLine("play war3 and dota");}}
注册和解析
IUnityContainer container = new UnityContainer();container.RegisterType<IPeople, PlayBoy>();container.RegisterType<IEat, BoyEat>();container.RegisterType<ISleep, BoySleep>();container.RegisterType<IPlay, BoyPlay>();var p = container.Resolve<IPeople>();p.Live();