Unity 笔记(2) 注入方式

依赖注入方式

  • 属性注入
  • 构造函数注入
  • 方法注入

1.属性注入

1.1 具体类型属性注入

public class MyObject
{
    private SomeOtherObject _dependentObject;

    [Dependency]
    public SomeOtherObject DependentObject
    {
        get { return _dependentObject; }
        set { _dependentObject = value; }
    }
} 
IUnityContainer uContainer = new UnityContainer();
MyObject myInstance = uContainer.Resolve<MyObject>();

// now access the property containing the dependency
SomeOtherObject depObj = myInstance.DependentObject;

1.2 接口和抽象类属性注入

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<IMyInterface, FirstObject>()
   .RegisterType<MyBaseClass, SecondObject>();
MyObject myInstance = uContainer.Resolve<MyObject>();

// now access the properties containing the dependencies
IMyInterface depObjA = myInstance.InterfaceObject;
MyBaseClass depObjB = myInstance.BaseObject;

1.3 给属性注入命名

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<IMyInterface, FirstObject>("MapTypeA")
   .RegisterType<IMyInterface, SecondObject>("MapTypeB");
MyObject myInstance = uContainer.Resolve<MyObject>();

// now access the properties containing the dependencies
IMyInterface depObjA = myInstance.ObjectA;
IMyInterface depObjB = myInstance.ObjectB;

2.构造函数注入

2.1 单构造函数

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<MyObject>();

2.2 参数为接口或者基类注入

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<IMyInterface, FirstObject>()
   .RegisterType<MyBaseClass, SecondObject>();
MyObject myInstance = uContainer.Resolve<MyObject>();

2.3 多重构造函数通过属性指定注入构造函数

public class MyObject
{

    public MyObject(SomeOtherClass myObjA)
    {
        ....
    }

    [InjectionConstructor]
    public MyObject(MyDependentClass myObjB)
    {
        ....
    }
}
IUnityContainer uContainer = new UnityContainer();
MyObject myInstance = uContainer.Resolve<MyObject>();

3.方法注入

3.1 方法依赖于具体类

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<MyObject>();

3.2 方法依赖于抽象类或接口

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<IMyInterface, FirstObject>()
   .RegisterType<MyBaseClass, SecondObject>();
MyObject myInstance = uContainer.Resolve<MyObject>();
posted @ 2011-07-14 17:25  EdisonZ  阅读(593)  评论(1编辑  收藏  举报