2

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.