Created
November 2, 2009 21:13
-
-
Save jfromaniello/224465 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PersonValidationDef : ValidationDef<AsientoContableLinea> | |
{ | |
public PersonValidationDef() | |
{ | |
//Si bien puedo hacer esto, no tengo forma de separar | |
//la validacion IsMale. (bueno podría creando un validador) | |
ValidateInstance.By(p => p.Father.IsMale) | |
.WithMessage("Father must be male."); | |
//esta es la forma que yo utilice: | |
this.DefineFor(p => p.Father) | |
.Satisfy(p => p.IsMale) | |
.WithMessage("Father must be male."); | |
//de esta manera puedo meter en un extension method, desde el satisfy para adelante: | |
this.DefineFor(p => p.Father) | |
.MustBeMale(); | |
//incluso es "chaineable": | |
this.DefineFor(p => p.Father) | |
.MustBeMale() | |
.And.GreatherThan(21); | |
//y se puede MAL-usar sobre cualquier tipo (se que no se debería hacer pero..): | |
this.DefineFor(p => p.Name) | |
.Satisfy(name => name.Length > 5 && name.Length < 25); | |
//muy desaconsejado. | |
} | |
} | |
//Este es el código que me hizo falta: | |
public static class ExtensionValidationDefinitions | |
{ | |
public static EntityValidation<TPropertyType> DefineFor<TEntity, TPropertyType>( | |
this ValidationDef<TEntity> validationDef, | |
Expression<Func<TEntity, TPropertyType>> property | |
) where TEntity : class | |
{ | |
var memberInfo = TypeUtils.DecodeMemberAccessExpression(property); | |
return new EntityValidation<TPropertyType>(validationDef, memberInfo); | |
} | |
} | |
public class EntityValidation<TEntity> : BaseConstraints<EntityValidation<TEntity>> | |
{ | |
public EntityValidation(IConstraintAggregator parent, MemberInfo member) | |
: base(parent, member) | |
{ | |
} | |
public IChainableConstraint<EntityValidation<TEntity>> Satisfy( | |
Func<TEntity, IConstraintValidatorContext, bool> isValidDelegate) | |
{ | |
var attribute = new DelegatedValidatorAttribute( | |
new DelegatedConstraint<TEntity>(isValidDelegate)); | |
return AddWithConstraintsChain(attribute); | |
} | |
public IChainableConstraint<EntityValidation<TEntity>> | |
Satisfy(Func<TEntity, bool> isValidDelegate) | |
{ | |
var attribute = new DelegatedValidatorAttribute( | |
new DelegatedSimpleConstraint<TEntity>(isValidDelegate)); | |
return AddWithConstraintsChain(attribute); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment