我开始使用Unity做越来越多的工作。我注意到Resolver方法采用 params 参数 ResolverOverride 。
有人可以给我一个例子,说明如何使用 ResolverOverride 或者指出一些我可以获得更多线索的其他来源。
答案 0 :(得分:106)
正如您已经注意到这是Unity 2的一个新的(非常酷的功能)。此功能让您
有ParameterOverride : ResolverOverride
一个可以让你的ResolverOverride类 覆盖传递给a的命名参数 构造
DependencyOverride : ResolverOverride
覆盖该值的类 每当有一个注入时注入 给定类型的依赖关系, 无论它出现在哪里 对象图。
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 override which appears only in unity 2.0) classes will have 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; }
}
}
不知道为什么Google对此保持沉默。
引文来自Unity源代码xml docs。
答案 1 :(得分:45)
如果有人感兴趣,我已经制作了一个扩展方法,使得使用ParameterOverride
解析的语法更容易阅读。方法如下:
public static class UnityExtensions
{
public static T Resolve<T>(this IUnityContainer container, object parameterOverrides)
{
var properties = parameterOverrides
.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var overridesArray = properties
.Select(p => new ParameterOverride(p.Name, p.GetValue(parameterOverrides, null)))
.Cast<ResolverOverride>()
.ToArray();
return container.Resolve<T>(null, overridesArray); //null needed to avoid a StackOverflow :)
}
}
这样,您可以按如下方式重写ParameterOverride示例:
var service = container.Resolve<IConcreteService>(new {val=3});
我希望这对某人有用......
答案 2 :(得分:14)
添加我的2c。你可以像这样添加一个ParameterOverrides:
Container.Resolve<IConcreteService>(new ParameterOverrides
{
{"val", 42}
});