zoukankan      html  css  js  c++  java
  • 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>();
  • 相关阅读:
    Win10安装系统后,提示initializing and establishing link无法启动系统
    CSVN使用入门
    CSVN(SVN)命令入门及使用过程中遇到的错误问题汇总
    CSVN部署安装,实现web管理svn
    xenserver使用快照创建虚拟机,提示eth0 has different mac错误
    开机或者安装系统时提示tsc: Fast TSC calibration failed解决方法
    Nginx——debug的使用
    nginx屏蔽某一ip的访问
    plsql快速入门
    Web02_HTML&CSS
  • 原文地址:https://www.cnblogs.com/benbenzlj/p/2106612.html
Copyright © 2011-2022 走看看