zoukankan      html  css  js  c++  java
  • Unity: Passing Constructor Parameters to Resolve

    In this tutorial we will go through of couple different ways of using custom constructor parameters when resolving an instance with Unity:

    1. By using the built-in ParameterOverride
    2. By creating a custom ResolverOverride.

    Background

    When you’re using a DI-container like Unity, you normally don’t have to worry about how the container resolves the new instance. You have configured the container and the container will act based on your configuration. But there may be cases where you have pass in custom constructor parameters for the resolve operation. Some may argue that this screams of bad architecture but there’s situations like bringing a DI-container to a legacy system which may require these kind of actions.

    Resolved class

    In this tutorial we are resolving the following test class:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class MyClass
    {
        public string Hello { get; set; }
        public int Number { get; set; }
     
        public MyClass(string hello, int number)
        {
            Hello = hello;
            Number = number;
        }
    }

    It is registered to the container using RegisterType-method and without passing in any parameters:

    1
    2
    var unity = new UnityContainer();
    unity.RegisterType<MyClass>();

    So let’s see how we can pass in the “hello” and “number” variables for the MyClass’ constructor when calling Unity’s Resolve.

    Unity ResolverOverride

    Unity allows us to pass in a “ResolverOverride” when the container’s Resolve-method is called. ResolverOverride is an abstract base class and Unity comes with few of these built-in. One of them isParameterOverride which “lets you override a named parameter passed to a constructor.” 

    So knowing that we need to pass in a string named “hello” and an integer called “number”, we can resolve the instance with the help of ParameterOverride:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [Test]
    public void Test()
    {
        var unity = new UnityContainer();
        unity.RegisterType<MyClass>();
     
        var myObj = unity.Resolve<MyClass>(new ResolverOverride[]
                                       {
                                           new ParameterOverride("hello", "hi there"), new ParameterOverride("number", 21)
                                       });
     
        Assert.That(myObj.Hello, Is.EqualTo("hi there"));
        Assert.That(myObj.Number, Is.EqualTo(21));
    }

    We pass in two instances of ParameterOverride. Both of these take in the name and the value of the parameter.

    Custom ResolverOverride: OrderedParametersOverride

    But what if you don’t like passing in the parameter names and instead you want to pass in just the parameter values, in correct order? In order to achieve this we can create a custom ResolverOverride. Here’s one way to do it:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    public class OrderedParametersOverride : ResolverOverride
    {
        private readonly Queue<InjectionParameterValue> parameterValues;
     
        public OrderedParametersOverride(IEnumerable<object> parameterValues)
        {
            this.parameterValues = new Queue<InjectionParameterValue>();
            foreach (var parameterValue in parameterValues)
            {
                this.parameterValues.Enqueue(InjectionParameterValue.ToParameter(parameterValue));
            }
        }
     
        public override IDependencyResolverPolicy GetResolver(IBuilderContext context, Type dependencyType)
        {
            if (parameterValues.Count < 1)
                return null;
     
            var value = this.parameterValues.Dequeue();
            return value.GetResolverPolicy(dependencyType);
        }
    }

    The parameter values are passed  in through the constructor and put into a queue. When the container is resolving an instance, the parameters are used in the order which they were given to the OrderedParametersOverride.

    Here’s a sample usage of the new OrderedParametersOverride:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [Test]
    public void TestOrderedParametersOverride()
    {
        var unity = new UnityContainer();
        unity.RegisterType<MyClass>();
     
        var myObj = unity.Resolve<MyClass>(new OrderedParametersOverride(new object[] {"greetings", 24 }));
     
        Assert.That(myObj.Hello, Is.EqualTo("greetings"));
        Assert.That(myObj.Number, Is.EqualTo(24));
    }

    Sample code

    The above examples can be found from GitHub.

  • 相关阅读:
    黑鲨2无限重启 把竞技按钮调到最上
    绿联 电池
    阿里云
    Centos 8 搭建时钟服务器
    CentOS8系统时间同步解决方法
    解决问题的人干活快的人
    【海通国际】Joe Lowry(Mr. Lithium)谈全球电池原材料供应危机
    Linux 实验楼
    用 set follow-fork-mode child即可。这是一个 gdb 命令,其目的是告诉 gdb 在目标应用调用fork之后接着调试子进程而不是父进程,因为在 Linux 中fork系统调用成功会返回两次,一次在父进程,一次在子进程
    【随笔】阿里云修改DNS
  • 原文地址:https://www.cnblogs.com/itelite/p/4104664.html
Copyright © 2011-2022 走看看