Skip to content

Commit

Permalink
Refactored classes to use Primary Constructors where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed May 2, 2024
1 parent 5a8fc7a commit 7ca9108
Show file tree
Hide file tree
Showing 280 changed files with 849 additions and 2,720 deletions.
18 changes: 6 additions & 12 deletions Core.ElasticSearch/Projections/ElasticSearchProjection.cs
Expand Up @@ -6,21 +6,15 @@

namespace Core.ElasticSearch.Projections;

public class ElasticSearchProjection<TEvent, TView> : IEventHandler<EventEnvelope<TEvent>>
public class ElasticSearchProjection<TEvent, TView>(
ElasticsearchClient elasticClient,
Func<TEvent, string> getId)
: IEventHandler<EventEnvelope<TEvent>>
where TView : class, IProjection
where TEvent : notnull
{
private readonly ElasticsearchClient elasticClient;
private readonly Func<TEvent, string> getId;

public ElasticSearchProjection(
ElasticsearchClient elasticClient,
Func<TEvent, string> getId
)
{
this.elasticClient = elasticClient ?? throw new ArgumentNullException(nameof(elasticClient));
this.getId = getId ?? throw new ArgumentNullException(nameof(getId));
}
private readonly ElasticsearchClient elasticClient = elasticClient ?? throw new ArgumentNullException(nameof(elasticClient));
private readonly Func<TEvent, string> getId = getId ?? throw new ArgumentNullException(nameof(getId));

public async Task Handle(EventEnvelope<TEvent> eventEnvelope, CancellationToken ct)
{
Expand Down
12 changes: 3 additions & 9 deletions Core.ElasticSearch/Repository/ElasticSearchRepository.cs
Expand Up @@ -13,16 +13,10 @@ public interface IElasticSearchRepository<T> where T : class, IAggregate
Task Delete(T aggregate, CancellationToken cancellationToken);
}

public class ElasticSearchRepository<T>: IElasticSearchRepository<T> where T : class, IAggregate
public class ElasticSearchRepository<T>(ElasticsearchClient elasticClient): IElasticSearchRepository<T>
where T : class, IAggregate
{
private readonly ElasticsearchClient elasticClient;

public ElasticSearchRepository(
ElasticsearchClient elasticClient
)
{
this.elasticClient = elasticClient ?? throw new ArgumentNullException(nameof(elasticClient));
}
private readonly ElasticsearchClient elasticClient = elasticClient ?? throw new ArgumentNullException(nameof(elasticClient));

public async Task<T?> Find(Guid id, CancellationToken cancellationToken)
{
Expand Down
Expand Up @@ -92,13 +92,8 @@ public async Task CommandIsStoredInEventStoreDBAndForwardedToCommandHandler()

public record AddUser(Guid UserId, string? Sth = default);

internal class AddUserCommandHandler: ICommandHandler<AddUser>
internal class AddUserCommandHandler(List<Guid> userIds): ICommandHandler<AddUser>
{
private readonly List<Guid> userIds;

public AddUserCommandHandler(List<Guid> userIds) =>
this.userIds = userIds;

public Task Handle(AddUser command, CancellationToken ct)
{
userIds.Add(command.UserId);
Expand Down
7 changes: 1 addition & 6 deletions Core.EventStoreDB/Commands/EventStoreDBAsyncCommandBus.cs
Expand Up @@ -9,15 +9,10 @@ namespace Core.EventStoreDB.Commands;
/// Note: This is an example of the outbox pattern for Command Bus using EventStoreDB
/// For production use mature tooling like Wolverine, MassTransit or NServiceBus
/// </summary>
public class EventStoreDBAsyncCommandBus : IAsyncCommandBus
public class EventStoreDBAsyncCommandBus(EventStoreClient eventStoreClient): IAsyncCommandBus
{
public static readonly string CommandsStreamId = "commands-external";

private readonly EventStoreClient eventStoreClient;

public EventStoreDBAsyncCommandBus(EventStoreClient eventStoreClient) =>
this.eventStoreClient = eventStoreClient;

public Task Schedule<TCommand>(TCommand command, CancellationToken ct = default) where TCommand: notnull
{
return eventStoreClient.Append(CommandsStreamId, command, ct);
Expand Down
18 changes: 6 additions & 12 deletions Core.EventStoreDB/Repository/EventStoreDBRepository.cs
Expand Up @@ -16,19 +16,13 @@ public interface IEventStoreDBRepository<T> where T : class, IAggregate
Task<ulong> Delete(T aggregate, ulong? expectedRevision = null, CancellationToken ct = default);
}

public class EventStoreDBRepository<T>: IEventStoreDBRepository<T> where T : class, IAggregate
public class EventStoreDBRepository<T>(
EventStoreClient eventStore,
IActivityScope activityScope)
: IEventStoreDBRepository<T>
where T : class, IAggregate
{
private readonly EventStoreClient eventStore;
private readonly IActivityScope activityScope;

public EventStoreDBRepository(
EventStoreClient eventStore,
IActivityScope activityScope
)
{
this.eventStore = eventStore;
this.activityScope = activityScope;
}
private readonly IActivityScope activityScope = activityScope;

public Task<T?> Find(Guid id, CancellationToken cancellationToken) =>
eventStore.AggregateStream<T>(
Expand Down
Expand Up @@ -3,24 +3,13 @@

namespace Core.EventStoreDB.Repository;

public class EventStoreDBRepositoryWithETagDecorator<T>: IEventStoreDBRepository<T>
public class EventStoreDBRepositoryWithETagDecorator<T>(
IEventStoreDBRepository<T> inner,
IExpectedResourceVersionProvider expectedResourceVersionProvider,
INextResourceVersionProvider nextResourceVersionProvider)
: IEventStoreDBRepository<T>
where T : class, IAggregate
{
private readonly IEventStoreDBRepository<T> inner;
private readonly IExpectedResourceVersionProvider expectedResourceVersionProvider;
private readonly INextResourceVersionProvider nextResourceVersionProvider;

public EventStoreDBRepositoryWithETagDecorator(
IEventStoreDBRepository<T> inner,
IExpectedResourceVersionProvider expectedResourceVersionProvider,
INextResourceVersionProvider nextResourceVersionProvider
)
{
this.inner = inner;
this.expectedResourceVersionProvider = expectedResourceVersionProvider;
this.nextResourceVersionProvider = nextResourceVersionProvider;
}

public Task<T?> Find(Guid id, CancellationToken cancellationToken) =>
inner.Find(id, cancellationToken);

Expand Down
Expand Up @@ -4,21 +4,12 @@

namespace Core.EventStoreDB.Repository;

public class EventStoreDBRepositoryWithTelemetryDecorator<T>: IEventStoreDBRepository<T>
public class EventStoreDBRepositoryWithTelemetryDecorator<T>(
IEventStoreDBRepository<T> inner,
IActivityScope activityScope)
: IEventStoreDBRepository<T>
where T : class, IAggregate
{
private readonly IEventStoreDBRepository<T> inner;
private readonly IActivityScope activityScope;

public EventStoreDBRepositoryWithTelemetryDecorator(
IEventStoreDBRepository<T> inner,
IActivityScope activityScope
)
{
this.inner = inner;
this.activityScope = activityScope;
}

public Task<T?> Find(Guid id, CancellationToken cancellationToken) =>
inner.Find(id, cancellationToken);

Expand Down
Expand Up @@ -5,15 +5,10 @@ namespace Core.EventStoreDB.Subscriptions;

public record CheckpointStored(string SubscriptionId, ulong? Position, DateTime CheckpointedAt);

public class EventStoreDBSubscriptionCheckpointRepository: ISubscriptionCheckpointRepository
public class EventStoreDBSubscriptionCheckpointRepository(EventStoreClient eventStoreClient)
: ISubscriptionCheckpointRepository
{
private readonly EventStoreClient eventStoreClient;

public EventStoreDBSubscriptionCheckpointRepository(
EventStoreClient eventStoreClient)
{
this.eventStoreClient = eventStoreClient ?? throw new ArgumentNullException(nameof(eventStoreClient));
}
private readonly EventStoreClient eventStoreClient = eventStoreClient ?? throw new ArgumentNullException(nameof(eventStoreClient));

public async ValueTask<ulong?> Load(string subscriptionId, CancellationToken ct)
{
Expand Down
7 changes: 1 addition & 6 deletions Core.Marten/Commands/MartenAsyncCommandBus.cs
Expand Up @@ -8,15 +8,10 @@ namespace Core.Marten.Commands;
/// Note: This is an example of the outbox pattern for Command Bus using Marten
/// For production use mature tooling like Wolverine, MassTransit or NServiceBus
/// </summary>
public class MartenAsyncCommandBus : IAsyncCommandBus
public class MartenAsyncCommandBus(IDocumentSession documentSession): IAsyncCommandBus
{
public static readonly Guid CommandsStreamId = new("11111111-1111-1111-1111-111111111111");

private readonly IDocumentSession documentSession;

public MartenAsyncCommandBus(IDocumentSession documentSession) =>
this.documentSession = documentSession;

public Task Schedule<TCommand>(TCommand command, CancellationToken ct = default) where TCommand: notnull
{
documentSession.Events.Append(CommandsStreamId, command);
Expand Down
16 changes: 3 additions & 13 deletions Core.Marten/ExternalProjections/MartenExternalProjection.cs
Expand Up @@ -5,22 +5,12 @@

namespace Core.Marten.ExternalProjections;

public class MartenExternalProjection<TEvent, TView>: IEventHandler<EventEnvelope<TEvent>>
public class MartenExternalProjection<TEvent, TView>(
IDocumentSession session,
Func<TEvent, Guid> getId): IEventHandler<EventEnvelope<TEvent>>
where TView : IVersionedProjection
where TEvent : notnull
{
private readonly IDocumentSession session;
private readonly Func<TEvent, Guid> getId;

public MartenExternalProjection(
IDocumentSession session,
Func<TEvent, Guid> getId
)
{
this.session = session;
this.getId = getId;
}

public async Task Handle(EventEnvelope<TEvent> eventEnvelope, CancellationToken ct)
{
var (@event, eventMetadata) = eventEnvelope;
Expand Down
9 changes: 2 additions & 7 deletions Core.Marten/Ids/MartenIdGenerator.cs
Expand Up @@ -4,14 +4,9 @@

namespace Core.Marten.Ids;

public class MartenIdGenerator : IIdGenerator
public class MartenIdGenerator(IDocumentSession documentSession): IIdGenerator
{
private readonly IDocumentSession documentSession;

public MartenIdGenerator(IDocumentSession documentSession)
{
this.documentSession = documentSession ?? throw new ArgumentNullException(nameof(documentSession));
}
private readonly IDocumentSession documentSession = documentSession ?? throw new ArgumentNullException(nameof(documentSession));

public Guid New() => CombGuidIdGeneration.NewGuid();
}
Expand Up @@ -6,27 +6,14 @@

namespace Core.Marten.Repository;

public class MartenRepositoryWithTracingDecorator<T>: IMartenRepository<T>
public class MartenRepositoryWithTracingDecorator<T>(
IMartenRepository<T> inner,
IDocumentSession documentSession,
IActivityScope activityScope,
ILogger<MartenRepositoryWithTracingDecorator<T>> logger)
: IMartenRepository<T>
where T : class, IAggregate
{
private readonly IMartenRepository<T> inner;
private readonly IDocumentSession documentSession;
private readonly IActivityScope activityScope;
private readonly ILogger<MartenRepositoryWithTracingDecorator<T>> logger;

public MartenRepositoryWithTracingDecorator(
IMartenRepository<T> inner,
IDocumentSession documentSession,
IActivityScope activityScope,
ILogger<MartenRepositoryWithTracingDecorator<T>> logger
)
{
this.inner = inner;
this.activityScope = activityScope;
this.logger = logger;
this.documentSession = documentSession;
}

public Task<T?> Find(Guid id, CancellationToken cancellationToken) =>
inner.Find(id, cancellationToken);

Expand Down
8 changes: 2 additions & 6 deletions Core.Marten/Repository/MartenRepository.cs
Expand Up @@ -14,13 +14,9 @@ public interface IMartenRepository<T> where T : class, IAggregate
Task<long> Delete(T aggregate, long? expectedVersion = null, CancellationToken cancellationToken = default);
}

public class MartenRepository<T>: IMartenRepository<T> where T : class, IAggregate
public class MartenRepository<T>(IDocumentSession documentSession): IMartenRepository<T>
where T : class, IAggregate
{
private readonly IDocumentSession documentSession;

public MartenRepository(IDocumentSession documentSession) =>
this.documentSession = documentSession;

public Task<T?> Find(Guid id, CancellationToken ct) =>
documentSession.Events.AggregateStreamAsync<T>(id, token: ct);

Expand Down
21 changes: 5 additions & 16 deletions Core.Marten/Repository/MartenRepositoryWithETagDecorator.cs
Expand Up @@ -3,24 +3,13 @@

namespace Core.Marten.Repository;

public class MartenRepositoryWithETagDecorator<T>: IMartenRepository<T>
public class MartenRepositoryWithETagDecorator<T>(
IMartenRepository<T> inner,
IExpectedResourceVersionProvider expectedResourceVersionProvider,
INextResourceVersionProvider nextResourceVersionProvider)
: IMartenRepository<T>
where T : class, IAggregate
{
private readonly IMartenRepository<T> inner;
private readonly IExpectedResourceVersionProvider expectedResourceVersionProvider;
private readonly INextResourceVersionProvider nextResourceVersionProvider;

public MartenRepositoryWithETagDecorator(
IMartenRepository<T> inner,
IExpectedResourceVersionProvider expectedResourceVersionProvider,
INextResourceVersionProvider nextResourceVersionProvider
)
{
this.inner = inner;
this.expectedResourceVersionProvider = expectedResourceVersionProvider;
this.nextResourceVersionProvider = nextResourceVersionProvider;
}

public Task<T?> Find(Guid id, CancellationToken cancellationToken) =>
inner.Find(id, cancellationToken);

Expand Down
Expand Up @@ -5,17 +5,12 @@

namespace Core.Marten.Serialization;

public class NonDefaultConstructorMartenJsonNetContractResolver: JsonNetContractResolver
public class NonDefaultConstructorMartenJsonNetContractResolver(
Casing casing,
CollectionStorage collectionStorage,
NonPublicMembersStorage nonPublicMembersStorage = NonPublicMembersStorage.Default)
: JsonNetContractResolver(casing, collectionStorage, nonPublicMembersStorage)
{
public NonDefaultConstructorMartenJsonNetContractResolver(
Casing casing,
CollectionStorage collectionStorage,
NonPublicMembersStorage nonPublicMembersStorage = NonPublicMembersStorage.Default):
base(casing, collectionStorage, nonPublicMembersStorage)
{

}

protected override JsonObjectContract CreateObjectContract(Type objectType)
{
return JsonObjectContractProvider.UsingNonDefaultConstructor(
Expand Down
11 changes: 1 addition & 10 deletions Core.Testing/EventListener.cs
Expand Up @@ -32,17 +32,8 @@ await foreach (var item in events.Reader.ReadAllAsync(ct).ConfigureAwait(false))
events.Reader.ReadAllAsync(ct);
}

public class EventCatcher: IEventBus
public class EventCatcher(EventListener listener, IEventBus eventBus): IEventBus
{
private readonly EventListener listener;
private readonly IEventBus eventBus;

public EventCatcher(EventListener listener, IEventBus eventBus)
{
this.listener = listener;
this.eventBus = eventBus;
}

public async Task Publish(IEventEnvelope @event, CancellationToken ct)
{
await eventBus.Publish(@event, ct).ConfigureAwait(false);
Expand Down
Expand Up @@ -8,22 +8,13 @@

namespace Core.WebApi.OptimisticConcurrency;

public class OptimisticConcurrencyMiddleware
public class OptimisticConcurrencyMiddleware(RequestDelegate next)
{
private readonly RequestDelegate next;

private readonly string[] SupportedMethods =
[
HttpMethod.Post.Method, HttpMethod.Put.Method, HttpMethod.Delete.Method
];

public OptimisticConcurrencyMiddleware(
RequestDelegate next
)
{
this.next = next;
}

public async Task Invoke(
HttpContext context,
IExpectedResourceVersionProvider expectedResourceVersionProvider,
Expand Down

0 comments on commit 7ca9108

Please sign in to comment.