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

Add Validators via Assembly search not working #168

Open
peteharrison opened this issue Aug 17, 2020 · 2 comments
Open

Add Validators via Assembly search not working #168

peteharrison opened this issue Aug 17, 2020 · 2 comments

Comments

@peteharrison
Copy link

Hi Team,

Am I missing something?

I am trying to use serviceCollection.AddValidatorsFromAssemblyContaining() to effectively find all instances of my fluent validation validators and add them to the service collection as transient however it doesn't appear to pick them up.

However, if I manually add the validator to the service collection via serviceCollection.AddTransient<IValidator, LoginCommandValidator>(); it works.

Am I not using this correctly? It doesn't throw any errors just isn't picking up my validators.

@AKomyshan
Copy link
Contributor

AKomyshan commented Aug 17, 2020

Hi,

I checked this behavior.

Make sure that you pass the type from the Assembly that contains inheritors of IValidator<T> (AbstractValidator<TCommand>) as first parameter.

More info: https://github.com/JamesRandall/FunctionMonkey/blob/master/Source/FunctionMonkey.FluentValidation/IServiceCollectionExtensions.cs

@peteharrison
Copy link
Author

peteharrison commented Aug 18, 2020

Yes I had a look at this and I copied what James had done in this article https://dev.to/jamesrandall/elegant-azure-functions-development-in-c-with-function-monkey-1ea7 by calling .AddValidatorsFromAssemblyContaining() and it didn't work?

Can you give me an example?

my validator class looks like:

class LoginCommandValidator : AbstractValidator
{
public LoginCommandValidator()
{
RuleFor(x => x.Credentials).NotNull().NotEmpty();
RuleFor(x => x.Credentials.Identifier).NotEmpty().NotNull();
RuleFor(x => x.Credentials.Password).NotEmpty().NotNull();
RuleFor(x => x.Credentials.AppId).NotEmpty().NotNull();
}
}

and my function app configuration looks like

public class FunctionAppConfiguration : IFunctionAppConfiguration
{
public void Build(IFunctionHostBuilder builder)
{
builder
.Setup((serviceCollection, commandRegistry) =>
{
string SqlConnection = Environment.GetEnvironmentVariable("AuthDBConnectionString");

                serviceCollection.AddDbContext<AuthDBContext>(
                    options => options.UseSqlServer(SqlConnection))
                .AddTransient<IAuthRepository, AuthRepository>()
                .AddValidatorsFromAssemblyContaining<FunctionAppConfiguration>();

                commandRegistry.Discover<FunctionAppConfiguration>();
                
            })
            .DefaultHttpResponseHandler<DefaultResponseHandler>()
            .AddFluentValidation()
            .Functions(functions => functions
                .HttpRoute("v1/Login", route => route
                    .HttpFunction<Login>(HttpMethod.Post)
                )
            );
    }
}

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