/// <summary> /// Initializes a new instance of the <see cref="EntityPropertyValidationRule{TEntity, TProperty}"/> class. /// </summary> /// <param name="validator">The validator.</param> /// <param name="propertyDisplayNameResolver">The property display name resolver.</param> /// <param name="propertyExpression">The property expression.</param> /// <param name="specification">The specification.</param> /// <param name="message">The message.</param> public EntityPropertyValidationRule(IEntityPropertyValidator validator, IPropertyDisplayNameResolver propertyDisplayNameResolver, Expression <Func <TEntity, TProperty> > propertyExpression, ISpecification <TEntity> specification = null, string message = null) { if (validator == null) { throw new ArgumentNullException("validator"); } if (propertyDisplayNameResolver == null) { throw new ArgumentNullException("propertyDisplayNameResolver"); } if (propertyExpression == null) { throw new ArgumentNullException("propertyExpression"); } m_Validator = validator; m_PropertyDisplayNameResolver = propertyDisplayNameResolver; m_Specification = specification; m_Message = message; m_PropertyFunc = propertyExpression.Compile(); m_MemberInfo = LinqUtils.GetMemberInfo(propertyExpression); }
/// <summary> /// Adds the property mapping. /// </summary> /// <param name="uiModelPropertyExpression">The source expression.</param> /// <param name="validationModelPropertyExpression">The target expression.</param> protected void AddPropertyMapping(Expression <Func <TUIModel, object> > uiModelPropertyExpression, Expression <Func <TValidationModel, object> > validationModelPropertyExpression) { if (uiModelPropertyExpression == null) { throw new ArgumentNullException("uiModelPropertyExpression"); } if (validationModelPropertyExpression == null) { throw new ArgumentNullException("validationModelPropertyExpression"); } MemberInfo uiModelPropertyInfo = LinqUtils.GetMemberInfo(uiModelPropertyExpression); MemberInfo validationModelPropertyInfo = LinqUtils.GetMemberInfo(validationModelPropertyExpression); string uiModelPropertyName = uiModelPropertyInfo.Name; string validationModelPropertyName = validationModelPropertyInfo.Name; m_PropertyMappingsFromUIModelToValidationModel.Add(uiModelPropertyName, new MappingMemberInfo(validationModelPropertyName, validationModelPropertyInfo)); m_PropertyMappingsFromValidationModelToUIModel.Add(validationModelPropertyName, new MappingMemberInfo(uiModelPropertyName, uiModelPropertyInfo)); }
public void GetDisplayNameShouldReturnMemberNameIfNoDisplayAttributeIsNotUsed() { DefaultPropertyDisplayNameResolver defaultPropertyDisplayNameResolver = new DefaultPropertyDisplayNameResolver(); Assert.AreEqual("Password", defaultPropertyDisplayNameResolver.GetDisplayName(LinqUtils.GetMemberInfo <Customer, string>(x => x.Password))); }
public void GetDisplayNameShouldReturnDisplayNameAttributeNamePropertyValue() { DefaultPropertyDisplayNameResolver defaultPropertyDisplayNameResolver = new DefaultPropertyDisplayNameResolver(); Assert.AreEqual("Email Address", defaultPropertyDisplayNameResolver.GetDisplayName(LinqUtils.GetMemberInfo <Customer, string>(x => x.Email))); }
public override IEntityPropertyValidator CreateValidator() { return(new EqualToEntityPropertyValidator(x => 10, LinqUtils.GetMemberInfo <Model, int>(x => x.Age), typeof(Model))); }
/// <summary> /// Adds an equal to property validator to the rule builder. /// </summary> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <typeparam name="TProperty">The type of the property.</typeparam> /// <param name="ruleBuilder">The rule builder.</param> /// <param name="compareExpression">The compare expression.</param> /// <param name="comparer">The comparer.</param> /// <returns>Rule builder.</returns> /// <exception cref="System.ArgumentNullException"> /// ruleBuilder /// or /// compareExpression /// </exception> public static IEntityValidationRuleBuilder <TEntity, TProperty> EqualTo <TEntity, TProperty>(this IEntityValidationRuleBuilderInitial <TEntity, TProperty> ruleBuilder, Expression <Func <TEntity, TProperty> > compareExpression, IEqualityComparer comparer = null) { if (ruleBuilder == null) { throw new ArgumentNullException("ruleBuilder"); } if (compareExpression == null) { throw new ArgumentNullException("compareExpression"); } Func <TEntity, TProperty> memberToCompareFunc = compareExpression.Compile(); return(ruleBuilder.AddValidator(new EqualToEntityPropertyValidator(x => memberToCompareFunc((TEntity)x), LinqUtils.GetMemberInfo(compareExpression), typeof(TEntity), true, comparer))); }