Unity是微软团队开发的一个轻量级,可扩展的依赖注入容器,为松散耦合应用程序提供了很好的解决方案,支持构造器注入,属性注入,方法注入。
同样根据控制反转IOC与依赖注入DI中的例子
interface IDal { void save(); } class SqlServerDal : IDal { public void save() { Console.WriteLine("SqlServer save."); } } class OracleDal : IDal { public void save() { Console.WriteLine("Oracle save."); } }
Unity的实现如下:
IUnityContainer container = new UnityContainer(); container.RegisterType参考文档:
http://unity.codeplex.com http://www.cnblogs.com/dotview/archive/2011/08/08/2136762.html
public class MyObject { private SomeOtherObject _dependentObject; [Dependency] public SomeOtherObject DependentObject { get { return _dependentObject; } set { _dependentObject = value; } } }
IUnityContainer uContainer = new UnityContainer(); MyObject myInstance = uContainer.Resolve(); // now access the property containing the dependency SomeOtherObject depObj = myInstance.DependentObject;
public class MyObject { private IMyInterface _interfaceObj; private MyBaseClass _baseObj; [Dependency] public IMyInterface InterfaceObject { get { return _interfaceObj; } set { _interfaceObj = value; } } [Dependency] public MyBaseClass BaseObject { get { return _baseObj; } set { _baseObj = value; } } }
IUnityContainer uContainer = new UnityContainer() .RegisterType() .RegisterType (); MyObject myInstance = uContainer.Resolve (); // now access the properties containing the dependencies IMyInterface depObjA = myInstance.InterfaceObject; MyBaseClass depObjB = myInstance.BaseObject;
public class MyObject { private IMyInterface _objA, _objB; [Dependency("MapTypeA")] public IMyInterface ObjectA { get { return _objA; } set { _objA = value; } } [Dependency("MapTypeB")] public IMyInterface ObjectB { get { return _objB; } set { _objB = value; } } }
IUnityContainer uContainer = new UnityContainer() .RegisterType("MapTypeA") .RegisterType ("MapTypeB"); MyObject myInstance = uContainer.Resolve (); // now access the properties containing the dependencies IMyInterface depObjA = myInstance.ObjectA; IMyInterface depObjB = myInstance.ObjectB;
public class MyObject { public MyObject(MyDependentClass myInstance) { // work with the dependent instance myInstance.SomeProperty = "SomeValue"; // or assign it to a class-level variable } }
IUnityContainer uContainer = new UnityContainer(); MyObject myInstance = uContainer.Resolve();
public class MyObject { public MyObject(IMyInterface interfaceObj, MyBaseClass baseObj) { // work with the concrete dependent instances // or assign them to class-level variables } }
IUnityContainer uContainer = new UnityContainer() .RegisterType() .RegisterType (); MyObject myInstance = uContainer.Resolve ();
public class MyObject { public MyObject(SomeOtherClass myObjA) { .... } [InjectionConstructor] public MyObject(MyDependentClass myObjB) { .... } }
IUnityContainer uContainer = new UnityContainer(); MyObject myInstance = uContainer.Resolve();
public class MyObject { private SomeOtherObject dependentObject; [InjectionMethod] public void Initialize(SomeOtherObject dep) { // assign the dependent object to a class-level variable dependentObject = dep; } }
IUnityContainer uContainer = new UnityContainer(); MyObject myInstance = uContainer.Resolve();
public class MyObject { private IMyInterface depObjectA; private MyBaseClass depObjectB; [InjectionMethod] public void Initialize(IMyInterface interfaceObj, MyBaseClass baseObj) { depObjectA = interfaceObj; depObjectB = baseObj; } }
IUnityContainer uContainer = new UnityContainer() .RegisterType() .RegisterType (); MyObject myInstance = uContainer.Resolve ();
参考资料 http://www.cnblogs.com/benbenzlj/archive/2011/07/14/2106612.html