Exemplo n.º 1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            IEntityWithTypedId <int> entityToValidate = validationContext.ObjectInstance as IEntityWithTypedId <int>;

            if (entityToValidate == null)
            {
                throw new InvalidOperationException(
                          "This validator must be used at the class level of an IEntityWithTypedId<int>. " +
                          "The type you provided was " + validationContext.ObjectInstance.GetType());
            }

            IEntityDuplicateChecker duplicateChecker = DependencyResolver.Current.GetService <IEntityDuplicateChecker>();

            if (duplicateChecker == null)
            {
                throw new TypeLoadException("IEntityDuplicateChecker has not been registered with IoC");
            }

            if (duplicateChecker.DoesDuplicateExistWithTypedIdOf(entityToValidate))
            {
                return(new ValidationResult(String.Empty));
            }

            return(null);
        }
        public bool IsValid(object value, IConstraintValidatorContext constraintValidatorContext)
        {
            IEntityWithTypedId <int> entityToValidate = value as IEntityWithTypedId <int>;

            Check.Require(entityToValidate != null,
                          "This validator must be used at the class level of an " +
                          "IdomainWithTypedId<int>. The type you provided was " + value.GetType().ToString());

            IEntityDuplicateChecker duplicateChecker = SafeServiceLocator <IEntityDuplicateChecker> .GetService();

            return(!duplicateChecker.DoesDuplicateExistWithTypedIdOf <int>(entityToValidate));
        }
        public bool IsValid(object value, IConstraintValidatorContext constraintValidatorContext)
        {
            IEntityWithTypedId <string> entityToValidate = value as IEntityWithTypedId <string>;

            Check.Require(entityToValidate != null,
                          "This validator must be used at the class level of an " +
                          "IdomainWithTypedId<string>. The type you provided was " + value.GetType().ToString() + ". " +
                          "Other validators exist for various Id types. Please open an issue with S#arp Architecture " +
                          "if you need a new Id type supported; you can make your own in the meantime.");

            IEntityDuplicateChecker duplicateChecker = SafeServiceLocator <IEntityDuplicateChecker> .GetService();

            return(!duplicateChecker.DoesDuplicateExistWithTypedIdOf <string>(entityToValidate));
        }