public void RegisterAdapterFactory(Type attributeType, DataAnnotationsModelValidationFactory factory) { ValidateAttributeType(attributeType); ValidateAttributeFactory(factory); AttributeFactories[attributeType] = factory; }
public static void RegisterAdapterFactory(Type attributeType, DataAnnotationsModelValidationFactory factory) { ValidateAttributeType(attributeType); ValidateFactory(factory); _adaptersLock.EnterWriteLock(); try { AttributeFactories[attributeType] = factory; } finally { _adaptersLock.ExitWriteLock(); } }
public void RegisterDefaultAdapterFactory() { // Arrange var provider = new DataAnnotationsModelValidatorProvider(); var metadata = _metadataProvider.GetMetadataForType(() => null, typeof(MyValidatedClass)); ModelValidator validator = new Mock <ModelValidator>(_noValidatorProviders).Object; DataAnnotationsModelValidationFactory factory = delegate { return(validator); }; provider.RegisterDefaultAdapterFactory(factory); // Act var result = provider.GetValidators(metadata, _noValidatorProviders).Single(); // Assert Assert.Same(validator, result); }
public static void RegisterAdapterFactory(Type attributeType, DataAnnotationsModelValidationFactory factory) { ValidateAttributeType(attributeType); ValidateAttributeFactory(factory); _adaptersLock.EnterWriteLock(); try { AttributeFactories[attributeType] = factory; } finally { _adaptersLock.ExitWriteLock(); } }
public void RegisterAdapterFactory() { // Arrange var provider = new DataAnnotationsModelValidatorProvider(); provider.AttributeFactories = new Dictionary <Type, DataAnnotationsModelValidationFactory>(); DataAnnotationsModelValidationFactory factory = delegate { return(null); }; // Act provider.RegisterAdapterFactory(typeof(MyValidationAttribute), factory); // Assert var type = provider.AttributeFactories.Keys.Single(); Assert.Equal(typeof(MyValidationAttribute), type); Assert.Same(factory, provider.AttributeFactories.Values.Single()); }
public void RegisterAdapterFactoryGuardClauses() { DataAnnotationsModelValidationFactory factory = (metadata, context, attribute) => null; // Attribute type cannot be null Assert.ThrowsArgumentNull( () => DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(null, factory), "attributeType"); // Factory cannot be null Assert.ThrowsArgumentNull( () => DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(MyValidationAttribute), null), "factory"); // Validation attribute must derive from ValidationAttribute Assert.Throws <ArgumentException>( () => DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(object), factory), "The type System.Object must derive from System.ComponentModel.DataAnnotations.ValidationAttribute\r\nParameter name: attributeType"); }
public void RegisterAdapterFactory() { var oldFactories = DataAnnotations4ModelValidatorProvider.AttributeFactories; try { // Arrange DataAnnotations4ModelValidatorProvider.AttributeFactories = new Dictionary <Type, DataAnnotationsModelValidationFactory>(); DataAnnotationsModelValidationFactory factory = delegate { return(null); }; // Act DataAnnotations4ModelValidatorProvider.RegisterAdapterFactory(typeof(MyValidationAttribute), factory); // Assert var type = DataAnnotations4ModelValidatorProvider.AttributeFactories.Keys.Single(); Assert.AreEqual(typeof(MyValidationAttribute), type); Assert.AreSame(factory, DataAnnotations4ModelValidatorProvider.AttributeFactories.Values.Single()); } finally { DataAnnotations4ModelValidatorProvider.AttributeFactories = oldFactories; } }
public void RegisterAdapterFactoryGuardClauses() { var provider = new DataAnnotationsModelValidatorProvider(); DataAnnotationsModelValidationFactory factory = (metadata, validatorProviders, attribute) => null; // Attribute type cannot be null Assert.ThrowsArgumentNull( () => provider.RegisterAdapterFactory(null, factory), "attributeType"); // Factory cannot be null Assert.ThrowsArgumentNull( () => provider.RegisterAdapterFactory(typeof(MyValidationAttribute), null), "factory"); // Validation attribute must derive from ValidationAttribute Assert.ThrowsArgument( () => provider.RegisterAdapterFactory(typeof(object), factory), "attributeType", "The type Object must derive from ValidationAttribute"); }
public void RegisterDefaultAdapterFactory() { var oldFactory = DataAnnotations4ModelValidatorProvider.DefaultAttributeFactory; try { // Arrange var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => null, typeof(MyValidatedClass)); var context = new ControllerContext(); ModelValidator validator = new Mock <ModelValidator>(metadata, context).Object; DataAnnotationsModelValidationFactory factory = delegate { return(validator); }; DataAnnotations4ModelValidatorProvider.RegisterDefaultAdapterFactory(factory); // Act var result = new DataAnnotations4ModelValidatorProvider().GetValidators(metadata, context).Single(); // Assert Assert.AreSame(validator, result); } finally { DataAnnotations4ModelValidatorProvider.DefaultAttributeFactory = oldFactory; } }
private static void AddValidationAttributeAdapter(Dictionary<Type, DataAnnotationsModelValidationFactory> dictionary, Type validataionAttributeType, DataAnnotationsModelValidationFactory factory) { Contract.Assert(dictionary != null); if (validataionAttributeType != null) { dictionary.Add(validataionAttributeType, factory); } }
private static void ValidateFactory(DataAnnotationsModelValidationFactory factory) { if (factory == null) { throw new ArgumentNullException("factory"); } }
public static void RegisterDefaultAdapterFactory(DataAnnotationsModelValidationFactory factory) { ValidateFactory(factory); DefaultAttributeFactory = factory; }
public static void RegisterDefaultAdapter(Type adapterType) { ValidateAdapterType(adapterType); ConstructorInfo constructor = GetAdapterConstructor(typeof(ValidationAttribute), adapterType); DefaultAttributeFactory = (metadata, context, attribute) => (ModelValidator)constructor.Invoke(new object[] { metadata, context, attribute }); }
public static void RegisterDefaultAdapterFactory(DataAnnotationsModelValidationFactory factory) { ValidateFactory(factory); DefaultAttributeFactory = factory; }
static void AddValidationAttributeAdapter(Dictionary <Type, DataAnnotationsModelValidationFactory> dictionary, Type validataionAttributeType, DataAnnotationsModelValidationFactory factory) { Assert.IsNotNull(dictionary); if (validataionAttributeType != null) { dictionary.Add(validataionAttributeType, factory); } }
private static void ValidateAttributeFactory(DataAnnotationsModelValidationFactory factory) { if (factory == null) { throw Error.ArgumentNull("factory"); } }
public static void RegisterAdapterFactory(Type attributeType, DataAnnotationsModelValidationFactory factory) { GuardAttributeType(attributeType); GuardAttributeFactory(factory); _lockObject.EnterWriteLock(); try { AttributeFactories[attributeType] = factory; } finally { _lockObject.ExitWriteLock(); } }
private static void AddValidationAttributeAdapter(Dictionary <Type, DataAnnotationsModelValidationFactory> dictionary, Type validataionAttributeType, DataAnnotationsModelValidationFactory factory) { Contract.Assert(dictionary != null); if (validataionAttributeType != null) { dictionary.Add(validataionAttributeType, factory); } }