示例#1
0
        private static void LoadGlobalPropertiesFromAppConfig()
        {
            object config = ConfigurationManager.GetSection(CfgXmlHelper.CfgSectionName);

            if (config == null)
            {
                log.Info(string.Format("{0} section not found in application configuration file", CfgXmlHelper.CfgSectionName));
                return;
            }

            var nhConfig = config as IHibernateConfiguration;

            if (nhConfig == null)
            {
                log.Info(
                    string.Format(
                        "{0} section handler, in application configuration file, is not IHibernateConfiguration, section ignored",
                        CfgXmlHelper.CfgSectionName));
                return;
            }

            GlobalProperties[PropertyBytecodeProvider]       = CfgXmlHelper.ByteCodeProviderToString(nhConfig.ByteCodeProviderType);
            GlobalProperties[PropertyUseReflectionOptimizer] = nhConfig.UseReflectionOptimizer.ToString();
            if (nhConfig.SessionFactory != null)
            {
                foreach (KeyValuePair <string, string> kvp in nhConfig.SessionFactory.Properties)
                {
                    GlobalProperties[kvp.Key] = kvp.Value;
                }
            }
        }
 public void ValidatorModeTest()
 {
     Assert.AreEqual(ValidatorMode.UseAttribute, CfgXmlHelper.ValidatorModeConvertFrom("UseAttribute"));
     Assert.AreEqual(ValidatorMode.UseExternal, CfgXmlHelper.ValidatorModeConvertFrom("useExternal"));
     Assert.AreEqual(ValidatorMode.OverrideExternalWithAttribute, CfgXmlHelper.ValidatorModeConvertFrom("OVERRIDEExternalWithAttribute"));
     Assert.AreEqual(ValidatorMode.OverrideAttributeWithExternal, CfgXmlHelper.ValidatorModeConvertFrom("OverrideAttributeWithExternal"));
     Assert.AreEqual(ValidatorMode.UseAttribute, CfgXmlHelper.ValidatorModeConvertFrom(null));
     Assert.AreEqual(ValidatorMode.UseAttribute, CfgXmlHelper.ValidatorModeConvertFrom(string.Empty));
 }
示例#3
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);
        }
 public void InvalidValidatorMode()
 {
     Assert.That(() => CfgXmlHelper.ValidatorModeConvertFrom("Not supported"), Throws.TypeOf <ValidatorConfigurationException>());
 }
 public void InvalidValidatorMode()
 {
     ActionAssert.Throws <ValidatorConfigurationException>(() => CfgXmlHelper.ValidatorModeConvertFrom("Not supported"));
 }