public void SetUp() { ConnectionString = $@"Data Source={Path.GetTempFileName()};New=True;"; var configuration = new global::NHibernate.Cfg.Configuration() .AddProperties(new Dictionary <string, string> { { "dialect", dialect }, { global::NHibernate.Cfg.Environment.ConnectionString, ConnectionString } }); var metaModel = new SagaMetadataCollection(); metaModel.Initialize(new[] { typeof(T) }); var metadata = metaModel.Find(typeof(T)); var modelMapper = new SagaModelMapper(metaModel, new[] { metadata.SagaEntityType }); configuration.AddMapping(modelMapper.Compile()); SessionFactory = configuration.BuildSessionFactory(); new SchemaUpdate(configuration).Execute(false, true); SagaPersister = new SagaPersister(); }
/// <summary> /// Builds the session factory with the given properties. Database is updated if updateSchema is set /// </summary> public ISessionFactory Build(Configuration nhibernateConfiguration) { var scannedAssemblies = typesToScan.Select(t => t.Assembly).Distinct(); foreach (var assembly in scannedAssemblies) { nhibernateConfiguration.AddAssembly(assembly); } var modelMapper = new SagaModelMapper(typesToScan.Except(nhibernateConfiguration.ClassMappings.Select(x => x.MappedClass))); nhibernateConfiguration.AddMapping(modelMapper.Compile()); try { return nhibernateConfiguration.BuildSessionFactory(); } catch (Exception e) { if (e.InnerException != null) throw new ConfigurationErrorsException(e.InnerException.Message, e); throw; } }
/// <summary> /// Builds the session factory with the given properties. Database is updated if updateSchema is set /// </summary> public ISessionFactory Build(Configuration nhibernateConfiguration) { var scannedAssemblies = typesToScan.Select(t => t.Assembly).Distinct(); foreach (var assembly in scannedAssemblies) { nhibernateConfiguration.AddAssembly(assembly); } var modelMapper = new SagaModelMapper(typesToScan.Except(nhibernateConfiguration.ClassMappings.Select(x => x.MappedClass))); nhibernateConfiguration.AddMapping(modelMapper.Compile()); try { return(nhibernateConfiguration.BuildSessionFactory()); } catch (Exception e) { if (e.InnerException != null) { throw new ConfigurationErrorsException(e.InnerException.Message, e); } throw; } }
public void SetUp() { ConnectionString = $"Data Source={Path.GetTempFileName()};New=True;"; var configuration = new Configuration() .AddProperties(new Dictionary <string, string> { { "dialect", dialect }, { Environment.ConnectionString, ConnectionString } }); var metaModel = new SagaMetadataCollection(); metaModel.Initialize(SagaTypes); var sagaDataTypes = new List <Type>(); using (var enumerator = metaModel.GetEnumerator()) { while (enumerator.MoveNext()) { sagaDataTypes.Add(enumerator.Current.SagaEntityType); } } sagaDataTypes.Add(typeof(ContainSagaData)); SagaModelMapper.AddMappings(configuration, metaModel, sagaDataTypes); SessionFactory = configuration.BuildSessionFactory(); new SchemaUpdate(configuration).Execute(true, true); SagaPersister = new SagaPersister(); }
public async Task SetUp() { var cfg = new Configuration() .DataBaseIntegration(x => { x.Dialect <MsSql2012Dialect>(); x.ConnectionString = Consts.SqlConnectionString; }); var metaModel = new SagaMetadataCollection(); metaModel.Initialize(SagaTypes); var sagaDataTypes = new List <Type>(); using (var enumerator = metaModel.GetEnumerator()) { while (enumerator.MoveNext()) { sagaDataTypes.Add(enumerator.Current.SagaEntityType); } } sagaDataTypes.Add(typeof(ContainSagaData)); SagaModelMapper.AddMappings(cfg, metaModel, sagaDataTypes); SessionFactory = cfg.BuildSessionFactory(); schema = new SchemaExport(cfg); await schema.CreateAsync(false, true); SagaPersister = new SagaPersister(); }
/// <summary> /// Builds the session factory with the given properties. Database is updated if updateSchema is set /// </summary> /// <param name="nhibernateProperties"></param> /// <param name="updateSchema"></param> /// <returns></returns> public ISessionFactory Build(IDictionary<string, string> nhibernateProperties, bool updateSchema) { var scannedAssemblies = typesToScan.Select(t => t.Assembly).Distinct(); var nhibernateConfiguration = new Configuration().SetProperties(nhibernateProperties); foreach (var assembly in scannedAssemblies) nhibernateConfiguration.AddAssembly(assembly); var mapping = new SagaModelMapper(typesToScan.Except(nhibernateConfiguration.ClassMappings.Select(x => x.MappedClass))); HackIdIntoMapping(mapping); nhibernateConfiguration.AddMapping(mapping.Compile()); ApplyDefaultsTo(nhibernateConfiguration); if (updateSchema) UpdateDatabaseSchemaUsing(nhibernateConfiguration); try { return nhibernateConfiguration.BuildSessionFactory(); } catch (Exception e) { if (e.InnerException != null) throw new ConfigurationErrorsException(e.InnerException.Message, e); throw; } }
/// <summary> /// Generates the table creation script for the saga data table /// </summary> /// <param name="tableNamingConvention">Optional custom table naming convention.</param> /// <typeparam name="TSaga">Saga type.</typeparam> public static string GenerateSagaScript <TSaga>(Func <Type, string> tableNamingConvention = null) where TSaga : Saga { var sagaBase = typeof(TSaga).BaseType; var sagaDataType = sagaBase.GetGenericArguments()[0]; var metadata = new SagaMetadataCollection(); metadata.Initialize(new[] { typeof(TSaga) }); var typesToScan = new List <Type> { sagaDataType }; var sagaDataBase = sagaDataType.BaseType; while (sagaDataBase != null) { typesToScan.Add(sagaDataBase); sagaDataBase = sagaDataBase.BaseType; } var config = new Configuration(); config.DataBaseIntegration(db => { db.Dialect <T>(); }); SagaModelMapper.AddMappings(config, metadata, typesToScan, tableNamingConvention); return(GenerateScript(config)); }
private static void HackIdIntoMapping(SagaModelMapper hbmMapping) { var hbmIdField = typeof(global::NHibernate.Mapping.ByCode.Impl.IdMapper).GetField("hbmId", BindingFlags.Instance | BindingFlags.NonPublic); hbmMapping.Mapper.AfterMapClass += (mi, t, map) => { map.Id(idmap => { var hbmId = (global::NHibernate.Cfg.MappingSchema.HbmId)hbmIdField.GetValue(idmap); hbmId.type1 = typeof(GuidToPartitionKeyAndRowKey).AssemblyQualifiedName; hbmId.type = null; hbmId.column1 = null; hbmId.column = new[] { new global::NHibernate.Cfg.MappingSchema.HbmColumn { name = "RowKey" }, new global::NHibernate.Cfg.MappingSchema.HbmColumn { name = "PartitionKey" }, }; }); }; hbmMapping.Mapper.AfterMapManyToOne += (mi, type, map) => MapIdColumns(map, type.LocalMember); hbmMapping.Mapper.AfterMapBag += (mi, type, map) => map.Key(km => MapIdColumns(km, type.LocalMember)); hbmMapping.Mapper.AfterMapJoinedSubclass += (mi, type, map) => map.Key(km => MapIdColumns(km, type.BaseType)); }
public void SetUp() { connectionString = String.Format(@"Data Source={0};New=True;", Path.GetTempFileName()); var configuration = new global::NHibernate.Cfg.Configuration() .AddProperties(new Dictionary <string, string> { { "dialect", dialect }, { global::NHibernate.Cfg.Environment.ConnectionString, connectionString } }); var modelMapper = new SagaModelMapper(new[] { typeof(T) }); configuration.AddMapping(modelMapper.Compile()); SessionFactory = configuration.BuildSessionFactory(); new SchemaUpdate(configuration).Execute(false, true); session = SessionFactory.OpenSession(); SagaPersister = new SagaPersister(new FakeSessionProvider(SessionFactory, session)); new Installer().Install(WindowsIdentity.GetCurrent().Name, null); }
internal void ApplyMappings(ReadOnlySettings settings, Configuration configuration) { var tableNamingConvention = settings.GetOrDefault <Func <Type, string> >("NHibernate.Sagas.TableNamingConvention"); var scannedAssemblies = settings.GetAvailableTypes().Select(t => t.Assembly).Distinct(); foreach (var assembly in scannedAssemblies) { configuration.AddAssembly(assembly); } var types = settings.GetAvailableTypes().Except(configuration.ClassMappings.Select(x => x.MappedClass)); SagaModelMapper modelMapper; if (tableNamingConvention == null) { modelMapper = new SagaModelMapper(types); } else { modelMapper = new SagaModelMapper(types, tableNamingConvention); } configuration.AddMapping(modelMapper.Compile()); }
internal void ApplyMappings(ReadOnlySettings settings, Configuration configuration) { var tableNamingConvention = settings.GetOrDefault <Func <Type, string> >("NHibernate.Sagas.TableNamingConvention"); var scannedAssemblies = settings.GetAvailableTypes().Select(t => t.Assembly).Distinct(); foreach (var assembly in scannedAssemblies) { configuration.AddAssembly(assembly); } var allSagaMetadata = settings.Get <SagaMetadataCollection>(); var types = settings.GetAvailableTypes().Except(configuration.ClassMappings.Select(x => x.MappedClass)); SagaModelMapper.AddMappings(configuration, allSagaMetadata, types, tableNamingConvention); }
/// <summary> /// Builds the session factory with the given properties. Database is updated if updateSchema is set /// </summary> /// <param name="nhibernateProperties"></param> /// <param name="updateSchema"></param> /// <returns></returns> public ISessionFactory Build(IDictionary <string, string> nhibernateProperties, bool updateSchema) { var scannedAssemblies = _typesToScan.Select(t => t.Assembly).Distinct(); var nhibernateConfiguration = new Configuration().SetProperties(nhibernateProperties); foreach (var assembly in scannedAssemblies) { nhibernateConfiguration.AddAssembly(assembly); } var modelMapper = new SagaModelMapper(_typesToScan.Except(nhibernateConfiguration.ClassMappings.Select(x => x.MappedClass))); var mapping = modelMapper.Compile(); //var serializer = new XmlSerializer(typeof (HbmMapping)); //using (var writer = XmlWriter.Create("sagamappings.xml", new XmlWriterSettings() { Indent = true})) //serializer.Serialize(writer, mapping); nhibernateConfiguration.AddMapping(mapping); ApplyDefaultsTo(nhibernateConfiguration); if (updateSchema) { UpdateDatabaseSchemaUsing(nhibernateConfiguration); } //using (var writer = new StreamWriter("schema.sql")) // new SchemaExport(nhibernateConfiguration) // .Execute(s => writer.Write(s), true, false); try { return(nhibernateConfiguration.BuildSessionFactory()); } catch (Exception e) { if (e.InnerException != null) { throw new ConfigurationErrorsException(e.InnerException.Message, e); } throw; } }
/// <summary> /// Executes the installer. /// </summary> /// <param name="identity">The user for whom permissions will be given.</param> public void Install(string identity) { if (RunInstaller) { var configSection = Configure.GetConfigSection <NHibernateSagaPersisterConfig>(); if (configSection != null) { if (configSection.NHibernateProperties.Count == 0) { throw new InvalidOperationException( "No NHibernate properties found. Please specify NHibernateProperties in your NHibernateSagaPersisterConfig section"); } foreach (var property in configSection.NHibernateProperties.ToProperties()) { ConfigureNHibernate.SagaPersisterProperties[property.Key] = property.Value; } } ConfigureNHibernate.ThrowIfRequiredPropertiesAreMissing(ConfigureNHibernate.SagaPersisterProperties); var configuration = ConfigureNHibernate.CreateConfigurationWith(ConfigureNHibernate.SagaPersisterProperties); var typesToScan = Configure.TypesToScan.ToList(); var scannedAssemblies = typesToScan.Select(t => t.Assembly).Distinct(); foreach (var assembly in scannedAssemblies) { configuration.AddAssembly(assembly); } var modelMapper = new SagaModelMapper(typesToScan.Except(configuration.ClassMappings.Select(x => x.MappedClass))); configuration.AddMapping(modelMapper.Compile()); new SchemaUpdate(configuration).Execute(false, true); } }
/// <summary> /// Builds the session factory with the given properties. Database is updated if updateSchema is set /// </summary> /// <param name="nhibernateProperties"></param> /// <param name="updateSchema"></param> /// <returns></returns> public ISessionFactory Build(IDictionary <string, string> nhibernateProperties, bool updateSchema) { var scannedAssemblies = typesToScan.Select(t => t.Assembly).Distinct(); var nhibernateConfiguration = new Configuration().SetProperties(nhibernateProperties); foreach (var assembly in scannedAssemblies) { nhibernateConfiguration.AddAssembly(assembly); } var mapping = new SagaModelMapper(typesToScan.Except(nhibernateConfiguration.ClassMappings.Select(x => x.MappedClass))); ApplyConventions(mapping); nhibernateConfiguration.AddMapping(mapping.Compile()); ApplyDefaultsTo(nhibernateConfiguration); if (updateSchema) { UpdateDatabaseSchemaUsing(nhibernateConfiguration); } try { return(nhibernateConfiguration.BuildSessionFactory()); } catch (Exception e) { if (e.InnerException != null) { throw new ConfigurationErrorsException(e.InnerException.Message, e); } throw; } }
internal void ApplyMappings(ReadOnlySettings settings, Configuration configuration) { var tableNamingConvention = settings.GetOrDefault<Func<Type, string>>("NHibernate.Sagas.TableNamingConvention"); var scannedAssemblies = settings.GetAvailableTypes().Select(t => t.Assembly).Distinct(); foreach (var assembly in scannedAssemblies) { configuration.AddAssembly(assembly); } var types = settings.GetAvailableTypes().Except(configuration.ClassMappings.Select(x => x.MappedClass)); SagaModelMapper modelMapper; if (tableNamingConvention == null) { modelMapper = new SagaModelMapper(types); } else { modelMapper = new SagaModelMapper(types, tableNamingConvention); } configuration.AddMapping(modelMapper.Compile()); }
private static void ApplyConventions(SagaModelMapper hbmMapping) { var hbmIdField = typeof(global::NHibernate.Mapping.ByCode.Impl.IdMapper).GetField("hbmId", BindingFlags.Instance | BindingFlags.NonPublic); hbmMapping.Mapper.AfterMapClass += (mi, t, map) => { map.Id(idmap => { var hbmId = (global::NHibernate.Cfg.MappingSchema.HbmId)hbmIdField.GetValue(idmap); hbmId.type1 = typeof(GuidToPartitionKeyAndRowKey).AssemblyQualifiedName; hbmId.type = null; hbmId.column1 = null; hbmId.column = new[] { new global::NHibernate.Cfg.MappingSchema.HbmColumn { name = "RowKey" }, new global::NHibernate.Cfg.MappingSchema.HbmColumn { name = "PartitionKey" }, }; }); }; hbmMapping.Mapper.AfterMapManyToOne += (mi, type, map) => MapIdColumns(map, type.LocalMember); hbmMapping.Mapper.AfterMapBag += (mi, type, map) => map.Key(km => MapIdColumns(km, type.LocalMember)); hbmMapping.Mapper.AfterMapJoinedSubclass += (mi, type, map) => map.Key(km => MapIdColumns(km, type.BaseType)); hbmMapping.Mapper.BeforeMapProperty += (mi, type, map) => { var info = type.LocalMember as PropertyInfo; if (info != null && info.PropertyType == typeof(DateTime)) { map.Type <UtcDateTimeUserType>(); } }; }
private static void HackIdIntoMapping(SagaModelMapper hbmMapping) { var hbmIdField = typeof(global::NHibernate.Mapping.ByCode.Impl.IdMapper).GetField("hbmId", BindingFlags.Instance | BindingFlags.NonPublic); hbmMapping.Mapper.AfterMapClass += (mi, t, map) => { map.Id(idmap => { var hbmId = (global::NHibernate.Cfg.MappingSchema.HbmId)hbmIdField.GetValue(idmap); hbmId.type1 = typeof(GuidToPartitionKeyAndRowKey).AssemblyQualifiedName; hbmId.type = null; hbmId.column1 = null; hbmId.column = new[] { new global::NHibernate.Cfg.MappingSchema.HbmColumn {name = "RowKey"}, new global::NHibernate.Cfg.MappingSchema.HbmColumn {name = "PartitionKey"}, }; }); }; hbmMapping.Mapper.AfterMapManyToOne += (mi, type, map) => MapIdColumns(map, type.LocalMember); hbmMapping.Mapper.AfterMapBag += (mi, type, map) => map.Key(km => MapIdColumns(km, type.LocalMember)); hbmMapping.Mapper.AfterMapJoinedSubclass += (mi, type, map) => map.Key(km => MapIdColumns(km, type.BaseType)); }