Taking a simple entity below ...
public class MyEntity
{
[MaxLength(100)]
[Required]
public string Name { get; private set; }
}
... you can read data annotations that adorn the Name property and validate the value specified in the ChangeName method, so ValidationResults can be combined with other validation results. Am I assuming using MethodInfo or PropertyInfo objects in some way?
I have it, but it feels very clumsey.
public ValidationResult ChangeName(string value)
{
var property = GetType().GetProperty("Name");
var attribute = property.GetCustomAttributes(typeof(MaxLengthAttribute), true)[0] as MaxLengthAttribute;
if (attribute == null) return null;
if (value.Length > attribute.Length)
{
return new ValidationResult(NameValidation.NameTooLong);
}
return null;
}
I want to be able to call ChangeName (value) from a method in my validator, for example:
private IEnumerable<ValidationResult> ValidateMyEntity(MyEntityAddCommand command)
{
MyEntity myEntity = new MyEntity();
yield return myEntity.ChangeName(command.Name);
}
, , CRUD, DDD, , , MVC.
, ? .