Optional Decorators?
See original GitHub issueHello!
I have the following setup. This code is very similiar to my earlier question: https://github.com/khellang/Scrutor/issues/68
services.Scan(s =>
{
s.FromAssembliesOf(typeof(ApplicationLayerServiceProviderExtensions))
.AddClasses(p => p.AssignableTo(typeof(ICommandHandler<>)).Where(x => !x.IsGenericType))
.AsClosedTypeOf(typeof(ICommandHandler<>))
.WithTransientLifetime();
s.FromAssembliesOf(typeof(ApplicationLayerServiceProviderExtensions))
.AddClasses(p => p.AssignableTo(typeof(AbstractValidator<>)).Where(x => !x.IsGenericType))
.AsClosedTypeOf(typeof(IValidator<>))
.WithTransientLifetime();
});
services.Decorate(typeof(ICommandHandler<>), typeof(ValidationCommandHandlerDecorator<>));
services.Decorate(typeof(ICommandHandler<>), typeof(LoggingCommandHandlerDecorator<>));
The AsClosedTypeOf()
code is this:
public static ILifetimeSelector AsClosedTypeOf(this IServiceTypeSelector selector, Type closedType)
{
return selector.As(t => t
.GetInterfaces()
.Where(p => p.IsGenericType && p.GetGenericTypeDefinition() == closedType)
);
}
my ValidationCommandHandlerDecorator<>
looks like this:
public class ValidationCommandHandlerDecorator<TCommand>
: ICommandHandler<TCommand> where TCommand : ICommand
{
private readonly ICommandHandler<TCommand> _innerHandler;
private readonly IValidator<TCommand> _validator;
public ValidationCommandHandlerDecorator(
ICommandHandler<TCommand> innerHandler,
IValidator<TCommand> validator)
{
_innerHandler = innerHandler;
_validator = validator;
}
public Task HandleAsync(TCommand command)
{
var validationResult = _validator.Validate(command);
// Other code is omitted
return _innerHandler.HandleAsync(command);
}
}
Right now, I need to create an implementation of IValidator<>
for every command that gets executed. If it does not exist, an exception is thrown because the implementation can’t be found.
I would like to support that Ivalidator<>
is optional. Sometimes I have a command that I need no validator for. Right now I HAVE to create an implementation of IValidator<>
that is just an empty class to satisfy this code.
Could you help me out?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
python - How to build a decorator with optional parameters?
If it is passed arguments, then it is first called with them and then the result of that preliminary call (which must itself...
Read more >How To Write A Decorator With An Optional Argument?
When playing with decorators (this week's challenge) I got stuck: how do you write a decorator that takes an optional argument?
Read more >@Self or @Optional @Host? The visual guide to Angular ...
@Host() decorator makes Angular to look for the injector on the component itself, so in that regard it may look similar to the...
Read more >Self, @SkipSelf & @Optional Decorators Angular
Self, @SkipSelf, @Optional are Angular Decorators configure how the DI resolve the dependencies. They modify the behavior of injectors.
Read more >Fully-typed Python decorator with optional arguments
This is my very first blog post! In this short post, I will show the blueprint for a fully-typed Python decorator that optionally...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I am now using your last suggestion (IValidator<TCommand> validator = null) and that works! thanks!
Thank you very much, I will try this out!