本文介绍了统一2.0:如何使用解决与ResolverOverride?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始做与统一越来越多的工作。我注意到解析方法需要的 PARAMS 的参数的 ResolverOverride 的。

I started doing more and more work with Unity. I notice that Resolver method takes a params argument ResolverOverride.

有人可以给我一个例子,说明我可以使用的 ResolverOverride 的或点我在哪里可以得到更多的线索其他来源。

Can someone give me an example how I can use ResolverOverride or point me some other source where I can get more clues.

推荐答案

正如您已经注意到,这是一个新的(真的很酷的功能)此功能让你

As you have noticed this is a new (and realy cool feature) of the Unity 2. This feature let you

  • 参数传递给在当下构造函数时您解决这个类。在统一1,你可以在那一刻,当你通过注册新InjectionConstructor类型只设定一个值(...)

ParameterOverride:ResolverOverride

一个ResolverOverride类,可以让你  覆盖传递给命名参数  构造函数。

  • 在相同的依赖关系 DependencyOverride:ResolverOverride

这将覆盖值A级  注入每当有  依赖给定类型的,  不论在何地出现在中  对象图。

  • 在相同的性能与 PropertyOverride:ResolverOverride

一个ResolverOverride,让你重写值的指定属性。

示例

using System;
using Microsoft.Practices.Unity;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {

            var container = new UnityContainer();

            //ParameterOverride example

            container.RegisterType<IConcreteService, ConcreteService>(
                new InjectionConstructor(7) //Old way to pass value to constructor - not flexible. 
                                            //All resolved (without ovverride wich aapears only in unity 2.0) classes will hav val=7
                );

            var service0 = container.Resolve<IConcreteService>();
            Console.WriteLine(service0.Val); //prints 7

            var service = container.Resolve<IConcreteService>(new ParameterOverride("val", 3));
            Console.WriteLine(service.Val); // prints 3

            var service2 = container.Resolve<IConcreteService>(new ParameterOverride("val", 5));
            Console.WriteLine(service2.Val); // prints 5

            Console.ReadLine();

            //DependencyOverride example

            var b0 = container.Resolve<B>(new DependencyOverride<IConcreteService>(new ConcreteService(42)));
            Console.WriteLine(b0.Service.Val); //print 42
            Console.WriteLine(b0.Service1.Val); //print 42

            var b = container.Resolve<B>(new DependencyOverride<IConcreteService>(new ConcreteService(-42)));
            Console.WriteLine(b.Service.Val); // print -42
            Console.WriteLine(b.Service1.Val); // print -42

            Console.ReadLine();

            //PropertyOverride example 

            var b1 = container.Resolve<B>(new PropertyOverride("Service", new ConcreteService(42)), 
                new PropertyOverride("Service1", new ConcreteService(-42)));
            Console.WriteLine(b1.Service.Val); //print 42
            Console.WriteLine(b1.Service1.Val); //print -42

            Console.ReadLine();



        }
    }

    public interface IConcreteService {
        int Val { get; set; }
    }
    public class ConcreteService : IConcreteService {

        public int Val { get; set; }

        public ConcreteService(int val) {
            Val = val;
        }
    }

    public class B {
        [Dependency]
        public IConcreteService Service{ get; set; }

        [Dependency]
        public IConcreteService Service1 { get; set; }

    }
}

不知道为什么谷歌一直对沉默。

Have no idea why does google keeps silence about that.

行情是从Unity源$ C ​​$ C XML文档。

Quotes are from Unity source code xml docs.

这篇关于统一2.0:如何使用解决与ResolverOverride?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 18:53