Advertisement
Ruslan_F

Untitled

Jul 20th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using ChewedMole.DataContracts.Constraints;
  6. using ChewedMole.StoreService.DataContracts.Constraints;
  7.  
  8. namespace ChewedMole
  9. {
  10.     public class StoreConstraintFactory
  11.     {
  12.         public static Dictionary<StoreClassTypes, BaseConstraint> Constraints = new Dictionary<StoreClassTypes, BaseConstraint>();
  13.  
  14.         static StoreConstraintFactory()
  15.         {
  16.             var knownTypeAttributes = typeof(StoreClassConstraint).GetCustomAttributes(typeof(KnownTypeAttribute), false).OfType<KnownTypeAttribute>();
  17.             foreach (var knownTypeAttribute in knownTypeAttributes)
  18.             {
  19.                 var constriant = (StoreClassConstraint)Activator.CreateInstance(knownTypeAttribute.Type);
  20.                 Constraints.Add(constriant.ClassType,constriant);
  21.             }
  22.         }
  23.         public static BaseConstraint CreateForType(Type type)
  24.         {
  25.             StoreClassTypes classType;
  26.             if (Enum.TryParse(type.Name, out classType))
  27.             {
  28.                 return Constraints[classType];
  29.             }
  30.             throw new NotSupportedException($"Type {type.Name} does not suport validation");
  31.         }
  32.     }
  33.  
  34.     public class StoreClassValidator<TObject>
  35.     {
  36.        
  37.         public static bool IsValid(TObject obj)
  38.         {
  39.             var constraint = StoreConstraintFactory.CreateForType(typeof(TObject));
  40.             return constraint.PropertyConstraints.All(p => IsValidProperty(obj, p.Key, p.Value));
  41.         }
  42.  
  43.        
  44.  
  45.         private static bool IsValidProperty(TObject obj, string propertyPath, List<BasePropertyConstraint> constraints)
  46.         {
  47.             throw new System.NotImplementedException();
  48.         }
  49.     }
  50.  
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement