Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve using parent hierarchy interfaces using DryIOC

Tags:

c#

dryioc

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

like image 748
IdontCareAboutReputationPoints Avatar asked Jun 21 '16 12:06

IdontCareAboutReputationPoints


1 Answers

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);
like image 185
dadhi Avatar answered Nov 01 '22 12:11

dadhi