Conditionally resolve named implementation in Unity

落爺英雄遲暮 提交于 2019-12-07 02:30:24

问题


Unity allows you to name different implementations of the same interface and then resolve them by name:

var container = new UnityContainer();

// register
container.Register<IFish, OneFish>("One");
container.Register<IFish, TwoFish>("Two");

// resolve
var twoFish = container.Resolve("Two");

Now assume I have a class that depends on IFish and implements ITank:

class Tank : ITank
{
   public Tank(IFish fish) {...}
}

How can I resolve ITank and specify which implementation of IFish to get?

This doesn't work:

container.Register<ITank, Tank>();

var tank = container.Resolve<ITank>("One");

This works:

var fish = container.Resolve<IFish>("One");

var tank = container.Resolve<Tank>(new DependencyOverride(typeof(IFish), fish);

but it only handles simple cases (such as in this example) not the general case where there may be many implementations named "One". What I want is to be able to tell to Unity:

"When resolving use implementations named "One", if no such implementation is register fall back to the unnamed implementation"

Is there a custom resolver that can be plugged into Unity with this behavior?


回答1:


You could you the InjectionFactory of Unity to inject a named instance.

var container = new UnityContainer();

// register
container.Register<IFish, OneFish>("One");
container.Register<IFish, TwoFish>("Two");

container.RegisterType<ITank,Tank>(new InjectionFactory(c=>c.Resolve<IFish>("One")));

If you now resolve an instance of type Tank, an instance of OneFish is injected to your Tank.

To handle your case that you want to inject a default implemenation of IFish you can change InjectionFactory to the following

new InjectionFactory(c=>{
  if (c.IsRegistered<IFish>("One")) {
    c.Resolve<IFish>("One");
  }
  else {
    c.Resolve<IFish>("Two");
  }
})


来源:https://stackoverflow.com/questions/19171135/conditionally-resolve-named-implementation-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!