Repository navigation
Closed
Milestone
Description
The following throws an exception (Could not find any registered services for type 'DiTest.IPrinter`2'.):
private static void ConfigureServices(ServiceCollection services)
{
services.Scan(
scan => scan.FromAssemblies(typeof(Program).GetTypeInfo().Assembly)
.AddClasses(x => x.AssignableTo(typeof(IPrinter<,>)))
.AsImplementedInterfaces()
.WithSingletonLifetime()
);
services.Decorate(typeof(IPrinter<,>), typeof(TextColorDecorator<,>));
services.Decorate(typeof(IPrinter<,>), typeof(BackgroundColorDecorator<,>));
}
These classes where used:
public interface IPrinter<T1, T2>
{
void Print(T1 arg1, T2 arg2);
}
public class Printer : IPrinter<string, string>
{
public void Print(string arg1, string arg2)
{
Console.WriteLine("Hi {0}, I am decoratee! {1}", arg1, arg2);
}
}
public class TextColorDecorator<T1, T2> : IPrinter<T1, T2>
{
private readonly IPrinter<T1, T2> decoratee;
public TextColorDecorator(IPrinter<T1, T2> decoratee)
{
this.decoratee = decoratee;
}
public void Print(T1 arg1, T2 arg2)
{
var originalColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
this.decoratee.Print(arg1, arg2);
Console.ForegroundColor = originalColor;
}
}
public class BackgroundColorDecorator<T1, T2> : IPrinter<T1, T2>
{
private readonly IPrinter<T1, T2> decoratee;
public BackgroundColorDecorator(IPrinter<T1, T2> decoratee)
{
this.decoratee = decoratee;
}
public void Print(T1 arg1, T2 arg2)
{
var originalColor = Console.BackgroundColor;
Console.BackgroundColor = ConsoleColor.White;
this.decoratee.Print(arg1, arg2);
Console.BackgroundColor = originalColor;
}
}
Metadata
Metadata
Assignees
Labels
No labels
Activity
yolofy commentedon Oct 28, 2016
Might be useful: http://tmont.com/blargh/2011/3/determining-if-an-open-generic-type-isassignablefrom-a-type
khellang commentedon Oct 28, 2016
Yeah, this is a limitation of Microsoft.Extensions.DependencyInjection. I've tried hard to make this work, but I can't see a way to accomplish this today. (See aspnet/DependencyInjection#450)
The way decoration works, is that we chain factory functions. There's several problems with factory functions and open generics today:
Example
If you have a registration:
When you decorate this type, using
DecoratorService, it roughly translate toThis is not 100% true, since this would blow the stack, but for the sake of simplicity, let's just say that's how it works.
When you resolve
IService, it'll translate toHowever, if you add open generics into this mix, things get a bit more complicated:
Do you see the problem? With open generics, you could resolve
IService<>with any generic argument:In order for us to decorate this, we need to know the generic argument,
string,intandMyClassrespectively, in order to construct theDecoratorService<T>type. This is information that the container clearly has, but it's not making it available anywhere today.khellang commentedon Oct 28, 2016
BTW, scanning works with open generics. We already do something similar to what you linked when you call
AssignableToin your example. This limitation is only for decoration.yolofy commentedon Oct 28, 2016
I don't think the following is production ready but it seems to work (I user the Decorate2 method):
yolofy commentedon Oct 28, 2016
Side note: this code does require the decoratee (Printer) to be closed
khellang commentedon Oct 28, 2016
Hmm. I just ran the following test using the code above:
And it fails with the following message:
I think this is the first limitation I mentioned above.
yolofy commentedon Oct 28, 2016
It will work when you decoratee is closed (e.g.
DecoratedimplementsIDecoratedService<string>)khellang commentedon Apr 28, 2017
This should be closed with #25 and released as part of v2.0.0 ✨