0

Currently, I am working on a .NET Core 6.0 project that uses MediatR, Automapper, and FluentValidation.

I understand the FluentValidation validators are registered by using the "AddValidatorsFromAssembly" method like below

    public static void AddApplicationLayer(this IServiceCollection services)
    {
        services.AddAutoMapper(Assembly.GetExecutingAssembly());
        services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
        services.AddMediatR(Assembly.GetExecutingAssembly());
        services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
    }

However, the project I am working on looks like this

    protected internal override void AddServices(IServiceCollection services)
    {
        services.AddMediatR(typeof(Program));

        services.Scan(s => s
                        .FromAssembliesOf(this.AssemblyMarkerTypes)
                        .AddClasses(false)
                        .UsingRegistrationStrategy(RegistrationStrategy.Append)
                        .AsImplementedInterfaces()
                        .WithTransientLifetime());

        services.AddAutoMapper(typeof(Program));

        services.AddControllers();
        services.AddHealthChecks();
        base.AddServices(services);
    }

It is not clear to me how FluentValidation validators are registered without calling AddValidatorsFromAssembly.

Additionally, it is not clear what the following code does.

        services.Scan(s => s
                        .FromAssembliesOf(this.AssemblyMarkerTypes)
                        .AddClasses(false)
                        .UsingRegistrationStrategy(RegistrationStrategy.Append)
                        .AsImplementedInterfaces()
                        .WithTransientLifetime());

If the above scan, register all the DIs, why do we need a separate registrations for AddAutoMapper & AddMediatR?

2 Answers 2

2

It seems that project is using Scrutor. Based on provided code it will search in assemblies which contain some subset of types defined by this.AssemblyMarkerTypes for all non-abstract types classes (including non-public) and will register them as all implemented interfaces with transient lifetime.

Without access to the source code it is hard to argue what subset of assemblies is covered by AssemblyMarkerTypes but usually it should be all or almost all assemblies which are custom developed for the project, including the one containing validators.

why do we need a separate registrations for AddAutoMapper & AddMediatR

Those methods also register some internal AutoMapper and MediatR stuff. There are 2 reasons why they would not be covered by Scan - first, the types of assemblies for AutoMapper and MediatR were not included in AssemblyMarkerTypes, and the second reason (basically why they were not included) - library developers added those methods so library consumers do not need to bother with what and how needs to be registered for them to function correctly, cause surely there can be stuff which requires different lifetimes and/or is registered as types (i.e. something like services.AddX<SomeConcreteType>()).

Sign up to request clarification or add additional context in comments.

Comments

1

I suspect that ...

this.AssemblyMarkerTypes

... refers to an Enumerable of types from each assembly holding services to be registered. This is passed to Scrutor's Scan extension method to perform the registrations.

It is likely that the Enumerable of types only marks assemblies that under the source control of the owner of the solution you are working on.

AddMediatR and AddAutoMapper are extension methods provided by the respective packages to register and configure all services required for their operation (that are not going to be in the list of assemblies marked by AssemblyMarkerTypes). This is why these calls are still required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.