gpt4 book ai didi

swagger - MicroElements.Swashbuckle.FluentValidation AddFluentValidationRules 使用命令处理程序模式

转载 作者:行者123 更新时间:2023-12-02 00:48:25 29 4
gpt4 key购买 nike

尝试使用来自 https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/microservice-application-layer-implementation-web-api 的命令处理程序模式使 MicroElements.Swashbuckle.FluentValidation 工作

使用 ASP .Net Core 2.2
MicroElements.Swashbuckle.FluentValidation v3.0.0-alpha.1(作为程序集而不是包引用)
Swashbuckle.AspNetCore 5.0.0-rc2

我有这是 Startup.cs

return services.AddSwaggerGen(setup =>
{
setup.AddFluentValidationRules();
});

使用流畅的验证

这不会将 Fluent 验证提取到请求正文对象的架构中。
    public class AddModelsCommandValidator : AbstractValidator<AddModelsCommand>
{
public AddModelsCommandValidator()
{
//1. validate request
RuleFor(e => e.Model).InvalidRequestValidation();

When(x => x.Model != null, () =>
{
//2. validate request body
RuleFor(e => e.Model.ModelCode).StringRequiredValidation();
RuleFor(e => e.Model.ModelCode).StringMaxLengthValidation(5);
RuleFor(e => e.Model.ProgramName).StringRequiredValidation();
RuleFor(e => e.Model.ProgramName).StringMaxLengthValidation(50);
});
}
}

public class AddModelsCommand : IRequest<AddModelsCommandResult>
{
public Model Model { get; }

public AddModelsCommand(Model model)
{
Model = model;
}
}

public class Model
{
/// <summary>
/// Unique code of the Model
/// </summary>
public string ModelCode { get; set; }

/// <summary>
/// The name of the Program
/// </summary>
public string ProgramName { get; set; }
}


以下代码确实将 Fluent 验证提取到请求正文对象的架构中。 (因为 1. AbstractValidator 在模型上而不是命令上,并且 2. 我已经删除了条件 When() 验证)
public class AddModelsCommandValidator : AbstractValidator<Model>
{
public AddModelsCommandValidator()
{

//2. validate request body
RuleFor(e => e.ModelCode).StringRequiredValidation();
RuleFor(e => e.ModelCode).StringMaxLengthValidation(5);
RuleFor(e => e.ProgramName).StringRequiredValidation();
RuleFor(e => e.ProgramName).StringMaxLengthValidation(50);
}
}

有没有办法调用 AddFluentValidationRules 并使用命令处理程序模式?

最佳答案

这里的问题是 AbstractValidator<Model>必须用..AbstractValidator<Command > 真的没有意义

关于swagger - MicroElements.Swashbuckle.FluentValidation AddFluentValidationRules 使用命令处理程序模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59622414/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com