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

Decorate derived interface #156

Open
YuriiNskyi opened this issue Nov 10, 2021 · 2 comments
Open

Decorate derived interface #156

YuriiNskyi opened this issue Nov 10, 2021 · 2 comments

Comments

@YuriiNskyi
Copy link

YuriiNskyi commented Nov 10, 2021

Suppose, I have this simplified model with the specified registration:

interface IExecutor<TRequest, TResponse>
{
    TResponse Execute(TRequest request);
}

interface ISpecifiedExecutor : IExecutor<SpecificRequest, SpecificResponse>
{
}

class SpecifiedExecutor : ISpecifiedExecutor
{
   ...
}

services
    .Scan(x => x
        .FromApplicationDependencies()
        .AddClasses(classes => classes.AssignableTo(typeof(IExecutor<,>)))
        .AsSelfWithInterfaces()
        .WithTransientLifetime()
    );

Works like a charm, everything is fine.

Things tend to get complicated, when I'm trying to decorate my executors:

services.Decorate(typeof(IExecutor<,>), typeof(LoggingDecorator<,>));

// returns decorated with LoggingDecorator
provider.GetRequiredService<IExecutor<SpecificRequest, SpecificResponse>>()

// returns NOT decorated with LoggingDecorator
provider.GetRequiredService<ISpecifiedExecutor>()

// if trying to decorate ISpecifiedExecutor
// throws an exception, when I'm trying to resolve ISpecifiedExecutor
// A suitable constructor for type 
// 'LoggingDecorator`2[TRequest,TResponse]' could not be located.
// Ensure the type is concrete and services are registered for all parameters of a public constructor.
services.Decorate(typeof(ISpecifiedExecutor), typeof(LoggingDecorator<,>));

Is there any way to decorate ISpecifiedExecutor with LoggingDecorator<,>?

@YuriiNskyi
Copy link
Author

As far as I understood, if this test pass, it is fully satisfy my requirement, am I right?

@khellang
Copy link
Owner

Maybe. You omitted the constructor in question, so it's hard to say...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants