additonal fluent validation extension method for working with collection


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public class GreaterThanNullableDate<T> : PropertyValidator
  2. {
  3. private Expression<Func<T, DateTime?>> mTarget;
  4. private Expression<Func<T, T>> mTargetWithCollection;
  5. private string mCollectionProperty;
  6. private string mPropertyOfCollection;
  7.  
  8. public GreaterThanNullableDate(Expression<Func<T, DateTime?>> expression)
  9. : base("Property {PropertyName} greater than another date!")
  10. {
  11. mTarget = expression;
  12. }
  13.  
  14. public GreaterThanNullableDate(Expression<Func<T, T>> expression, string pCollectionProperty, string pPropertyOfCollection)
  15. : base("some date is greater than some other date.")
  16. {//for use with a collection and a property of each object in that collection.
  17. //example: RuleFor(p => p.Master.IncidentDateTime).GreaterThanNullableDateList( p => p ,"NIBRSArrests", "ArrestDateTime").WithMessage("Arrest Date time must smaller than incident datetime.");
  18. mTargetWithCollection = expression;
  19. mCollectionProperty = pCollectionProperty;
  20. mPropertyOfCollection = pPropertyOfCollection;
  21. }
  22.  
  23. protected override bool IsValid(PropertyValidatorContext context)
  24. {
  25. if (mTarget != null)
  26. {
  27. Func<T, DateTime?> oFunc = mTarget.Compile();
  28. //Type oType = mTarget.Parameters[0].Type;
  29. DateTime? oTargetDateTime = oFunc.Invoke((T)context.Instance);
  30.  
  31. DateTime? oSource = context.PropertyValue as DateTime?;
  32.  
  33. if (oSource != null && oTargetDateTime != null)
  34. {
  35. if (oSource < oTargetDateTime)
  36. return false;
  37. }
  38. }
  39. else if (mTargetWithCollection != null)
  40. {
  41. Func<T, T> oFunc = mTargetWithCollection.Compile();
  42. //Type oType = mTarget.Parameters[0].Type;
  43. T oTarget = oFunc.Invoke((T)context.Instance);
  44. PropertyInfo oCollectionPropertyInfo = oTarget.GetType().GetProperty(mCollectionProperty);
  45. Object oCollectionValue = oCollectionPropertyInfo.GetValue(oTarget, null);
  46.  
  47.  
  48. DateTime? oSource = context.PropertyValue as DateTime?;
  49.  
  50. foreach (Object oObj in (IEnumerable)oCollectionValue)
  51. {
  52. PropertyInfo oPropertyOfCollectionInfo = oObj.GetType().GetProperty(mPropertyOfCollection);
  53.  
  54. if (oSource != null && oPropertyOfCollectionInfo.GetValue(oObj, null) != null)
  55. {
  56. if (oSource < (DateTime?)oPropertyOfCollectionInfo.GetValue(oObj, null))
  57. return false;
  58. }
  59. }
  60. }
  61.  
  62. return true;
  63. }
  64. }
  65. public static class DefaultValidatorExtensions
  66. {
  67. public static IRuleBuilderOptions<T, DateTime?> GreaterThanNullableDate<T>(this IRuleBuilder<T, DateTime?> ruleBuilder, Expression<Func<T, DateTime?>> expression) //Expression<PropertySelector<T, DateTime?>> expression)//Expression<Func<T, DateTime?>> expression)
  68. {
  69.  
  70. return ruleBuilder.SetValidator(new GreaterThanNullableDate<T>(expression));
  71. }
  72.  
  73. public static IRuleBuilderOptions<T, DateTime?> GreaterThanNullableDateList<T>(this IRuleBuilder<T, DateTime?> ruleBuilder, Expression<Func<T, T>> expression, string pCollectionProperty, string pPropertyOfCollection) //Expression<PropertySelector<T, DateTime?>> expression)//Expression<Func<T, DateTime?>> expression)
  74. {//will get all the objects of a collection and for each object check a property.
  75. return ruleBuilder.SetValidator(new GreaterThanNullableDate<T>(expression, pCollectionProperty, pPropertyOfCollection));
  76. }
  77. }
  78.  
  79.  
  80. use it like this:
  81. RuleFor(p => p.Master.IncidentDateTime).GreaterThanNullableDateList( p => p ,"NIBRSArrests", "ArrestDateTime").WithMessage("Arrest Date time must smaller than incident datetime.");

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.