6

I have the following objects 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 make 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 am 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 back individual registrations lines above, Resolve works for IAction and ICommand but not for IParser.

It seems that RegisterMany is not registering parent types properly...

Edit2:

I changed my registration to following, 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 type of the first registered mapping, in this case ICommand

I am using DryIOC v2.6.2

1 Answers1

3

Here is working and explaining 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>();

The exception in DryIoc supposed to be guiding you in this case.

Update:

In case you want to include non-public service types for RegisterMany use:

container.RegisterMany<Action1>(nonPublicServiceTypes: true);                 
container.RegisterMany<Command1>(nonPublicServiceTypes: true);
dadhi
  • 4,807
  • 19
  • 25
  • Thank you for your help, unfortunately I did as as you instructed but it didn't work, I don't know for what reason but I think something is wrong with my code. I will isolate these components in a clean project and check if it works or not. btw I am using the SRC version not DLL version from NuGet. Thanks again! – IdontCareAboutReputationPoints Jun 22 '16 at 11:01
  • What exceptions did it raise? Src should not be different from dll package functionality-wise. – dadhi Jun 22 '16 at 12:29
  • Unable to resolve `App.Interfaces.IAction.` Where no service registrations found and number of Rules.FallbackContainers: 0 and number of Rules.UnknownServiceResolvers: 0 – IdontCareAboutReputationPoints Jun 22 '16 at 12:39
  • Strange, what ais visiblity of IAction interface? Is it public? – dadhi Jun 22 '16 at 13:04
  • 1
    Also you may enable non public service types in RegisterMany via optional parameter nonPublicServiceTypes: true – dadhi Jun 22 '16 at 13:08
  • Yes, that was it, the interfaces were not public, setting `nonPublicServiceTypes: true` fixed the issue. Thank you very much, and sorry for causing any trouble. can you please add this solution to your answer above, I will mark it as accepted answer. – IdontCareAboutReputationPoints Jun 22 '16 at 13:27
  • 1
    Updated the answer. – dadhi Jun 22 '16 at 13:42