-1

I'm using Hexagon Architecture

I have 2 driving sides, let's call them A and B (they are two difference assemblies)

The problem is the all the Dependency Injection configuration of the driving A are also applied on the driving B, and I don't want that.

for example:

I disable auto validate FromBody in driving A by using:

public static void AddApiServices(this IServiceCollection services)
{
    services.Configure<ApiBehaviorOptions>(
        options => { options.SuppressModelStateInvalidFilter = true; });
}

but this configuration will also apply on the driving B (I call the extention method in StartUp project)

also with WebApplication the code below also apply to both

public static void UseApiConfiguration(this WebApplication app)
{
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
    }

    app.UseHttpsRedirection();
    
    app.MapControllers();
}

do you have any ideas, please help me, thank you so much

Steven
  • 166,672
  • 24
  • 332
  • 435
vu pham
  • 1
  • 2
  • @Steven, driving A is to serve our react app, driving B is to serve other – vu pham Aug 08 '23 at 16:51
  • even putting both in an assembly does not solve the problem – vu pham Aug 08 '23 at 16:53
  • `I'm using Hexagon Architecture` doesn't explain anything, nor does the `clean-architecture` tag. Especially the latter is just a brand name. It seems like the *real* question is how to disable automatic validation errors for specific containers. That's not related to DI. First of all *why* do you want to disable automatic validation errors? Perhaps what you really want is to customize the response? Or perhaps you're using a DTO with strict rules in a controller that requires laxer ones? In that case, reusing the DTO is wrong. – Panagiotis Kanavos Aug 08 '23 at 17:05
  • I suspect what you really ask is the same as [this SO question - how to disable automatic validation for a specific action](https://stackoverflow.com/questions/63181061/disable-model-validation-for-single-net-core-api-action). The best answer is the one that says you should use different DTOs with different validation rules per action, instead of trying to disable validation rules for fields that are required in `Create` but completely unused in a `Save` or vice versa – Panagiotis Kanavos Aug 08 '23 at 17:11
  • yeah, it's not really related to hexagon architecture, but also not specifically about "how to disable automatic validation" – vu pham Aug 08 '23 at 23:22
  • I mean, I want to disable automatic validation for all controllers in driving A but keep it in driving B. – vu pham Aug 08 '23 at 23:24
  • This is *specifically* about automatic validation. This is provided by the ModelStateInvalidFilter class, not DI. That filter accepts `ApiBehaviorOptions` which just happen to be provided by DI by default. The filter is only added if you add the `ApiController` attribute to a controller class, which means A explicitly asked for this behavior. If you want to change this, you need to either remove `AppiController` or set up a different pipeline, so that `A` controllers use different filters. This *is* possible but *not* related to DI. – Panagiotis Kanavos Aug 09 '23 at 08:20
  • ok, I got it, but the "auto validation" is just an example – vu pham Aug 09 '23 at 10:24

0 Answers0