Skip to content

Commit

Permalink
Merge pull request #9 from reecerussell/add-on-complete-events
Browse files Browse the repository at this point in the history
Add on complete events
  • Loading branch information
reecerussell authored Jul 2, 2021
2 parents 625a673 + adf12a7 commit c115a55
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
11 changes: 9 additions & 2 deletions TxCommand.Abstractions/ITxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@

namespace TxCommand.Abstractions
{
/// <summary>
/// An empty interface used to identify the different variants of <see cref="ITxCommand"/>.
/// </summary>
public interface ICommand
{
}

/// <summary>
/// A transaction command is an abstraction used to execute a command which requires
/// a database transaction in order to operate correctly. <see cref="ITxCommand"/> provides a method
/// to execute the command, providing it with a <see cref="IDbTransaction"/>.
/// </summary>
public interface ITxCommand
public interface ITxCommand : ICommand
{
/// <summary>
/// Executes the implementing command, providing a <see cref="IDbTransaction"/>, allowing
Expand All @@ -34,7 +41,7 @@ public interface ITxCommand
/// this provides a type argument, <typeparamref name="TResult"/>, allowing the
/// command to output data.
/// </summary>
public interface ITxCommand<TResult>
public interface ITxCommand<TResult> : ICommand
{
/// <summary>
/// Executes the implementing command, providing a <see cref="IDbTransaction"/>, allowing
Expand Down
6 changes: 3 additions & 3 deletions TxCommand.Abstractions/TxCommand.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<RepositoryUrl>https://github.com/reecerussell/tx-command</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>CQRS, commanding, sql, transaction</PackageTags>
<AssemblyVersion>0.2.0.0</AssemblyVersion>
<FileVersion>0.2.0.0</FileVersion>
<Version>0.2.0</Version>
<AssemblyVersion>0.3.0.0</AssemblyVersion>
<FileVersion>0.3.0.0</FileVersion>
<Version>0.3.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/reecerussell/tx-command</PackageProjectUrl>
</PropertyGroup>
Expand Down
22 changes: 21 additions & 1 deletion TxCommand.Tests/TxCommandExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,18 @@ public async Task ExecuteAsync_GivenCommand_ExecutesTheCommand()
command.Setup(x => x.ExecuteAsync(connection.Object, transaction)).Returns(Task.CompletedTask).Verifiable();

var commandExecutor = new TxCommandExecutor(connection.Object);

var callbackCalled = false;
commandExecutor.OnComplete += (c) =>
{
callbackCalled = true;
Assert.Equal(command.Object, c);
};

await commandExecutor.ExecuteAsync(command.Object);

Assert.True(callbackCalled);

command.Verify(x => x.Validate(), Times.Once);
command.Verify(x => x.ExecuteAsync(connection.Object, transaction), Times.Once);
connection.Verify(x => x.BeginTransaction());
Expand Down Expand Up @@ -224,8 +234,18 @@ public async Task ExecuteAsyncWithResult_GivenCommand_ExecutesTheCommand()
command.Setup(x => x.ExecuteAsync(connection.Object, transaction)).ReturnsAsync(testResult).Verifiable();

var commandExecutor = new TxCommandExecutor(connection.Object);
var result = await commandExecutor.ExecuteAsync(command.Object);

var callbackCalled = false;
commandExecutor.OnComplete += (c) =>
{
callbackCalled = true;
Assert.Equal(command.Object, c);
};

var result =await commandExecutor.ExecuteAsync(command.Object);

Assert.Equal(testResult, result);
Assert.True(callbackCalled);

command.Verify(x => x.Validate(), Times.Once);
command.Verify(x => x.ExecuteAsync(connection.Object, transaction), Times.Once);
Expand Down
6 changes: 3 additions & 3 deletions TxCommand/TxCommand.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.3.1</Version>
<Version>0.4.0</Version>
<Authors>Reece Russell</Authors>
<RepositoryUrl>https://github.com/reecerussell/tx-command</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>CQRS, commanding, sql, transaction</PackageTags>
<Description>Provides support for executing CQRS command within a database transaction.</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Company />
<AssemblyVersion>0.3.1.0</AssemblyVersion>
<FileVersion>0.3.1.0</FileVersion>
<AssemblyVersion>0.4.0.0</AssemblyVersion>
<FileVersion>0.4.0.0</FileVersion>
<PackageProjectUrl>https://github.com/reecerussell/tx-command</PackageProjectUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
Expand Down
12 changes: 11 additions & 1 deletion TxCommand/TxCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class TxCommandExecutor : ITxCommandExecutor
private bool _disposed = false;
private bool _completed = true;

public delegate void CommandDelegate(ICommand command);
public CommandDelegate OnComplete;

/// <summary>
/// Initializes a new instance of <see cref="TxCommandExecutor"/>.
/// </summary>
Expand Down Expand Up @@ -92,6 +95,8 @@ public async Task ExecuteAsync(ITxCommand command)
{
command.Validate();
await command.ExecuteAsync(_connection, _transaction);

OnComplete?.Invoke(command);
}
catch
{
Expand Down Expand Up @@ -137,7 +142,12 @@ public async Task<TResult> ExecuteAsync<TResult>(ITxCommand<TResult> command)
try
{
command.Validate();
return await command.ExecuteAsync(_connection, _transaction);

var result = await command.ExecuteAsync(_connection, _transaction);

OnComplete?.Invoke(command);

return result;
}
catch
{
Expand Down

0 comments on commit c115a55

Please sign in to comment.