【发布时间】:2019-07-08 02:37:20
【问题描述】:
我一直在构建一个具有流畅验证的 API。我正在研究项目的初始结构。当我向项目中添加更多开发人员时,我需要强制他们为每个模型类实现一个 Fluent Validation 类。
当前验证在存在验证时有效。但是当没有验证时它不会抛出错误。当不存在验证类时,有什么方法可以实现编译错误?就像在查询处理程序类上有一个抽象函数来检查验证一样。
public abstract class CommandHanlder<TCommand> : IRequestHandler<TCommand, ICommandResult> where TCommand : IRequest<ICommandResult>
{
protected IDbContext DbContext { get; }
public TCommand Command { get; private set; }
protected CommandHanlder(IDbContext dbContext)
{
DbContext = dbContext;
}
public async Task<ICommandResult> Handle(TCommand request, CancellationToken cancellationToken)
{
CheckValidation();
Command = request;
ICommandResult commandResult;
commandResult = await Execute();
return commandResult;
}
protected abstract Task<ICommandResult> Execute();
protected bool CheckValidation()
{
// Need to check whether validation present else should throw a compilation error
return true;
}
}
【问题讨论】:
标签: asp.net asp.net-core asp.net-web-api2 abstract-class fluentvalidation