Scrutor icon indicating copy to clipboard operation
Scrutor copied to clipboard

Support co- and contra-variance

Open jbogard opened this issue 8 years ago • 1 comments

Basically support this:

https://github.com/aspnet/DependencyInjection/issues/453

But do it during registration/scanning-time. StructureMap does this when it scans, and I had to add this manually to MediatR's MS DI extensions. See this code I borrowed from StructureMap:

https://github.com/jbogard/MediatR.Extensions.Microsoft.DependencyInjection/blob/master/src/MediatR.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs#L55

This code is pretty complicated inside of StructureMap, so I'd suggest pinging @jeremydmiller for tips on what does what there.

jbogard avatar Oct 24 '16 21:10 jbogard

You have to do the evaluation lazily when you request a closed type for the first time. What StructureMap does is to cache a list of all the "candidate" concrete types -- whether closed or open -- that implement the open interface (IService<T>). At the first request for a specific IService<ISomething>, SM will figure out which concrete types could be cast to the IService<ISomething>.

Massive PITA all the way around.

jeremydmiller avatar Oct 24 '16 22:10 jeremydmiller