Skip to content

Custom Convenience Attributes #283

@szv

Description

@szv

It would be cool, if we could create convenience/shortcut-attributes, like in the following example:

using FluentValidation;

namespace Application.Common.Attributes;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class RegisterValidatorAttribute<T> : RegisterScopedAttribute
    where T : class
{
    public RegisterValidatorAttribute()
    {
        ServiceType = typeof(IValidator<T>);
    }
}

for a validator class like this

[RegisterValidator<UpdatePostingCommand>]
internal class MyTypeToValidateValidator : AbstractValidator<MyTypeToValidate>
{
    // ...
}

In the case of FluentValidation, the validator class inherits AbstractValidator. When annotating the validator class with Injectio's [RegisterScoped] attribute, the validator gets registered for the type AbstractValidator<MyTypeToValidate>.
Currently it is possible to register the validator class by annotating it with [RegisterScoped(ServiceType=typeof(IValidator<MyTypeToValidate>)] or by adding the IValidator<MyTypeToValidate> explicitly to the MyTypeToValidate class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions