FluentValidation 从 IRuleBuilderOptions 获取属性名称
FluentValidations Get Propertyname from RuleBuilderOptions
我正在尝试创建自定义 When Extensions 以检查我的实体是否有更改。但是我在获取验证实例时需要的 Propertyname 时遇到了问题。
public static bool WhenHasChanged<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule)
{
//I need to get the PropertyValidatorContext from the rule
PropertyValidatorContext context;
var instance = (IChangeTrackingObject)context.Instance;
if (false == instance.GetChangedProperties().ContainsKey(context.PropertyName))
{
return true;
}
var oldValue = instance.GetChangedProperties().Get(context.PropertyName).OldValue;
var newValue = context.PropertyValue;
return (null == oldValue) ? null == newValue : oldValue.Equals(newValue);
}
我需要获取正在验证的 PropertyName 和正在验证的实例,通常这些位于 PropertyValidatorContext
内,有没有办法从规则中获取 PropertyValidatorContext
?
我最终创建了一个必须的扩展,所以我可以访问 属性 验证器上下文:
private static Func<T, TProperty, PropertyValidatorContext, bool> MustWhenChangedPredicate<T, TProperty>(Func<T, TProperty, PropertyValidatorContext, bool> predicate)
{
return (t, p, context) =>
{
var instance = (IChangeTrackingObject)context.Instance;
//The type name always prefixes the property
var propertyName = context.PropertyName.Split(new[] { '.' }, 2).Skip(1).First();
if (false == instance.GetChangedProperties().ContainsKey(propertyName))
{
return true;
}
var oldValue = instance.GetChangedProperties().Get(propertyName).OldValue;
var newValue = context.PropertyValue;
if (oldValue == null && newValue == null)
{
return true;
}
if ((oldValue != null && oldValue.Equals(newValue)) ||
(newValue != null && newValue.Equals(oldValue)))
{
return true;
}
return predicate(t, p, context);
};
}
我正在尝试创建自定义 When Extensions 以检查我的实体是否有更改。但是我在获取验证实例时需要的 Propertyname 时遇到了问题。
public static bool WhenHasChanged<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule)
{
//I need to get the PropertyValidatorContext from the rule
PropertyValidatorContext context;
var instance = (IChangeTrackingObject)context.Instance;
if (false == instance.GetChangedProperties().ContainsKey(context.PropertyName))
{
return true;
}
var oldValue = instance.GetChangedProperties().Get(context.PropertyName).OldValue;
var newValue = context.PropertyValue;
return (null == oldValue) ? null == newValue : oldValue.Equals(newValue);
}
我需要获取正在验证的 PropertyName 和正在验证的实例,通常这些位于 PropertyValidatorContext
内,有没有办法从规则中获取 PropertyValidatorContext
?
我最终创建了一个必须的扩展,所以我可以访问 属性 验证器上下文:
private static Func<T, TProperty, PropertyValidatorContext, bool> MustWhenChangedPredicate<T, TProperty>(Func<T, TProperty, PropertyValidatorContext, bool> predicate)
{
return (t, p, context) =>
{
var instance = (IChangeTrackingObject)context.Instance;
//The type name always prefixes the property
var propertyName = context.PropertyName.Split(new[] { '.' }, 2).Skip(1).First();
if (false == instance.GetChangedProperties().ContainsKey(propertyName))
{
return true;
}
var oldValue = instance.GetChangedProperties().Get(propertyName).OldValue;
var newValue = context.PropertyValue;
if (oldValue == null && newValue == null)
{
return true;
}
if ((oldValue != null && oldValue.Equals(newValue)) ||
(newValue != null && newValue.Equals(oldValue)))
{
return true;
}
return predicate(t, p, context);
};
}