private static void Main(string[] args) { XmlConfigurator.Configure(); var log = LogManager.GetLogger(typeof (Program)); SetupNHibernateValidator(); var nhibernateConfig = new Configuration().Configure(); nhibernateConfig.Initialize(); ISessionFactory sessionFactory = nhibernateConfig.BuildSessionFactory(); var schemaExport = new SchemaExport(nhibernateConfig); schemaExport.Execute(false, true, false); var junk = new Product { Name = "Spiffy Junk", Description = string.Empty, UnitPrice = -1M }; var ve = Environment.SharedEngineProvider.GetEngine(); var invalidValues = ve.Validate(junk); foreach (var invalidValue in invalidValues) log.InfoFormat("{0} {1}", invalidValue.PropertyName, invalidValue.Message); }
protected override void Configure(NHibernate.Cfg.Configuration configuration) { base.Configure(configuration); var nhvc = new FluentConfiguration(); nhvc.SetDefaultValidatorMode(ValidatorMode.UseAttribute).IntegrateWithNHibernate.ApplyingDDLConstraints().And. RegisteringListeners(); var onlyToUseToInitializeNh_Engine = new ValidatorEngine(); onlyToUseToInitializeNh_Engine.Configure(nhvc); configuration.Initialize(onlyToUseToInitializeNh_Engine); }
private void InjectValidationAndFieldLengths(ref Configuration configuration) { var fluentConfiguration = new FluentConfiguration(); RegisterValidationDefinitions(fluentConfiguration); fluentConfiguration.SetDefaultValidatorMode(ValidatorMode.UseExternal). IntegrateWithNHibernate.ApplyingDDLConstraints(). And.RegisteringListeners(); ValidatorEngine.Configure(fluentConfiguration); configuration.Initialize(ValidatorEngine); }
static void Main(string[] args) { XmlConfigurator.Configure(); var log = LogManager.GetLogger(typeof(Program)); SetupNHibernateValidator(); var cfg = new Configuration().Configure(); cfg.Initialize(); var sessionFactory = cfg.BuildSessionFactory(); var schemaExport = new SchemaExport(cfg); schemaExport.Execute(true, true, false); var junk = new Product { Name = "Spiffy Junk", Description = "Stuff we can't sell.", UnitPrice = -1M }; using (var session = sessionFactory.OpenSession()) { using (var tx = session.BeginTransaction()) { try { session.Save(junk); tx.Commit(); } catch (InvalidStateException validationException) { var errors = validationException.GetInvalidValues(); foreach (var error in errors) log.ErrorFormat("Error with property {0}: {1}", error.PropertyName, error.Message); tx.Rollback(); } } } }
private static ISessionFactory CreateSessionFactory(IKernel kernel) { var validatorEngine = kernel.Resolve<ValidatorEngine>(); Assert.NotNull(typeof(System.Data.SQLite.SQLiteConnection)); _nhConfig = new NHibernate.Cfg.Configuration() .SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, typeof(ConversationSessionContext).AssemblyQualifiedName) .SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName) .SetProperty(Environment.Dialect, typeof(SQLiteDialect).AssemblyQualifiedName) .SetProperty(Environment.ConnectionString, "Data Source=:memory:") .SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName) .SetProperty(Environment.ReleaseConnections, "on_close") .SetProperty(Environment.UseSecondLevelCache, "true") .SetProperty(Environment.UseQueryCache, "true") .SetProperty(Environment.CacheProvider, typeof(HashtableCacheProvider).AssemblyQualifiedName) .AddAssembly(typeof(User).Assembly); Security.Configure<User>(_nhConfig, SecurityTableStructure.Prefix); _nhConfig.Initialize(validatorEngine); return _nhConfig.BuildSessionFactory(); }
/// <summary> /// Configura o validados do nhibernate para validar as classes do dominio /// </summary> /// <param name="nHibernateConfiguration"></param> /// <returns></returns> private static ValidatorEngine ConfigureValidator(Configuration nHibernateConfiguration) { var configure = new NHibernate.Validator.Cfg.Loquacious.FluentConfiguration(); configure.Register( Assembly.GetExecutingAssembly().GetTypes() .Where(t => t.Namespace != null && t.Namespace.Equals("Progas.Portal.Factory.Entity")) .ValidationDefinitions()) .SetDefaultValidatorMode(ValidatorMode.UseAttribute) .IntegrateWithNHibernate.ApplyingDDLConstraints().And.RegisteringListeners(); var ve = new ValidatorEngine(); ve.Configure(configure); nHibernateConfiguration.Initialize(ve); return ve; }
private void injectValidationAndFieldLengths(Configuration nhConfig) { if (string.IsNullOrWhiteSpace(ValidationDefinitionsNamespace)) return; MappingsValidatorEngine = new ValidatorEngine(); var configuration = new FluentConfiguration(); var validationDefinitions = MappingsAssembly.GetTypes() .Where(t => t.Namespace == ValidationDefinitionsNamespace) .ValidationDefinitions(); configuration .Register(validationDefinitions) .SetDefaultValidatorMode(ValidatorMode.OverrideExternalWithAttribute) .IntegrateWithNHibernate .ApplyingDDLConstraints() .And .RegisteringListeners(); MappingsValidatorEngine.Configure(configuration); //Registering of Listeners and DDL-applying here nhConfig.Initialize(MappingsValidatorEngine); }