Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolving interfaces with covariant type parameter fails when using RegisterMapping #418

Closed
t-castelan opened this issue Jul 29, 2021 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@t-castelan
Copy link
Contributor

t-castelan commented Jul 29, 2021

We built some extension methods around RegisterMapping to register multiple types with a single call. So we are using it quite frequently. Now we ran into a problem that we cannot resolve an interface type with a covariant type parameter, when registering it by RegisterMapping. See the following example:

var container = new Container();
// Register the implementation as itself
container.Register(Made.Of(() => new List<SubClass>()));
// Register the interface with base type parameter
container.RegisterMapping<IReadOnlyList<BaseClass>, List<SubClass>>();

// Resolving IReadOnlyList<BaseClass> crashes here
var baseClassList = container.Resolve<IReadOnlyList<BaseClass>>();

The following is the message of the thrown exception:

DryIoc.ContainerException : code: Error.RegisteringImplementationNotAssignableToServiceType;
message: Registering implementation type List<SubClass> is not assignable to service type IReadOnlyList<BaseClass>.

I will provide a failing test and some working tests containing alternatives/workarounds that are sufficient for us, now. But it would be nice, if this issue got fixed.

t-castelan added a commit to t-castelan/DryIoc that referenced this issue Jul 29, 2021
When using RegisterMapping to register a covariant interface type, resolving it
may fail.

Issue dadhi#418
@dadhi
Copy link
Owner

dadhi commented Jul 29, 2021

@t-castelan Thanks for reporting and especially for the test. Will look.

@dadhi dadhi added the bug Something isn't working label Jul 29, 2021
dadhi pushed a commit that referenced this issue Jul 29, 2021
When using RegisterMapping to register a covariant interface type, resolving it
may fail.

Issue #418
@dadhi dadhi self-assigned this Jul 29, 2021
@dadhi dadhi added this to the v4.8.2 milestone Jul 29, 2021
@dadhi
Copy link
Owner

dadhi commented Oct 9, 2021

@t-castelan Forgot about this thingy, will look next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants