This article will show you how to combine type based validation rules with other validations, how to compose them and apply them in different scenarios, adding/removing validations when applied on concrete instances, combining objects of different types and implementing one validation strategy on all.
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComposableCore;
using System.Reflection;
namespace ComposableServices
{
/// <summary>
/// Validation extensions which
/// </summary>
public static class PropertyExt
{
/// <summary>
/// Wraps a source of T in a Stateful object of T combined with a list of propertyInfo's.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Source"></param>
/// <returns></returns>
public static Stateful<T, List<PropertyInfo>> toPropInfoStateFul<T>(this T Source)
{
return Source.ToStateful(new List<PropertyInfo>());
}
/// <summary>
/// Wraps a function for getting a source of T in a Stateful object of T combined with a list of propertyInfo's into a fType object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Source"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static fType<Stateful<T, List<PropertyInfo>>, Stateful<T, List<PropertyInfo>>> fPropertyInfos<T>(this
Stateful<T, List<PropertyInfo>> Source, T obj)
{
return fPropertyInfos(Source, obj, new List<string>());
}
/// <summary>
/// Wraps a function for getting a source of T in a Stateful object of T combined with a list of propertyInfo's, but without the excluded listed properties, into a fType object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Source"></param>
/// <param name="obj"></param>
/// <param name="Excludes"></param>
/// <returns></returns>
public static fType<Stateful<T, List<PropertyInfo>>, Stateful<T, List<PropertyInfo>>> fPropertyInfos<T>(this
Stateful<T, List<PropertyInfo>> Source, T obj, List<string> Excludes)
{
fType<Stateful<T, List<PropertyInfo>>, Stateful<T, List<PropertyInfo>>> retvalue = null;
if (Excludes == null) { Excludes = new List<string>(); }
Func<List<string>, string, bool> fExclude = ((e, i) => { return !e.Contains(i); });
System.Reflection.PropertyInfo[] props = obj.GetType().GetProperties();
Func<PropertyInfo, Stateful<T, List<PropertyInfo>>,
fType<Stateful<T, List<PropertyInfo>>,
Stateful<T, List<PropertyInfo>>>> flinq =
((x, y) =>
{
var fret = fType.toFtype<Stateful<T, List<PropertyInfo>>, Stateful<T, List<PropertyInfo>>>(z =>
{
z.ThenOnState(zz =>
{
zz.Add(x);
return zz;
});
return z;
});
if (retvalue == null)
{
retvalue = fret;
}
else
{
retvalue = retvalue.Then(fret);
}
return retvalue;
});
var prps = (from prp in props
where (fExclude(Excludes, prp.Name))
select flinq(prp, Source)).ToList();
return retvalue;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.