Fluent 验证和 IoC(独特字段)

发布于 2024-11-25 05:32:13 字数 1101 浏览 6 评论 0原文

我正在使用 asp.net mvc 3 和 DDD 开发一个 Web 应用程序。对于我的领域模型验证,我一直在使用 Fluent Validation。这是我的第一个具有流畅验证的项目,我仍在学习和建模实体。

我的实体客户有两个属性需要在我的系统中唯一,这些属性是电子邮件和 CPF(它是巴西文档,需要在所有系统中唯一)。我想知道,我该怎么做?

Soo,我的想法是,(通过构造函数)将我的存储库注入到我的 Customer 验证类中,并通过自定义验证来检查它。验证将使用存储库进行检查,如果我的表中是否存在此电子邮件的记录与 ID 不同(0 表示插入,真实 Id 表示更新...我不需要检查我正在更新的记录,因为它' d 始终为真)。

我正在尝试这样的事情:

 public class CustomerValidator : AbstractValidator<Customer> {

     protected ICustomerRepository Repository { get; set; }

     // I intend to inject it by IoC with Unity.. is it possible ?
     public CustomerValidator(ICustomerRepository rep) 
     {
         this.Repository = rep;

         // other properties

         RuleFor(customer = customer.Email)
             .EmailAddress()
             .NotEmpty()
             .Must(email = { return Repository.IsEmailInUse(email, ?); });

         RuleFor(customer = customer.CPF)
             .NotEmpty()
             .Must(cpf = { return Repository.IsCPFInUse(cpf, ?); });

     }   }

我不知道是否可能,在验证器中注入存储库,以及如何在 .Must 方法扩展中获取 Id ?或者还有其他方法可以做到吗?

I'm developing a web application with asp.net mvc 3 and DDD. For my domain model validation, I've been using Fluent Validation. It's my first project with fluent validation and I'm still learning and modeling the entities.

My entity Customer has two properties that need to be unique in my system, these properties are Email and CPF (it's a Brasilian document and need to be unique in all system). I would like to know, how can I to it ?

Soo, my ideia is, inject (by constructor) my repository in my validation class of Customer and check it by a custom validation. The validation would be check using the repository if is there record in my table with this email different of an Id (0 for inserts and real Id for updates... I don't need to check the record I'm updating because it'd always be true).

I`m trying something like this:

 public class CustomerValidator : AbstractValidator<Customer> {

     protected ICustomerRepository Repository { get; set; }

     // I intend to inject it by IoC with Unity.. is it possible ?
     public CustomerValidator(ICustomerRepository rep) 
     {
         this.Repository = rep;

         // other properties

         RuleFor(customer = customer.Email)
             .EmailAddress()
             .NotEmpty()
             .Must(email = { return Repository.IsEmailInUse(email, ?); });

         RuleFor(customer = customer.CPF)
             .NotEmpty()
             .Must(cpf = { return Repository.IsCPFInUse(cpf, ?); });

     }   }

I don't know if is possible, inject a repository inside the validator, and how could I get the Id in the .Must method extension ? Or is there another method to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千仐 2024-12-02 05:32:13
RuleFor(customer => customer.Email)
    .EmailAddress()
    .NotEmpty()
    .Must((customer, email) => Repository.IsEmailInUse(email, customer.Id));

RuleFor(customer => customer.CPF)
    .NotEmpty()
    .Must((customer, cpf) => Repository.IsCPFInUse(cpf, customer.Id));

话虽这么说,当您尝试插入记录并捕获适当的异常时,系统本身(数据库?)也可以更有效地检查唯一性,而不是在验证层中执行此操作。原因是在 FluentValidation 检查唯一性和实际插入记录之间,可能会发生很多事情。

RuleFor(customer => customer.Email)
    .EmailAddress()
    .NotEmpty()
    .Must((customer, email) => Repository.IsEmailInUse(email, customer.Id));

RuleFor(customer => customer.CPF)
    .NotEmpty()
    .Must((customer, cpf) => Repository.IsCPFInUse(cpf, customer.Id));

This being said, checking for uniqueness could also be more efficiently done by the system itself (database?) at the moment you are trying to insert the record and catch the appropriate exception instead of doing this in the validation layer. The reason for this is that between the time your FluentValidation checked for uniqueness and the actual time a record is inserted many things could happen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文