-
Notifications
You must be signed in to change notification settings - Fork 320
Support constrained open generic types #471
Comments
That's pretty unfortunate. What's the scenarios for this? |
I'm adding a collection of filters for my own in-memory pipes-and-filters implementation in MediatR. My real interface looks like:
These behaviors act like filters around a core
If I made the behavior closed instead of constrained, it conflicts with rules around "favor closed over open" (which is consistent with at least StructureMap). So if I keep it an open generic type, but merely constrain it, then I can get the correct I assume some F# person is going to read all this and laugh one day. |
See the StructureMap implementation: |
We are not planning to include this feature here, in part because it doesn't seem to be broadly available in other DI systems and demand doesn't seem very high. We're also concerned about the first chance exception that gets thrown. |
@Eilon Is there any way to override some defaults so that we could get the expected behavior mentioned by @jbogard?
NOTE: Same use can be solved using many other DI containers such as Autofac, DryIoC, LightInject, StructureMap, Unity and Castle Windsor. See: MediatR Container-Feature-Support, Test source code |
@t03apt sorry I'm not sure if that's possible. |
I'm trying to implement something similar with constrained generics. Would be super helpful to have this built into the DI system. @jbogard have you had any luck with a work around for this? |
No, I had to pick one of the other containers. We were trying to get away with just using the built-in container but it's just not possible today. |
In case anyone is wondering, the following containers support this feature:
The following do not:
The latter two I thought were dead but seem to be undergoing a revival of sorts. |
… close because of a generic constraint. See aspnet#471 for discussion.
If I register a collection of open generics, some of the open generics may be constrained. Consider the interface and implementations:
If I register multiple services:
And try to resolve, the version that adheres to all constraints works, but if I try to get a collection with a generic parameter that doesn't match the constraint, I get an exception:
Exception:
Expected: constrained open generics are tested before attempted to instantiate. It looks like the only way to reliably test is to try-catch-swallow, see http://stackoverflow.com/a/4864565/58508
PR with failing test coming shortly.
The text was updated successfully, but these errors were encountered: