public static T ConvertFromBase <T>(SagaHeaders baseObj) where T : SagaHeaders, new() { var result = new T(); result.CorrelationId = baseObj.CorrelationId; result.Key = baseObj.Key; result.Value = baseObj.Value; return(result); }
/// <summary> /// Persists the instance of saga into the database storage. /// /// Actually stores SagaData and Headers. All other variables in saga are not persisted /// </summary> /// <typeparam name="TSaga">Type of saga</typeparam> /// <param name="saga">Saga instance</param> public void Save <TSaga>(TSaga saga) where TSaga : class, IAccessibleSaga { Guard.ArgumentIsNotNull(saga, nameof(saga)); var sagaData = NSagaReflection.Get(saga, "SagaData"); var sagaHeaders = (Dictionary <String, String>)NSagaReflection.Get(saga, "Headers"); var correlationId = (Guid)NSagaReflection.Get(saga, "CorrelationId"); var serialisedData = messageSerialiser.Serialise(sagaData); var dataModel = new SagaData() { CorrelationId = correlationId, BlobData = serialisedData, }; using (var connection = connectionFactory.CreateOpenConnection()) using (var database = new Database(connection)) using (var transaction = database.GetTransaction()) { try { int updatedRaws = database.Update(dataModel); if (updatedRaws == 0) { // no records were updated - this means no records already exist - need to insert new record database.Insert(dataModel); } // delete all existing headers database.Delete <SagaHeaders>("WHERE CorrelationId=@0", correlationId); // and insert updated ones foreach (var header in sagaHeaders) { var storedHeader = new SagaHeaders() { CorrelationId = correlationId, Key = header.Key, Value = header.Value, }; database.Insert(storedHeader); } transaction.Complete(); } catch (Exception) { transaction.Dispose(); throw; } } }
/// <summary> /// Persists the instance of saga into the database storage. /// /// Actually stores SagaData and Headers. All other variables in saga are not persisted /// </summary> /// <typeparam name="TSaga">Type of saga</typeparam> /// <param name="saga">Saga instance</param> public void Save <TSaga>(TSaga saga) where TSaga : class, IAccessibleSaga { Guard.ArgumentIsNotNull(saga, nameof(saga)); var sagaData = NSagaReflection.Get(saga, "SagaData"); var sagaHeaders = saga.Headers; var correlationId = saga.CorrelationId; var serialisedData = messageSerialiser.Serialise(sagaData); var dataModel = new SagaData() { CorrelationId = correlationId, BlobData = serialisedData, }; using (var transaction = database.BeginTransaction()) { try { int updatedRaws = database.Update(dataModel); if (updatedRaws == 0) { // no records were updated - this means no records already exist - need to insert new record database.Insert(dataModel); } // delete all existing headers database.DeleteById <SagaHeaders>(correlationId); // and insert updated ones foreach (var header in sagaHeaders) { var storedHeader = new SagaHeaders() { CorrelationId = correlationId, Key = header.Key, Value = header.Value, }; database.Insert(storedHeader); } transaction.CommitTransaction(); } catch (Exception ex) { transaction.RollbackTransaction(); throw; } } }
public object Insert(Database database, SagaHeaders poco) { var entity = SagaHeaders.ConvertFromBase <TSagaHeaders>(poco); return(database.Insert(entity)); }
public int Update(Database database, SagaHeaders poco) { var entity = SagaHeaders.ConvertFromBase <TSagaHeaders>(poco); return(database.Update(entity)); }