Skip to content

Commit

Permalink
Unit of work refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
KonstantinRyazantsev committed Aug 12, 2020
1 parent dcef08e commit 0e71e79
Show file tree
Hide file tree
Showing 21 changed files with 307 additions and 162 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Threading.Tasks;

namespace Swisschain.Extensions.Idempotency
{
internal sealed class DefaultOutboxReadRepository : IOutboxReadRepository
{
public Task<Outbox> GetOrDefault(string idempotencyId)
{
throw new InvalidOperationException("Outbox repository is not configured. To use outbox, you need to configure a persistence in service.AddIdempotency(c => {...})");
}
}
}
23 changes: 0 additions & 23 deletions src/Swisschain.Extensions.Idempotency/DefaultOutboxRepository.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/Swisschain.Extensions.Idempotency/IIdGenerator.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Swisschain.Extensions.Idempotency
{
/// <summary>
/// Should work in the context of the <see cref="IUnitOfWork"/> transaction
/// </summary>
public interface IIdGeneratorRepository
{
Task<long> GetId(string idempotencyId, string generatorName);
Expand Down
2 changes: 1 addition & 1 deletion src/Swisschain.Extensions.Idempotency/IOutboxManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Swisschain.Extensions.Idempotency
{
public interface IOutboxManager
internal interface IOutboxManager
{
Task<Outbox> Open(string idempotencyId);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Swisschain.Extensions.Idempotency/IOutboxReadRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading.Tasks;

namespace Swisschain.Extensions.Idempotency
{
/// <summary>
/// Should work out of the context of the <see cref="IUnitOfWork"/> transaction
/// </summary>
public interface IOutboxReadRepository
{
Task<Outbox> GetOrDefault(string idempotencyId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Swisschain.Extensions.Idempotency
{
public interface IOutboxRepository
/// <summary>
/// Should work in the context of the <see cref="IUnitOfWork"/> transaction
/// </summary>
public interface IOutboxWriteRepository
{
Task<Outbox> GetOrDefault(IUnitOfWork unitOfWork, IOutboxDispatcher dispatcher, string idempotencyId);
Task Add(Outbox outbox);
Task Update(Outbox outbox);
}
Expand Down
42 changes: 34 additions & 8 deletions src/Swisschain.Extensions.Idempotency/IUnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
using System;
using System.Threading.Tasks;

namespace Swisschain.Extensions.Idempotency
{
public interface IUnitOfWork : IDisposable
public interface IUnitOfWork : IAsyncDisposable
{
IIdGeneratorRepository IdGeneratorRepository { get; }
IOutboxWriteRepository OutboxWriteRepository { get; }
string IdempotencyId { get; }
bool IsCommitted { get; }
bool IsRolledBack { get; }
Outbox Outbox { get; }

/// <summary>
/// Should be used only by Swisschain.Extensions.Idempotency
/// Closes the <see cref="Outbox"/> and commits unit of work transaction
/// </summary>
IOutboxRepository Outbox { get; }
Task Commit();

/// <summary>
/// Should be used only by Swisschain.Extensions.Idempotency
/// Rollbacks unit of work transaction
/// </summary>
void Commit();
Task Rollback();

/// <summary>
/// Should be used only by Swisschain.Extensions.Idempotency
/// Generates or returns existing unique ID for the given <see cref="IdempotencyId"/> using
/// <see cref="IdGeneratorRepository"/> and specified <paramref name="generatorName"/>
/// </summary>
void Rollback();
Task<long> GenerateId(string generatorName);

/// <summary>
/// Generates or returns existing unique ID for the specified <paramref name="idempotencyId"/> using
/// <see cref="IdGeneratorRepository"/> and specified <paramref name="generatorName"/>
/// </summary>
Task<long> GenerateId(string idempotencyId, string generatorName);

/// <summary>
/// Dispatches <see cref="Outbox"/> using default <see cref="IOutboxDispatcher"/>.
/// It's recommended to use more specific dispatcher if possible.
/// </summary>
Task EnsureOutboxDispatched();

/// <summary>
/// Dispatches <see cref="Outbox"/> using specified <paramref name="dispatcher"/>.
/// </summary>
Task EnsureOutboxDispatched(IOutboxDispatcher dispatcher);
}
}
}
11 changes: 7 additions & 4 deletions src/Swisschain.Extensions.Idempotency/IUnitOfWorkFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace Swisschain.Extensions.Idempotency
using System.Threading.Tasks;

namespace Swisschain.Extensions.Idempotency
{
public interface IUnitOfWorkFactory
public interface IUnitOfWorkFactory<TUnitOfWork>
where TUnitOfWork : UnitOfWorkBase
{
IUnitOfWork Create();
Task<TUnitOfWork> Create(Outbox outbox);
}
}
}
10 changes: 10 additions & 0 deletions src/Swisschain.Extensions.Idempotency/IUnitOfWorkManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Tasks;

namespace Swisschain.Extensions.Idempotency
{
public interface IUnitOfWorkManager<TUnitOfWork>
where TUnitOfWork : UnitOfWorkBase
{
Task<TUnitOfWork> Begin(string idempotencyId);
}
}
10 changes: 0 additions & 10 deletions src/Swisschain.Extensions.Idempotency/IdFactory.cs

This file was deleted.

19 changes: 0 additions & 19 deletions src/Swisschain.Extensions.Idempotency/IdGenerator.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ public static class IdempotencyServiceCollectionExtensions
public static IServiceCollection AddIdempotency(this IServiceCollection services, Action<IdempotencyConfigurationBuilder> config)
{
services.AddTransient<IOutboxManager, OutboxManager>();
services.AddTransient<IIdGenerator, IdGenerator>();

services.AddTransient<IOutboxDispatcher, DefaultOutboxDispatcher>();
services.AddTransient<IOutboxRepository, DefaultOutboxRepository>();
services.AddTransient<IIdGeneratorRepository, DefaultIdGeneratorRepository>();
services.AddTransient<IOutboxReadRepository, DefaultOutboxReadRepository>();

if (config != null)
{
Expand Down
Loading

0 comments on commit 0e71e79

Please sign in to comment.