public void Write() { var directory = Path.Combine(TestContext.CurrentContext.TestDirectory, "../../"); foreach (var variant in Enum.GetValues(typeof(BuildSqlVariant)).Cast <BuildSqlVariant>()) { Write(directory, variant, "TimeoutCreate", TimeoutScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "TimeoutDrop", TimeoutScriptBuilder.BuildDropScript(variant)); Write(directory, variant, "OutboxCreate", OutboxScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "OutboxDrop", OutboxScriptBuilder.BuildDropScript(variant)); Write(directory, variant, "SubscriptionCreate", SubscriptionScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "SubscriptionDrop", SubscriptionScriptBuilder.BuildDropScript(variant)); var sagaDefinition = new SagaDefinition( tableSuffix: "OrderSaga", name: "OrderSaga", correlationProperty: new CorrelationProperty( name: "OrderNumber", type: CorrelationPropertyType.Int), transitionalCorrelationProperty: new CorrelationProperty( name: "OrderId", type: CorrelationPropertyType.Guid)); Write(directory, variant, "SagaCreate", SagaScriptBuilder.BuildCreateScript(sagaDefinition, variant)); Write(directory, variant, "SagaDrop", SagaScriptBuilder.BuildDropScript(sagaDefinition, variant)); } foreach (var variant in Enum.GetValues(typeof(SqlVariant)).Cast <SqlVariant>()) { var timeoutCommands = TimeoutCommandBuilder.Build(sqlVariant: variant, tablePrefix: "EndpointName"); Write(directory, variant, "TimeoutAdd", timeoutCommands.Add); Write(directory, variant, "TimeoutNext", timeoutCommands.Next); Write(directory, variant, "TimeoutRange", timeoutCommands.Range); Write(directory, variant, "TimeoutRemoveById", timeoutCommands.RemoveById); Write(directory, variant, "TimeoutRemoveBySagaId", timeoutCommands.RemoveBySagaId); Write(directory, variant, "TimeoutPeek", timeoutCommands.Peek); var outboxCommands = OutboxCommandBuilder.Build(variant, "EndpointName"); Write(directory, variant, "OutboxCleanup", outboxCommands.Cleanup); Write(directory, variant, "OutboxGet", outboxCommands.Get); Write(directory, variant, "OutboxSetAsDispatched", outboxCommands.SetAsDispatched); Write(directory, variant, "OutboxStore", outboxCommands.Store); var subscriptionCommands = SubscriptionCommandBuilder.Build(variant, "EndpointName"); Write(directory, variant, "SubscriptionSubscribe", subscriptionCommands.Subscribe); Write(directory, variant, "SubscriptionUnsubscribe", subscriptionCommands.Unsubscribe); Write(directory, variant, "SubscriptionGetSubscribers", subscriptionCommands.GetSubscribers(new List <MessageType> { new MessageType("MessageTypeName", new Version()) })); var sagaCommandBuilder = new SagaCommandBuilder(variant, "EndpointName"); Write(directory, variant, "SagaComplete", sagaCommandBuilder.BuildCompleteCommand("SagaName")); Write(directory, variant, "SagadGetByProperty", sagaCommandBuilder.BuildGetByPropertyCommand("SagaName", "PropertyName")); Write(directory, variant, "SagaGetBySagaId", sagaCommandBuilder.BuildGetBySagaIdCommand("SagaName")); Write(directory, variant, "SagaSave", sagaCommandBuilder.BuildSaveCommand("SagaName", "CorrelationPproperty", "TransitionalCorrelationPproperty")); Write(directory, variant, "SagaUpdate", sagaCommandBuilder.BuildUpdateCommand("SagaName", "TransitionalCorrelationPproperty")); } }
protected override void Setup(FeatureConfigurationContext context) { var settings = context.Settings; settings.EnableFeature <StorageType.Sagas>(); var sqlVariant = settings.GetSqlVariant(); #pragma warning disable 618 var commandBuilder = new SagaCommandBuilder(sqlVariant); #pragma warning restore 618 var jsonSerializerSettings = SagaSettings.GetJsonSerializerSettings(settings); var jsonSerializer = BuildJsonSerializer(jsonSerializerSettings); var readerCreator = SagaSettings.GetReaderCreator(settings); if (readerCreator == null) { readerCreator = reader => new JsonTextReader(reader); } var writerCreator = SagaSettings.GetWriterCreator(settings); if (writerCreator == null) { writerCreator = writer => new JsonTextWriter(writer); } var nameFilter = SagaSettings.GetNameFilter(settings); if (nameFilter == null) { nameFilter = (sagaName => sagaName); } var versionDeserializeBuilder = SagaSettings.GetVersionSettings(settings); var tablePrefix = settings.GetTablePrefix(); var schema = settings.GetSchema(); var infoCache = new SagaInfoCache( versionSpecificSettings: versionDeserializeBuilder, jsonSerializer: jsonSerializer, readerCreator: readerCreator, writerCreator: writerCreator, commandBuilder: commandBuilder, tablePrefix: tablePrefix, schema: schema, sqlVariant: sqlVariant, metadataCollection: settings.Get <SagaMetadataCollection>(), nameFilter: nameFilter); var sagaPersister = new SagaPersister(infoCache, sqlVariant); var container = context.Container; container.ConfigureComponent(() => infoCache, DependencyLifecycle.SingleInstance); container.ConfigureComponent <ISagaPersister>(() => sagaPersister, DependencyLifecycle.SingleInstance); }
public SagaInfoCache( RetrieveVersionSpecificJsonSettings versionSpecificSettings, JsonSerializer jsonSerializer, Func <TextReader, JsonReader> readerCreator, Func <TextWriter, JsonWriter> writerCreator, SagaCommandBuilder commandBuilder, string tablePrefix, string schema, SqlVariant sqlVariant, SagaMetadataCollection metadataCollection, Func <string, string> nameFilter) { this.versionSpecificSettings = versionSpecificSettings; this.writerCreator = writerCreator; this.readerCreator = readerCreator; this.jsonSerializer = jsonSerializer; this.commandBuilder = commandBuilder; this.tablePrefix = tablePrefix; this.schema = schema; this.sqlVariant = sqlVariant; this.nameFilter = nameFilter; Initialize(metadataCollection); }
SagaPersister SetUp(string endpointName) { var runtimeSqlVariant = sqlVariant.Convert(); #pragma warning disable 618 var commandBuilder = new SagaCommandBuilder(runtimeSqlVariant); #pragma warning restore 618 var sagaMetadataCollection = new SagaMetadataCollection(); sagaMetadataCollection.Initialize(GetSagasAndFinders()); var infoCache = new SagaInfoCache( versionSpecificSettings: null, jsonSerializer: Serializer.JsonSerializer, commandBuilder: commandBuilder, readerCreator: reader => new JsonTextReader(reader), writerCreator: writer => new JsonTextWriter(writer), tablePrefix: $"{endpointName}_", schema: schema, sqlVariant: runtimeSqlVariant, metadataCollection: sagaMetadataCollection, nameFilter: sagaName => sagaName); return(new SagaPersister(infoCache, runtimeSqlVariant)); }
public RuntimeSagaInfo( SagaCommandBuilder commandBuilder, Type sagaDataType, RetrieveVersionSpecificJsonSettings versionSpecificSettings, Type sagaType, JsonSerializer jsonSerializer, Func <TextReader, JsonReader> readerCreator, Func <TextWriter, JsonWriter> writerCreator, string tablePrefix, string schema, SqlVariant sqlVariant, Func <string, string> nameFilter) { this.sagaDataType = sagaDataType; if (versionSpecificSettings != null) { deserializers = new ConcurrentDictionary <Version, JsonSerializer>(); } this.versionSpecificSettings = versionSpecificSettings; SagaType = sagaType; this.jsonSerializer = jsonSerializer; this.readerCreator = readerCreator; this.writerCreator = writerCreator; this.commandBuilder = new CommandBuilder(sqlVariant); CurrentVersion = sagaDataType.Assembly.GetFileVersion(); ValidateIsSqlSaga(); var sqlSagaAttributeData = SqlSagaTypeDataReader.GetTypeData(sagaType); var tableSuffix = nameFilter(sqlSagaAttributeData.TableSuffix); switch (sqlVariant) { case SqlVariant.MsSqlServer: TableName = $"[{schema}].[{tablePrefix}{tableSuffix}]"; FillParameter = ParameterFiller.Fill; break; case SqlVariant.MySql: TableName = $"`{tablePrefix}{tableSuffix}`"; FillParameter = ParameterFiller.Fill; break; case SqlVariant.Oracle: if (tableSuffix.Length > 27) { throw new Exception($"Saga '{tableSuffix}' contains more than 27 characters, which is not supported by SQL persistence using Oracle. Either disable Oracle script generation using the SqlPersistenceSettings assembly attribute, shorten the name of the saga, or specify an alternate table name by overriding the SqlSaga's TableSuffix property."); } if (Encoding.UTF8.GetBytes(tableSuffix).Length != tableSuffix.Length) { throw new Exception($"Saga '{tableSuffix}' contains non-ASCII characters, which is not supported by SQL persistence using Oracle. Either disable Oracle script generation using the SqlPersistenceSettings assembly attribute, change the name of the saga, or specify an alternate table name by overriding the SqlSaga's TableSuffix property."); } TableName = tableSuffix.ToUpper(); FillParameter = ParameterFiller.OracleFill; break; default: throw new Exception($"Unknown SqlVariant: {sqlVariant}."); } CompleteCommand = commandBuilder.BuildCompleteCommand(TableName); SelectFromCommand = commandBuilder.BuildSelectFromCommand(TableName); GetBySagaIdCommand = commandBuilder.BuildGetBySagaIdCommand(TableName); SaveCommand = commandBuilder.BuildSaveCommand(sqlSagaAttributeData.CorrelationProperty, sqlSagaAttributeData.TransitionalCorrelationProperty, TableName); UpdateCommand = commandBuilder.BuildUpdateCommand(sqlSagaAttributeData.TransitionalCorrelationProperty, TableName); CorrelationProperty = sqlSagaAttributeData.CorrelationProperty; HasCorrelationProperty = CorrelationProperty != null; if (HasCorrelationProperty) { GetByCorrelationPropertyCommand = commandBuilder.BuildGetByPropertyCommand(sqlSagaAttributeData.CorrelationProperty, TableName); } TransitionalCorrelationProperty = sqlSagaAttributeData.TransitionalCorrelationProperty; HasTransitionalCorrelationProperty = TransitionalCorrelationProperty != null; if (HasTransitionalCorrelationProperty) { TransitionalAccessor = sagaDataType.GetPropertyAccessor <IContainSagaData>(TransitionalCorrelationProperty); } }