zoukankan      html  css  js  c++  java
  • Unity用法(构造器注入的两种方法)

    对象的之前的依赖关系的表现方式之一,主对象通过构造器将其所依赖对象的实例的引入,并在将来的某个时候应用该实例。 而IOC对于该问题的解耦方法是应用

    Constructor Injection 方式。

     什么情况下使用Constructor Injection


    • 当实例化父对象时也能自动实例化所依赖的对象
    • 通过简单的方式使得很容易做到在代码中查看每个类所依赖的项
    • 父对象的构造器不需要很多相互间有关联关系的构造器
    • 父对象的构造器不需要很多参数
    • 通过不使用属性和方法暴露给调用程序,达到封装字段值以不能被看到的目的
    • 通过修改依赖对象的代码来控制哪些对象可以被注入,而不用改动父对象或应用程

     代码方式:

    准备工作

    public class Song
    {
        
    public string Singer
        {
            
    get
            {
                
    return "Westlife";
            }
        }

        
    public string Name
        {
            
    get
            {
                
    return "My Love";
            }
        }
    }

    public class Owner
    {
        
    public string Name
        {
            
    get
            {
                
    return "Inrie";
            }
        }

        
    public int Age
        {
            
    get
            {
                
    return 24;
            }
        }
    }

    public interface IPlayer
    {
        void Play();
    }

     

    开始

    根据类构造器的个数分成下面两种情况:
    1.单个构造器
    2.多个构造器

    1.单个构造器:

    Unity对于单个构造器的情况,将做自动的依赖注入。

    看个例子:

    public class Mp3Player : IPlayer
    {
        
    public Song mSong;

        
    public Mp3Player(Song song,Owner owner)
        {
            
    this.mSong = song;
        }

        
    public void Play()
        {
            Console.WriteLine(
    string.Format("Now Playing [{0}] Singing by ({1})"this.mSong.Name, this.mSong.Singer));
        }
    }


    可以通过下面的方式来获取Mp3Player对象实例。

    IUnityContainer container = new UnityContainer();
    container.RegisterType
    <IPlayer, Mp3Player>();

    IPlayer player 
    = container.Resolve<IPlayer>();

    player.Play(); 

     输出为:


    上例中Mp3Player类中只有一个构造器,该构造器依赖于两个类:Song和Owner类。Unity容器在执行Resolve方法时会自动装配所依赖类的实例,注入到构造器中。

    当类中只有一个构造器时,Unity容器在装配时自动以此构造器为默认构造器。


    2.多个构造器:

    Unity对于多个构造器的情况,需要配合[InjectionConstructor]特性来实现依赖注入。

    有以下几个原则:
    A.有加上[InjectionConstructor]标签的构造器为依赖注入的构造器

    public class Mp3Player : IPlayer
    {
        
    public Song mSong;

        [InjectionConstructor]
        
    public Mp3Player(Owner owner)
        {
            
        }

        
    public Mp3Player(Song song,Owner owner)
        {
            
    this.mSong = song;
        }

        
    public void Play()
        {
            Console.WriteLine(
    string.Format("Now Playing [{0}] Singing by ({1})"this.mSong.Name, this.mSong.Singer));
        }
    }

    该例中为 Mp3Player(Owner owner) 构造器贴上[InjectionConstructor]标签,所以该构造器将作为依赖注入的构造器。

    B.如果没有任何构造器有贴上[InjectionConstructor],则使用参数最多的构造器作为依赖注入的构造器

    public class Mp3Player : IPlayer
    {
        
    public Song mSong;

        
    public Mp3Player(Owner owner)
        {
            
        }

        
    public Mp3Player(Song song,Owner owner)
        {
            
    this.mSong = song;
        }

        
    public void Play()
        {
            Console.WriteLine(
    string.Format("Now Playing [{0}] Singing by ({1})"this.mSong.Name, this.mSong.Singer));
        }
    }

    该例中由于有多个构造器,同时也没有为任何一个构造器贴上[InjectionConstructor]标签,则使用参数最多的构造器,即Mp3Player(Song song,Owner owner)构造器作为依赖注入的构造器。

    这里有一点需要注意:如果有多个构造器都是属于"参数最多"的构造器,则会出现错误。

    注入到存在的对象实例

    用Resolve方法来获取已存在的对象实例时不会做 Constructor  Injection,因为该对象的创建没受到 Unity 容器的任何影响。甚至用BuildUp方法也不行。作为取代的方法,可以采用 Property Injection。

    关于BuildUp方法可参考:
    Unity Application Block 1.0系列(5): 使用BuildUp让对象实例也支持依赖注入 

    关于Property Injection可参考:

    Unity Application Block 1.0系列(3): 属性/设值方法注入(Property/Setter Injection)  

    配置文件方式:

    首先在配置文件中引入以下配置节,前提是要先加上类型别名。即加上<typeAlias>配置节。

            <register  type="Database"
                    mapTo="UnityTestLib.CustomDatabase,
                    UnityTestLib">
              <constructor>
                <param name="connString" type="System.String">
                  <value value="connection value..."/>
                </param>
                <param name="logger" type="ILogger">
                  <dependency />
                </param>
              </constructor>

            </register > 

    然后在代码中应用container.Resolve<Database>();得到DataBase的实例。

    完整的代码是:

    IUnityContainer container = new UnityContainer();
                var section =
                   (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                section.Configure(container);
                Database database = container.Resolve<Database>();

                

  • 相关阅读:
    Newegg集团招聘软件技术专家[上海]
    Roblox Studio 游戏开发引擎或者叫做平台开发工具
    无线网络:卫星网络
    CMS 文件管理系统:SQL 注入漏洞
    无线网络:无线个域网、无线体域网和无线家居网
    CMS 文件管理系统:XSS 漏洞获取密码和 cookie
    无线网络:无线传感器网络
    无线网络:无线自组织网络
    打卡
    打卡
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2510672.html
Copyright © 2011-2022 走看看