• 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>();
  • 相关阅读:
    sqlserver2005新特性介绍
    Sql 数据库 用户密码MD5加密
    easyui datagrid
    JQ js 对数组的操作
    c#2.0锐利体验《泛型编程》读书笔记
    jQuery EasyUI DataGrid Checkbox 数据设定与取值
    数据库的常用命令
    关于background-image设置背景图片
    Css背景设置 、
    实时监听输入框值变化的完美方案:oninput & onpropertychange
  • 原文地址:https://www.cnblogs.com/benbenzlj/p/2106612.html
Copyright © 2020-2023  润新知