Skip to content

Commit

Permalink
Merge pull request #6 from EventStore/cqrs_flow/dotnet/refactoring
Browse files Browse the repository at this point in the history
Refactoring of the CQRS Flow .NET example based on the feedback
  • Loading branch information
mat-mcloughlin authored Aug 5, 2021
2 parents a552bc5 + e16e74f commit 962416a
Show file tree
Hide file tree
Showing 43 changed files with 124 additions and 140 deletions.
33 changes: 17 additions & 16 deletions CQRS_Flow/.NET/Carts/Carts.Api/Controllers/CartsController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Carts.Api.Requests.Carts;
using Carts.Carts.GettingCartAtVersion;
Expand Down Expand Up @@ -32,7 +33,7 @@ public CartsController(
}

[HttpPost]
public async Task<IActionResult> InitializeCart([FromBody] InitializeCartRequest? request)
public async Task<IActionResult> InitializeCart([FromBody] InitializeCartRequest? request, CancellationToken ct)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
Expand All @@ -44,13 +45,13 @@ public async Task<IActionResult> InitializeCart([FromBody] InitializeCartRequest
request.ClientId
);

await commandBus.Send(command);
await commandBus.Send(command, ct);

return Created("api/Carts", cartId);
}

[HttpPost("{id}/products")]
public async Task<IActionResult> AddProduct(Guid id, [FromBody] AddProductRequest? request)
public async Task<IActionResult> AddProduct(Guid id, [FromBody] AddProductRequest? request, CancellationToken ct)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
Expand All @@ -63,13 +64,13 @@ public async Task<IActionResult> AddProduct(Guid id, [FromBody] AddProductReques
)
);

await commandBus.Send(command);
await commandBus.Send(command, ct);

return Ok();
}

[HttpDelete("{id}/products")]
public async Task<IActionResult> RemoveProduct(Guid id, [FromBody] RemoveProductRequest? request)
public async Task<IActionResult> RemoveProduct(Guid id, [FromBody] RemoveProductRequest? request, CancellationToken ct)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
Expand All @@ -83,49 +84,49 @@ public async Task<IActionResult> RemoveProduct(Guid id, [FromBody] RemoveProduct
)
);

await commandBus.Send(command);
await commandBus.Send(command, ct);

return Ok();
}

[HttpPut("{id}/confirmation")]
public async Task<IActionResult> ConfirmCart(Guid id)
public async Task<IActionResult> ConfirmCart(Guid id, CancellationToken ct)
{
var command = Carts.ConfirmingCart.ConfirmCart.Create(
id
);

await commandBus.Send(command);
await commandBus.Send(command, ct);

return Ok();
}

[HttpGet("{id}")]
public Task<CartDetails> Get(Guid id)
public Task<CartDetails> Get(Guid id, CancellationToken ct)
{
return queryBus.Send<GetCartById, CartDetails>(GetCartById.Create(id));
return queryBus.Send<GetCartById, CartDetails>(GetCartById.Create(id), ct);
}

[HttpGet]
public Task<IReadOnlyList<CartShortInfo>> Get([FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 20)
public Task<IReadOnlyList<CartShortInfo>> Get(CancellationToken ct, [FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 20)
{
return queryBus.Send<GetCarts, IReadOnlyList<CartShortInfo>>(GetCarts.Create(pageNumber, pageSize));
return queryBus.Send<GetCarts, IReadOnlyList<CartShortInfo>>(GetCarts.Create(pageNumber, pageSize), ct);
}


[HttpGet("{id}/history")]
public Task<IReadOnlyList<CartHistory>> GetHistory(Guid id)
public Task<IReadOnlyList<CartHistory>> GetHistory(Guid id, CancellationToken ct)
{
return queryBus.Send<GetCartHistory, IReadOnlyList<CartHistory>>(GetCartHistory.Create(id));
return queryBus.Send<GetCartHistory, IReadOnlyList<CartHistory>>(GetCartHistory.Create(id), ct);
}

[HttpGet("{id}/versions")]
public Task<CartDetails> GetVersion(Guid id, [FromQuery] GetCartAtVersion? query)
public Task<CartDetails> GetVersion(Guid id, [FromQuery] GetCartAtVersion? query, CancellationToken ct)
{
if (query == null)
throw new ArgumentNullException(nameof(query));

return queryBus.Send<GetCartAtVersion, CartDetails>(GetCartAtVersion.Create(id, query.Version));
return queryBus.Send<GetCartAtVersion, CartDetails>(GetCartAtVersion.Create(id, query.Version), ct);
}
}
}
2 changes: 1 addition & 1 deletion CQRS_Flow/.NET/Carts/Carts.Tests/Builders/CartBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Carts.Tests.Builders
{
internal class CartBuilder
{
private readonly Queue<IEvent> eventsToApply = new();
private readonly Queue<object> eventsToApply = new();

public CartBuilder Initialized()
{
Expand Down
5 changes: 2 additions & 3 deletions CQRS_Flow/.NET/Carts/Carts/Carts/AddingProduct/AddProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
using Carts.Pricing;
using Core.Commands;
using Core.Repositories;
using MediatR;

namespace Carts.Carts.AddingProduct
{
public class AddProduct: ICommand
public class AddProduct
{
public Guid CartId { get; }

Expand Down Expand Up @@ -44,7 +43,7 @@ IProductPriceCalculator productPriceCalculator
this.productPriceCalculator = productPriceCalculator;
}

public Task<Unit> Handle(AddProduct command, CancellationToken cancellationToken)
public Task Handle(AddProduct command, CancellationToken cancellationToken)
{
return cartRepository.GetAndUpdate(
command.CartId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Carts.Carts.AddingProduct
{
public class ProductAdded: IEvent
public class ProductAdded
{
public Guid CartId { get; }

Expand Down
1 change: 0 additions & 1 deletion CQRS_Flow/.NET/Carts/Carts/Carts/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Core.EventStoreDB.Repository;
using Core.Queries;
using Core.Repositories;
using MediatR;
using Microsoft.Extensions.DependencyInjection;

namespace Carts.Carts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Carts.Carts.ConfirmingCart
{
public class CartConfirmed: IEvent
public class CartConfirmed
{
public Guid CartId { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
using System.Threading.Tasks;
using Core.Commands;
using Core.Repositories;
using MediatR;

namespace Carts.Carts.ConfirmingCart
{
public class ConfirmCart: ICommand
public class ConfirmCart
{
public Guid CartId { get; }

Expand Down Expand Up @@ -37,7 +36,7 @@ IRepository<Cart> cartRepository
this.cartRepository = cartRepository;
}

public Task<Unit> Handle(ConfirmCart command, CancellationToken cancellationToken)
public Task Handle(ConfirmCart command, CancellationToken cancellationToken)
{
return cartRepository.GetAndUpdate(
command.CartId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Carts.Carts.GettingCartAtVersion
{
public class GetCartAtVersion : IQuery<CartDetails>
public class GetCartAtVersion
{
public Guid CartId { get; }
public ulong Version { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Carts.Carts.GettingCartById
{
public class GetCartById : IQuery<CartDetails>
public class GetCartById
{
public Guid CartId { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Carts.Carts.GettingCartHistory
{
public class GetCartHistory: IQuery<IReadOnlyList<CartHistory>>
public class GetCartHistory
{
public Guid CartId { get; }
public int PageNumber { get; }
Expand Down
2 changes: 1 addition & 1 deletion CQRS_Flow/.NET/Carts/Carts/Carts/GettingCarts/GetCarts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Carts.Carts.GettingCarts
{
public class GetCarts : IQuery<IReadOnlyList<CartShortInfo>>
public class GetCarts
{
public int PageNumber { get; }
public int PageSize { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Carts.Carts.InitializingCart
{
public class CartInitialized: IEvent
public class CartInitialized
{
public Guid CartId { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
using System.Threading.Tasks;
using Core.Commands;
using Core.Repositories;
using MediatR;

namespace Carts.Carts.InitializingCart
{
public class InitializeCart: ICommand
public class InitializeCart
{
public Guid CartId { get; }

Expand Down Expand Up @@ -42,13 +41,11 @@ IRepository<Cart> cartRepository
this.cartRepository = cartRepository;
}

public async Task<Unit> Handle(InitializeCart command, CancellationToken cancellationToken)
public async Task Handle(InitializeCart command, CancellationToken cancellationToken)
{
var cart = Cart.Initialize(command.CartId, command.ClientId);

await cartRepository.Add(cart, cancellationToken);

return Unit.Value;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using Carts.Carts.Products;
using Core.Events;

namespace Carts.Carts.RemovingProduct
{
public class ProductRemoved: IEvent
public class ProductRemoved
{
public Guid CartId { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
using Carts.Carts.Products;
using Core.Commands;
using Core.Repositories;
using MediatR;

namespace Carts.Carts.RemovingProduct
{
public class RemoveProduct: ICommand
public class RemoveProduct
{
public Guid CartId { get; }

Expand Down Expand Up @@ -38,7 +37,7 @@ IRepository<Cart> cartRepository
this.cartRepository = cartRepository;
}

public Task<Unit> Handle(RemoveProduct command, CancellationToken cancellationToken)
public Task Handle(RemoveProduct command, CancellationToken cancellationToken)
{
return cartRepository.GetAndUpdate(
command.CartId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
using Core.ElasticSearch.Indices;
using Core.Events;
using Core.Projections;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Nest;

namespace Core.ElasticSearch.Projections
{
public class ElasticSearchProjection<TEvent, TView> : IEventHandler<TEvent>
where TView : class, IProjection
where TEvent : IEvent
where TEvent : notnull
{
private readonly IElasticClient elasticClient;
private readonly Func<TEvent, string> getId;
Expand All @@ -35,7 +34,7 @@ public async Task Handle(TEvent @event, CancellationToken ct)

entity.When(@event);

var result = await elasticClient.UpdateAsync<TView>(id,
await elasticClient.UpdateAsync<TView>(id,
u => u.Doc(entity).Upsert(entity).Index(IndexNameMapper.ToIndexName<TView>()),
ct
);
Expand All @@ -47,9 +46,9 @@ public static class ElasticSearchProjectionConfig
public static IServiceCollection Project<TEvent, TView>(this IServiceCollection services,
Func<TEvent, string> getId)
where TView : class, IProjection
where TEvent : IEvent
where TEvent : notnull
{
services.AddTransient<INotificationHandler<TEvent>>(sp =>
services.AddTransient<IEventHandler<TEvent>>(sp =>
{
var session = sp.GetRequiredService<IElasticClient>();
Expand All @@ -59,4 +58,4 @@ public static IServiceCollection Project<TEvent, TView>(this IServiceCollection
return services;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static object Deserialize(this ResolvedEvent resolvedEvent)
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(resolvedEvent.Event.Data.Span), eventType!)!;
}

public static EventData ToJsonEventData(this IEvent @event) =>
public static EventData ToJsonEventData(this object @event) =>
new(
Uuid.NewUuid(),
EventTypeMapper.ToName(@event.GetType()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Core.EventStoreDB.Subscriptions
{
public record CheckpointStored(string SubscriptionId, ulong? Position, DateTime CheckpointedAt): IEvent;
public record CheckpointStored(string SubscriptionId, ulong? Position, DateTime CheckpointedAt);

public class EventStoreDBSubscriptionCheckpointRepository: ISubscriptionCheckpointRepository
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private async Task HandleEvent(StreamSubscription subscription, ResolvedEvent re
scope.ServiceProvider.GetRequiredService<IEventBus>();

// publish event to internal event bus
await eventBus.Publish((IEvent)resolvedEvent.Deserialize());
await eventBus.Publish(resolvedEvent.Deserialize(), ct);

await checkpointRepository.Store(subscriptionId, resolvedEvent.Event.Position.CommitPosition, ct);
}
Expand Down
2 changes: 1 addition & 1 deletion CQRS_Flow/.NET/Core/Core.Testing/AggregateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Core.Testing
{
public static class AggregateExtensions
{
public static T? PublishedEvent<T>(this IAggregate aggregate) where T : class, IEvent
public static T? PublishedEvent<T>(this IAggregate aggregate) where T : class
{
return aggregate.DequeueUncommittedEvents().LastOrDefault() as T;
}
Expand Down
2 changes: 1 addition & 1 deletion CQRS_Flow/.NET/Core/Core.Testing/Core.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.7" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion CQRS_Flow/.NET/Core/Core.Testing/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using MediatR;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
Expand Down
Loading

0 comments on commit 962416a

Please sign in to comment.