Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using ChewedMole.DataContracts.Constraints;
- using ChewedMole.StoreService.DataContracts.Constraints;
- namespace ChewedMole
- {
- public class StoreConstraintFactory
- {
- public static Dictionary<StoreClassTypes, BaseConstraint> Constraints = new Dictionary<StoreClassTypes, BaseConstraint>();
- static StoreConstraintFactory()
- {
- var knownTypeAttributes = typeof(StoreClassConstraint).GetCustomAttributes(typeof(KnownTypeAttribute), false).OfType<KnownTypeAttribute>();
- foreach (var knownTypeAttribute in knownTypeAttributes)
- {
- var constriant = (StoreClassConstraint)Activator.CreateInstance(knownTypeAttribute.Type);
- Constraints.Add(constriant.ClassType,constriant);
- }
- }
- public static BaseConstraint CreateForType(Type type)
- {
- StoreClassTypes classType;
- if (Enum.TryParse(type.Name, out classType))
- {
- return Constraints[classType];
- }
- throw new NotSupportedException($"Type {type.Name} does not suport validation");
- }
- }
- public class StoreClassValidator<TObject>
- {
- public static bool IsValid(TObject obj)
- {
- var constraint = StoreConstraintFactory.CreateForType(typeof(TObject));
- return constraint.PropertyConstraints.All(p => IsValidProperty(obj, p.Key, p.Value));
- }
- private static bool IsValidProperty(TObject obj, string propertyPath, List<BasePropertyConstraint> constraints)
- {
- throw new System.NotImplementedException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement