Skip to content

Commit

Permalink
ID Generator moved out of the unit of work
Browse files Browse the repository at this point in the history
  • Loading branch information
KonstantinRyazantsev committed Aug 12, 2020
1 parent e6dcf4a commit 51f2ff2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 26 deletions.
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 DefaultIdGeneratorRepository : IIdGeneratorRepository
{
public Task<long> GetId(string idempotencyId, string generatorName)
{
throw new InvalidOperationException("ID generator repository is not configured. To use ID generator, you need to configure a persistence in service.AddIdempotency(c => {...})");
}
}
}
9 changes: 9 additions & 0 deletions src/Swisschain.Extensions.Idempotency/IIdGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace Swisschain.Extensions.Idempotency
{
public interface IIdGenerator
{
Task<long> GetId(string idempotencyId, string generatorName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Swisschain.Extensions.Idempotency
{
/// <summary>
/// Should work in the context of the <see cref="IUnitOfWork"/> transaction
/// Should work not in the context of the <see cref="IUnitOfWork"/> transaction
/// </summary>
public interface IIdGeneratorRepository
{
Expand Down
13 changes: 0 additions & 13 deletions src/Swisschain.Extensions.Idempotency/IUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Swisschain.Extensions.Idempotency
{
public interface IUnitOfWork : IAsyncDisposable
{
IIdGeneratorRepository IdGeneratorRepository { get; }
IOutboxWriteRepository OutboxWriteRepository { get; }
string IdempotencyId { get; }
bool IsCommitted { get; }
Expand All @@ -27,18 +26,6 @@ Task Init(IIdGeneratorRepository idGeneratorRepository,
/// </summary>
Task Rollback();

/// <summary>
/// Generates or returns existing unique ID for the given <see cref="IdempotencyId"/> using
/// <see cref="IdGeneratorRepository"/> and specified <paramref name="generatorName"/>
/// </summary>
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.
Expand Down
19 changes: 19 additions & 0 deletions src/Swisschain.Extensions.Idempotency/IdGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Threading.Tasks;

namespace Swisschain.Extensions.Idempotency
{
internal sealed class IdGenerator : IIdGenerator
{
readonly IIdGeneratorRepository _idGeneratorRepository;

public IdGenerator(IIdGeneratorRepository idGeneratorRepository)
{
_idGeneratorRepository = idGeneratorRepository;
}

public Task<long> GetId(string idempotencyId, string generatorName)
{
return _idGeneratorRepository.GetId(idempotencyId, generatorName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ 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<IOutboxReadRepository, DefaultOutboxReadRepository>();
services.AddTransient<IIdGeneratorRepository, DefaultIdGeneratorRepository>();

if (config != null)
{
Expand Down
12 changes: 0 additions & 12 deletions src/Swisschain.Extensions.Idempotency/UnitOfWorkBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public UnitOfWorkBase(IOutboxDispatcher defaultOutboxDispatcher)
_defaultOutboxDispatcher = defaultOutboxDispatcher;
}

public IIdGeneratorRepository IdGeneratorRepository { get; private set; }
public IOutboxWriteRepository OutboxWriteRepository { get; private set; }
public string IdempotencyId { get; private set; }
public bool IsCommitted { get; private set; }
Expand All @@ -29,7 +28,6 @@ public Task Init(IIdGeneratorRepository idGeneratorRepository,
Outbox outbox)
{
OutboxWriteRepository = outboxWriteRepository;
IdGeneratorRepository = idGeneratorRepository;
IdempotencyId = idempotencyId;
Outbox = outbox;

Expand Down Expand Up @@ -73,16 +71,6 @@ public async Task Rollback()
IsRolledBack = true;
}

public Task<long> GenerateId(string generatorName)
{
return IdGeneratorRepository.GetId(IdempotencyId, generatorName);
}

public Task<long> GenerateId(string idempotencyId, string generatorName)
{
return IdGeneratorRepository.GetId(idempotencyId, generatorName);
}

public Task EnsureOutboxDispatched()
{
return EnsureOutboxDispatched(_defaultOutboxDispatcher);
Expand Down

0 comments on commit 51f2ff2

Please sign in to comment.