本文介绍了NHibernate Validator:使用属性与使用ValidationDefs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用NH Validator已有一段时间了,主要是通过ValidationDef s,但是我仍然不确定两件事:

I've been using NH Validator for some time, mostly through ValidationDefs, but I'm still not sure about two things:

  1. 使用ValidationDef进行简单/标准验证(例如NotNullMaxLength等)有什么特别的好处吗?
  2. 我担心这两种方法在验证时会引发不同种类的异常,例如:
    • ValidationDefDefine.NotNullable()引发PropertyValueException
    • 使用[NotNull]属性时,会抛出InvalidStateException.
  1. Is there any special benefit of using ValidationDef for simple/standard validations (like NotNull, MaxLength etc)?
  2. I'm worried about the fact that those two methods throw different kinds of exceptions on validation, for example:
    • ValidationDef's Define.NotNullable() throws PropertyValueException
    • When using [NotNull] attribute, an InvalidStateException is thrown.

这使我认为混合这两种方法不是一个好主意-一致地处理验证异常将非常困难.有什么建议/建议吗?

This makes me think mixing these two approaches isn't a good idea - it will be very difficult to handle validation exceptions consistently. Any suggestions/recommendations?

推荐答案

ValidationDef 可能更适合业务规则验证,即使如此,我甚至将其用于简单验证. 此处.
我喜欢 ValidationDef 的地方是它具有流畅的界面.我已经使用此引擎一段时间了,我整理了一些对我来说很好的引擎.
我已经定义了一个接口:

ValidationDef is probably more suitable for business-rules validation even if, having said that, I used it even for simple validation. There's more here.
What I like about ValidationDef is the fact that it has got a fluent interface.I've been playing around with this engine for quite a while and I've put together something that works quite well for me.
I've defined an interface:

public interface IValidationEngine
{
    bool IsValid(Entity entity);
    IList<Validation.IBrokenRule> Validate(Entity entity);
}

在我的验证引擎中实现了哪些

Which is implemented in my validation engine:

public class ValidationEngine : Validation.IValidationEngine
{
    private NHibernate.Validator.Engine.ValidatorEngine _Validator;

    public ValidationEngine()
    {
        var vtor = new NHibernate.Validator.Engine.ValidatorEngine();
        var configuration = new FluentConfiguration();
        configuration
            .SetDefaultValidatorMode(ValidatorMode.UseExternal)
            .Register<Data.NH.Validation.User, Domain.User>()
            .Register<Data.NH.Validation.Company, Domain.Company>()
            .Register<Data.NH.Validation.PlanType, Domain.PlanType>();
        vtor.Configure(configuration);
        this._Validator = vtor;
    }

    public bool IsValid(DomainModel.Entity entity)
    {
        return (this._Validator.IsValid(entity));
    }

    public IList<Validation.IBrokenRule> Validate(DomainModel.Entity entity)
    {
        var Values = new List<Validation.IBrokenRule>();
        NHibernate.Validator.Engine.InvalidValue[] values = this._Validator.Validate(entity);
        if (values.Length > 0)
        {
            foreach (var value in values)
            {
                Values.Add(
                    new Validation.BrokenRule()
                    {
                        // Entity = value.Entity as BpReminders.Data.DomainModel.Entity,
                        // EntityType = value.EntityType,
                        EntityTypeName = value.EntityType.Name,
                        Message = value.Message,
                        PropertyName = value.PropertyName,
                        PropertyPath = value.PropertyPath,
                        // RootEntity = value.RootEntity as DomainModel.Entity,
                        Value = value.Value
                    });
            }
        }
        return (Values);
    }
}

我将所有域规则插入其中.
我在应用启动时引导引擎:

I plug all my domain rules in there.
I bootstrap the engine at the app startup:

For<Validation.IValidationEngine>()
    .Singleton()
    .Use<Validation.ValidationEngine>();

现在,当我需要在保存之前验证我的实体时,我只需使用引擎即可:

Now, when I need to validate my entities before save, I just use the engine:

if (!this._ValidationEngine.IsValid(User))
{
    BrokenRules = this._ValidationEngine.Validate(User);
}

并最终返回违反规则的集合.

and return, eventually, the collection of broken rules.

这篇关于NHibernate Validator:使用属性与使用ValidationDefs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 15:11