示例#1
0
 public ValidatorEngine()
 {
     constraintValidatorFactory = Environment.ConstraintValidatorFactory ?? new DefaultConstraintValidatorFactory();
     entityTypeInspector        = new DefaultEntityTypeInspector();
     factory = new StateFullClassValidatorFactory(constraintValidatorFactory, null, null, null, ValidatorMode.UseAttribute,
                                                  entityTypeInspector);
 }
示例#2
0
        /// <summary>
        /// Configure NHibernate.Validator using the specified <see cref="INHVConfiguration"/>.
        /// </summary>
        /// <param name="config">The <see cref="INHVConfiguration"/> that is the configuration reader to configure NHibernate.Validator.</param>
        /// <param name="mappingLoader">The <see cref="XmlMappingLoader"/> instance.</param>
        /// <remarks>
        /// Calling Configure(INHVConfiguration) will overwrite the values set in app.config or web.config
        /// </remarks>
        public virtual void Configure(INHVConfiguration config, IMappingLoader mappingLoader)
        {
            if (config == null)
            {
                throw new ValidatorConfigurationException("Could not configure NHibernate.Validator.",
                                                          new ArgumentNullException("config"));
            }

            Clear();

            applyToDDL            = PropertiesHelper.GetBoolean(Environment.ApplyToDDL, config.Properties, true);
            autoRegisterListeners = PropertiesHelper.GetBoolean(Environment.AutoregisterListeners, config.Properties, true);
            defaultMode           =
                CfgXmlHelper.ValidatorModeConvertFrom(PropertiesHelper.GetString(Environment.ValidatorMode, config.Properties,
                                                                                 string.Empty));
            interpolator =
                GetImplementation <IMessageInterpolator>(
                    PropertiesHelper.GetString(Environment.MessageInterpolatorClass, config.Properties, string.Empty),
                    "message interpolator");

            if (Environment.ConstraintValidatorFactory == null)
            {
                constraintValidatorFactory = GetImplementation <IConstraintValidatorFactory>(
                    PropertiesHelper.GetString(Environment.ConstraintValidatorFactoryClass,
                                               config.Properties,
                                               string.Empty),
                    "Constraint Validator Factory") ?? new DefaultConstraintValidatorFactory();
            }
            else
            {
                constraintValidatorFactory = Environment.ConstraintValidatorFactory;
            }

            var inspectorsTypes = new HashSet <System.Type>(config.EntityTypeInspectors)
            {
                typeof(DefaultEntityTypeInspector)
            };

            if (inspectorsTypes.Count > 1)
            {
                var inspectors = config.EntityTypeInspectors.Select(typeInspector => Instatiate <IEntityTypeInspector>(typeInspector)).ToArray();
                entityTypeInspector = new MultiEntityTypeInspector(inspectors);
            }
            else
            {
                entityTypeInspector = new DefaultEntityTypeInspector();
            }

            ResourceManager customResourceManager         = null;
            var             customResourceManagerBaseName = PropertiesHelper.GetString(Environment.CustomResourceManager, config.Properties,
                                                                                       null);

            if (!string.IsNullOrEmpty(customResourceManagerBaseName))
            {
                var resourceAndAssembly = TypeNameParser.Parse(customResourceManagerBaseName);
                try
                {
                    var assembly = Assembly.Load(resourceAndAssembly.Assembly);
                    customResourceManager = new ResourceManager(resourceAndAssembly.Type, assembly);
                }
                catch (Exception e)
                {
                    throw new ValidatorConfigurationException("Could not configure NHibernate.Validator (custom resource manager).", e);
                }
            }

            factory = new StateFullClassValidatorFactory(constraintValidatorFactory, customResourceManager, null, interpolator, defaultMode,
                                                         entityTypeInspector);

            // UpLoad Mappings
            if (mappingLoader == null)
            {
                // Configured or Default loader (XmlMappingLoader)
                mappingLoader = GetImplementation <IMappingLoader>(
                    PropertiesHelper.GetString(Environment.MappingLoaderClass, config.Properties, string.Empty),
                    "mapping loader") ?? new XmlMappingLoader();
            }
            mappingLoader.LoadMappings(config.Mappings);
            Initialize(mappingLoader);
        }