2

我是 Fluent 验证的新手,需要一些帮助。我有一个接口,它有另一个接口一个属性,并且我已经为这两个接口编写了验证。挑战在于它没有显示接口属性的验证消息。

以下是我的情况的代码

public interface IAddress
{
    string City { get; set; }
    string Town { get; set; }
}

public interface IAccount
{
    string FullName { get; set; }
    int Age { get; set; }
    IAddress Address { get; set; }
}

    public AccountValidator ()
    {
        RuleFor(x => x.FullName).NotEmpty().WithMessage("Full Name  can not  be  empty");
        RuleFor(x => x.Age).GreaterThan(18).WithMessage("Age cannot  be  less  than 18 years");
        RuleFor(x => x.Address).SetValidator( new AddressValidator()); 
    }

    public AddressValidator()
    {
        RuleFor(x => x.City).NotEmpty().NotNull().WithMessage("City can not  be empty");
        RuleFor(x => x.Town).NotEmpty().NotNull().WithMessage("Town can not  be empty"); 
    }

我用来调用验证的客户端代码是:

        var accountValidator = new AccountValidator();

         accountValidator.ValidateAndThrow(_account );

提前致谢。

4

1 回答 1

0

好吧,我尝试了一个迷你控制台应用程序,并且很好地捕捉到了异常(年龄 + 城市)(我只是用 Must(...) 更改了 NotEmpty().NotNull(),因为您需要为每个应用程序放置一个特定的“WithMessage”或他们)。

所以我误解了你的问题吗?

public interface IAddress
    {
        string City { get; set; }
        string Town { get; set; }
    }

    public interface IAccount
    {
        string FullName { get; set; }
        int Age { get; set; }
        IAddress Address { get; set; }
    }

    public class Address : IAddress
    {
        public string City { get; set; }
        public string Town { get; set; }
    }
    public class Account : IAccount
    {
        public string FullName { get; set; }
        public int Age { get; set; }
        public IAddress Address { get; set; }
    }

    public class AccountValidator : AbstractValidator<IAccount>
    {
        public AccountValidator()
        {
            RuleFor(x => x.FullName).NotEmpty().WithMessage("Full Name  can not  be  empty");
            RuleFor(x => x.Age).GreaterThan(18).WithMessage("Age cannot  be  less  than 18 years");
            RuleFor(x => x.Address).SetValidator(new AddressValidator());
        }
    }

    public class AddressValidator : AbstractValidator<IAddress>
    {
        public AddressValidator()
        {
            RuleFor(x => x.City).Must(x => !string.IsNullOrEmpty(x)).WithMessage("City can not  be empty");
            RuleFor(x => x.Town).Must(x => !string.IsNullOrEmpty(x)).WithMessage("Town can not  be empty");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            var account = new Account
                               {
                                   Address = new Address
                                                 {
                                                     City = "",
                                                     Town = "Town"
                                                 },
                                   Age = 18,
                                   FullName = "FullName"
                               };
            var accountValidator = new AccountValidator();
            try
            {
                accountValidator.ValidateAndThrow(account);
            }
            catch (ValidationException e)
            {
                Console.WriteLine(e);
            }
            Console.ReadKey();

        }
    }
于 2012-04-05T09:20:23.900 回答