Resolve one of multiple registrations with DryIoc

女生的网名这么多〃 提交于 2019-12-06 04:45:49

There are number of ways:

First by using service keys as in your comment

Here the consumer selects dependency based on key.

c.Register<MyService2>(made: Made.Of(() => 
    new MyService2(Arg.Of<MyInterface>(ServiceKeys.ImplementationA))));

update: or in constructor agnostic way

c.Register<MyService2>(made:
    Parameters.Of.Type<MyInterface>(ServiceKeys.ImplementationA));

Dependency condition

Dependency selects the consumer based on condition:

c.Register<MyInterface, MyImplementationA>(setup: Setup.With(
    condition: request => request.Parent.ServiceType == typeof(MyService2)));

Dependency is reused in resolution scope of specific consumer

It won't be Transient anymore, but it may be OK depending on your setup.

c.Register<MyService2>(setup: Setup.With(openResolutionScope: true));
c.Register<MyInterface, MyImplementationA>(Reuse.InResolutionScopeOf<MyService2>());

Your registration was ok

c.Register<MyInterface, MyImplementationA>(serviceKey: "implementationA");
c.Register<MyInterface, MyImplementationB>(serviceKey: "implementationB");

If you want to Resolve a concrete Instance for A or B, you can get it easier by this:

var a = c.Resolve<MyInterface>(serviceKey: "implementationA");
var b = c.Resolve<MyInterface>(serviceKey: "implementationB");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!