Exemple #1
0
 Task IStoreSagaData.Store(ISagaData data, CorrelationProperty correlationProperty, CancellationToken cancellationToken)
 {
     Contract.Requires <ArgumentNullException>(data != null, nameof(data));
     Contract.Requires <ArgumentNullException>(correlationProperty != null, nameof(correlationProperty));
     Contract.Ensures(Contract.Result <Task>() != null);
     return(null);
 }
        /// <summary>
        /// Creates a command to store a saga.
        /// </summary>
        /// <param name="saga">The <see cref="ISagaData">saga data</see> to create the command for.</param>
        /// <param name="correlationProperty">The property used to correlate the stored data.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken">token</see> that can be used to cancel the operation.</param>
        /// <returns>A new, configured <see cref="DbCommand">command</see>.</returns>
        public virtual async Task Store(ISagaData saga, CorrelationProperty correlationProperty, CancellationToken cancellationToken)
        {
            Arg.NotNull(saga, nameof(saga));
            Arg.NotNull(correlationProperty, nameof(correlationProperty));
            Contract.Ensures(Contract.Result <Task>() != null);

            using (var connection = CreateConnection())
            {
                await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
                await Store(connection, saga, correlationProperty, cancellationToken).ConfigureAwait(false);
            }
        }
        /// <summary>
        /// Creates a command to store a saga.
        /// </summary>
        /// <param name="connection">The <see cref="DbConnection">connection</see> to create the command from.</param>
        /// <param name="saga">The <see cref="ISagaData">saga data</see> to create the command for.</param>
        /// <param name="correlationProperty">The property used to correlate the stored data.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken">token</see> that can be used to cancel the operation.</param>
        /// <returns>A new, configured <see cref="DbCommand">command</see>.</returns>
        public virtual async Task Store(DbConnection connection, ISagaData saga, CorrelationProperty correlationProperty, CancellationToken cancellationToken)
        {
            Arg.NotNull(connection, nameof(connection));
            Arg.NotNull(saga, nameof(saga));
            Arg.NotNull(correlationProperty, nameof(correlationProperty));
            Contract.Ensures(Contract.Result <Task>() != null);

            using (var stream = Serialize(saga))
                using (var command = NewStoreCommand(saga, correlationProperty, stream))
                {
                    command.Connection = connection;
                    await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                }
        }
        /// <summary>
        /// Attaches existing saga data to the saga instance.
        /// </summary>
        /// <param name="data">The <typeparamref name="TData">saga data</typeparamref> to attach.</param>
        public virtual void AttachExisting(TData data)
        {
            Arg.NotNull(data, nameof(data));

            SagaId        = data.Id;
            Modified      = Clock.Now;
            Instance.Data = data;

            var property       = Metadata.CorrelationProperty;
            var value          = property.GetValue(data);
            var defaultValue   = property.PropertyType.DefaultValue();
            var isDefaultValue = !Equals(value, defaultValue);

            CorrelationProperty = new CorrelationProperty(property, value, isDefaultValue);
        }
        public async Task retrieve_should_load_saga_by_correlated_property()
        {
            // arrange
            var storedData = new ProcurementData()
            {
                Id = NewGuid(), OrderId = NewGuid()
            };
            var correlationProperty = new CorrelationProperty(Metadata.CorrelationProperty, storedData.OrderId, isDefaultValue: false);

            await Sagas.Store(storedData, correlationProperty, CancellationToken.None);

            // act
            var retrievedData = await Sagas.Retrieve <ProcurementData>(nameof( ProcurementData.OrderId ), storedData.OrderId, CancellationToken.None);

            // assert
            retrievedData.Should().BeEquivalentTo(storedData);
        }
        /// <summary>
        /// Creates a command to store a saga.
        /// </summary>
        /// <param name="saga">The <see cref="ISagaData">saga data</see> to create the command for.</param>
        /// <param name="correlationProperty">The property used to correlate the stored data.</param>
        /// <param name="data">The serialized saga data.</param>
        /// <returns>A new, configured <see cref="DbCommand">command</see>.</returns>
        public virtual DbCommand NewStoreCommand(ISagaData saga, CorrelationProperty correlationProperty, Stream data)
        {
            Arg.NotNull(saga, nameof(saga));
            Arg.NotNull(correlationProperty, nameof(correlationProperty));
            Arg.NotNull(data, nameof(data));
            Contract.Ensures(Contract.Result <DbCommand>() != null);

            var command   = ProviderFactory.CreateCommand();
            var parameter = command.CreateParameter();

            parameter.DbType        = DbType.Guid;
            parameter.ParameterName = "SagaId";
            parameter.Value         = saga.Id;
            command.Parameters.Add(parameter);

            parameter               = command.CreateParameter();
            parameter.DbType        = DbType.String;
            parameter.Size          = 256;
            parameter.ParameterName = "DataType";
            parameter.Value         = saga.GetType().GetAssemblyQualifiedName();
            command.Parameters.Add(parameter);

            parameter               = command.CreateParameter();
            parameter.DbType        = DbType.String;
            parameter.Size          = 90;
            parameter.ParameterName = "PropertyName";
            parameter.Value         = correlationProperty.Property.Name;
            command.Parameters.Add(parameter);

            parameter               = command.CreateParameter();
            parameter.DbType        = DbType.Binary;
            parameter.Size          = 200;
            parameter.ParameterName = "PropertyValue";
            parameter.Value         = ValueAsBinary(correlationProperty.Value);
            command.Parameters.Add(parameter);

            parameter               = command.CreateParameter();
            parameter.DbType        = DbType.Binary;
            parameter.ParameterName = "Data";
            parameter.Value         = data;
            command.Parameters.Add(parameter);

            command.CommandText = Sql.Store;

            return(command);
        }
        public async Task retrieve_should_not_return_a_saga_marked_completed()
        {
            // arrange
            var storedData = new ProcurementData()
            {
                Id = NewGuid(), OrderId = NewGuid()
            };
            var correlationProperty = new CorrelationProperty(Metadata.CorrelationProperty, storedData.OrderId, isDefaultValue: false);

            await Sagas.Store(storedData, correlationProperty, CancellationToken.None);

            // act
            await Sagas.Complete(storedData, CancellationToken.None);

            var retrievedData = await Sagas.Retrieve <ProcurementData>(storedData.Id, CancellationToken.None);

            // assert
            retrievedData.Should().BeNull();
        }
 /// <summary>
 /// Stores the specified saga data.
 /// </summary>
 /// <param name="data">The data to store.</param>
 /// <param name="correlationProperty">The property used to correlate the stored data.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken">token</see> that can be used to cancel the operation.</param>
 /// <returns>A <see cref="Task">task</see> repesenting the asynchronous operation.</returns>
 public Task Store(ISagaData data, CorrelationProperty correlationProperty, CancellationToken cancellationToken)
 {
     Arg.NotNull(data, nameof(data));
     Arg.NotNull(correlationProperty, nameof(correlationProperty));
     return(Configuration.Store(data, correlationProperty, cancellationToken));
 }
 /// <summary>
 /// Stores the specified data.
 /// </summary>
 /// <param name="data">The <see cref="ISagaData">data</see> to store.</param>
 /// <param name="correlationProperty">The property used to correlate the stored data.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken">token</see> that can be used to cancel the operation.</param>
 /// <returns>A <see cref="Task">task</see> representing the asynchronous operation.</returns>
 public virtual Task Store(ISagaData data, CorrelationProperty correlationProperty, CancellationToken cancellationToken)
 {
     Arg.NotNull(data, nameof(data));
     storage.AddOrUpdate(data.Id, data, (key, current) => data);
     return(CompletedTask);
 }