【发布时间】:2023-03-09 10:30:01
【问题描述】:
在我正在工作的 MVC 项目中,我使用 Fluent Validation 来实现验证逻辑和统一作为依赖注入容器
在我的验证器类中有一些复杂的业务验证规则
public class ServicerRequestViewModelValidator : AbstractValidator<ServiceRequestViewModel>
{
public ServiceRequestViewModelValidator(ILocalizationService localizationService)
{
RuleFor(x => x.IdStato).NotNull().WithMessage(string.Format(localizationService.GetMessage("Validation.General.MandataryField"), localizationService.GetMessage("ServiceRequestDetail.State")));
// other business validations rule with localized error message
}
}
规则设置根据用户语言本地化的错误消息
杰里米斯金纳说:
验证器的实例化是一个昂贵的过程,因为 RuleFor 中的表达式树编译和解析 定义。因此,建议您使用验证器 实例作为单例 - 一旦实例化它们应该被缓存并 重用,而不是被多次实例化。
验证器不包含任何共享状态,因此它也应该是安全的 也可以在多线程场景中重用它们。最好的方法 缓存验证器实例将使用 IoC 容器(例如, StructureMap) 来管理实例生命周期。
所以我用 ContainerControlledLifetimeManager (singleton) 在容器中注册了验证器
container.RegisterType<IValidator<ServiceRequestViewModel>, ServiceRequestViewModelValidator>(new ContainerControlledLifetimeManager());
但是这样做会出现问题:
第一次解析ServiceRequestViewModelValidator 时,构造函数被执行,本地化的错误消息将根据用户的语言进行缓存,后续用户将根据实例化单例类的用户的语言获取本地化的消息。
【问题讨论】:
标签: c# asp.net asp.net-mvc validation fluentvalidation