NHibernate Validator: Using Attributes vs. Using ValidationDefs

时光怂恿深爱的人放手 提交于 2020-01-31 18:42:47

问题


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

  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?


回答1:


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.



来源:https://stackoverflow.com/questions/5322838/nhibernate-validator-using-attributes-vs-using-validationdefs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!