How to enable parent hierarchy interfaces using DryIOC

I have the following object structure

public interface IParser {} public interface IAction : IParser {} public interface ICommand : IParser {} //impl public class Action1 : IAction {} public class Command1 : ICommand {} //registration container.Register<IAction, Action1>(); container.Register<ICommand, Command1>(); //resolve var parsersList = container.Resolve<IList<IParser>>() //expected: parsersList.Count=2 actual count=0 

Is there any way to do some kind of binding between these parent and child interfaces in DryIOC?

EDIT:

I did some research and found that RegisterMany did the trick, but I'm a bit confused because

 //registration //container.Register<IAction, Action1>(); //if I drop these two lines \_____ //container.Register<ICommand, Command1>(); / | container.RegisterMany<Action1>(); // and use these lines | container.RegisterMany<Command1>(); | //resolve | var parsersList = container.Resolve<IList<IParser>>() //WORKS Count = 2 | container.Resolve<IAction>() //not working Unable to resolve IAction <---| container.Resolve<ICommand>() //Same here for ICommand <---| container.Resolve<IParser>() //not working either 

If I uncomment the individual log lines above, Resolve works for IAction and ICommand, but not for IParser.

RegisterMany seems to not register parent types correctly ...

Edit2:

I changed my registration to the next using RegisterMapping

 container.Register<IAction, Action1>(); container.Register<ICommand, Command1>(); container.RegisterMapping<IParsable, ICommand>(); //1st registered mapping container.RegisterMapping<IParsable, IAction>(); container.Resolve<IList<IParser>>().Count = 1 //instead of 2. 

The item in the IParsers list is of the type of the first registered match, in this case ICommand

I am using DryIOC v2.6.2

+6
source share
1 answer

Here we work and explain the sample on .NET Fiddle .

The code:

  var container = new Container(); container.RegisterMany<Action1>(); container.RegisterMany<Command1>(); var parsersList = container.Resolve<IList<IParser>>(); // works fine container.Resolve<IAction>(); // works fine container.Resolve<ICommand>(); // Throws because IParser is registered multiple times (as Action and as Command), // and container is unable to select one. This is precisely what exception is saying. //container.Resolve<IParser>(); 

An exception in DryIoc should be sent to you in this case.

Update:

If you want to enable non-public service types for RegisterMany , use:

 container.RegisterMany<Action1>(nonPublicServiceTypes: true); container.RegisterMany<Command1>(nonPublicServiceTypes: true); 
+2
source

All Articles