예제 #1
0
        public void UpdateSaga(Type sagaType, string sagaIdentifier, object saga, ITrackingToken token, IAssociationValues associationValues)
        {
            var entry = new SagaEntry(saga, sagaIdentifier, _serializer);

            _logger.LogDebug($"Updating saga id {sagaIdentifier} as {Encoding.UTF8.GetString(entry.SerializedSaga)}");
            int updateCount;

            try
            {
                using (var conn = _connectionProvider())
                {
                    conn.Open();
                    updateCount = _sqldef.SqlUpdateSaga(conn, entry.SagaId, entry.SerializedSaga, entry.SagaType, entry.Revision);

                    if (updateCount != 0)
                    {
                        foreach (var associationValue in associationValues.AddedAssociations)
                        {
                            _sqldef.SqlStoreAssocValue(conn, associationValue.PropertyKey, associationValue.PropertyValue, SagaTypeName(sagaType), sagaIdentifier);
                        }
                        foreach (var associationValue in associationValues.RemovedAssociations)
                        {
                            _sqldef.SqlRemoveAssocValue(conn, associationValue.PropertyKey, associationValue.PropertyValue, SagaTypeName(sagaType), sagaIdentifier);
                        }
                    }
                }
            }
            catch (DataException e)
            {
                throw new SagaStorageException("Exception while loading a Saga", e);
            }

            if (updateCount == 0)
            {
                _logger.LogWarning("Expected to be able to update a Saga instance, but no rows were found. Inserting instead.");
                InsertSaga(sagaType, sagaIdentifier, saga, token, associationValues.AsSet());
            }
        }
예제 #2
0
        public void InsertSaga(Type sagaType, string sagaIdentifier, object saga, ITrackingToken token, ISet <AssociationValue> associationValues)
        {
            var entry = new SagaEntry(saga, sagaIdentifier, _serializer);

            _logger.LogDebug($"Storing saga id {sagaIdentifier} as {Encoding.UTF8.GetString(entry.SerializedSaga)}");

            try
            {
                using (var conn = _connectionProvider())
                {
                    conn.Open();
                    _sqldef.SqlStoreSaga(conn, entry.SagaId, entry.Revision, entry.SagaType, entry.SerializedSaga);

                    foreach (var associationValue in associationValues)
                    {
                        _sqldef.SqlStoreAssocValue(conn, associationValue.PropertyKey, associationValue.PropertyValue, SagaTypeName(sagaType), sagaIdentifier);
                    }
                }
            }
            catch (DataException e)
            {
                throw new SagaStorageException("Exception while loading a Saga", e);
            }
        }