Skip to content

Commit

Permalink
Removed MediatR library to not have redundant dependency from the ext…
Browse files Browse the repository at this point in the history
…ernal package (no advanced features like pipelines were used)
  • Loading branch information
oskardudycz committed Aug 4, 2021
1 parent a552bc5 commit cdc1423
Show file tree
Hide file tree
Showing 29 changed files with 110 additions and 98 deletions.
33 changes: 17 additions & 16 deletions CQRS_Flow/.NET/Carts/Carts.Api/Controllers/CartsController.cs
@@ -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 class CartsController: Controller
}

[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);
}
}
}
3 changes: 1 addition & 2 deletions CQRS_Flow/.NET/Carts/Carts/Carts/AddingProduct/AddProduct.cs
Expand Up @@ -5,7 +5,6 @@
using Carts.Pricing;
using Core.Commands;
using Core.Repositories;
using MediatR;

namespace Carts.Carts.AddingProduct
{
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
1 change: 0 additions & 1 deletion CQRS_Flow/.NET/Carts/Carts/Carts/Config.cs
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
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using Core.Commands;
using Core.Repositories;
using MediatR;

namespace Carts.Carts.ConfirmingCart
{
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
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using Core.Commands;
using Core.Repositories;
using MediatR;

namespace Carts.Carts.InitializingCart
{
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;
}
}
}
Expand Up @@ -4,7 +4,6 @@
using Carts.Carts.Products;
using Core.Commands;
using Core.Repositories;
using MediatR;

namespace Carts.Carts.RemovingProduct
{
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
Expand Up @@ -4,7 +4,6 @@
using Core.ElasticSearch.Indices;
using Core.Events;
using Core.Projections;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Nest;

Expand Down Expand Up @@ -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 @@ -49,7 +48,7 @@ public static class ElasticSearchProjectionConfig
where TView : class, IProjection
where TEvent : IEvent
{
services.AddTransient<INotificationHandler<TEvent>>(sp =>
services.AddTransient<IEventHandler<TEvent>>(sp =>
{
var session = sp.GetRequiredService<IElasticClient>();
Expand All @@ -59,4 +58,4 @@ public static class ElasticSearchProjectionConfig
return services;
}
}
}
}
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/Core.Testing.csproj
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
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
15 changes: 9 additions & 6 deletions CQRS_Flow/.NET/Core/Core/Commands/CommandBus.cs
@@ -1,20 +1,23 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.DependencyInjection;

namespace Core.Commands
{
public class CommandBus: ICommandBus
{
private readonly IMediator mediator;
private readonly IServiceProvider serviceProvider;

public CommandBus(IMediator mediator)
public CommandBus(IServiceProvider serviceProvider)
{
this.mediator = mediator;
this.serviceProvider = serviceProvider;
}

public Task Send<TCommand>(TCommand command) where TCommand : ICommand
public Task Send<TCommand>(TCommand command, CancellationToken ct) where TCommand : ICommand
{
return mediator.Send(command);
var commandHandler = serviceProvider.GetRequiredService<ICommandHandler<TCommand>>();
return commandHandler.Handle(command, ct);
}
}
}
4 changes: 1 addition & 3 deletions CQRS_Flow/.NET/Core/Core/Commands/Config.cs
@@ -1,5 +1,4 @@
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

namespace Core.Commands
{
Expand All @@ -12,7 +11,6 @@ public static class Config
where TCommandHandler : class, ICommandHandler<TCommand>
{
return services.AddTransient<TCommandHandler>()
.AddTransient<IRequestHandler<TCommand, Unit>>(sp => sp.GetRequiredService<TCommandHandler>())
.AddTransient<ICommandHandler<TCommand>>(sp => sp.GetRequiredService<TCommandHandler>());
}
}
Expand Down
6 changes: 3 additions & 3 deletions CQRS_Flow/.NET/Core/Core/Commands/ICommand.cs
@@ -1,6 +1,6 @@
using MediatR;

namespace Core.Commands
{
public interface ICommand: IRequest { }
public interface ICommand
{
}
}
3 changes: 2 additions & 1 deletion CQRS_Flow/.NET/Core/Core/Commands/ICommandBus.cs
@@ -1,9 +1,10 @@
using System.Threading;
using System.Threading.Tasks;

namespace Core.Commands
{
public interface ICommandBus
{
Task Send<TCommand>(TCommand command) where TCommand : ICommand;
Task Send<TCommand>(TCommand command, CancellationToken ct) where TCommand : ICommand;
}
}
8 changes: 5 additions & 3 deletions CQRS_Flow/.NET/Core/Core/Commands/ICommandHandler.cs
@@ -1,9 +1,11 @@
using MediatR;
using System.Threading;
using System.Threading.Tasks;

namespace Core.Commands
{
public interface ICommandHandler<in T>: IRequestHandler<T>
where T : ICommand
public interface ICommandHandler<in TCommand>
where TCommand : ICommand
{
Task Handle(TCommand command, CancellationToken ct);
}
}
9 changes: 1 addition & 8 deletions CQRS_Flow/.NET/Core/Core/Config.cs
Expand Up @@ -2,7 +2,6 @@
using Core.Events;
using Core.Ids;
using Core.Queries;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

Expand All @@ -12,7 +11,7 @@ public static class Config
{
public static IServiceCollection AddCoreServices(this IServiceCollection services)
{
services.AddMediatR()
services
.AddScoped<ICommandBus, CommandBus>()
.AddScoped<IQueryBus, QueryBus>();

Expand All @@ -21,11 +20,5 @@ public static IServiceCollection AddCoreServices(this IServiceCollection service

return services;
}

private static IServiceCollection AddMediatR(this IServiceCollection services)
{
return services.AddScoped<IMediator, Mediator>()
.AddTransient<ServiceFactory>(sp => sp.GetRequiredService!);
}
}
}
2 changes: 0 additions & 2 deletions CQRS_Flow/.NET/Core/Core/Core.csproj
Expand Up @@ -7,8 +7,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="MediatR" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
</ItemGroup>

Expand Down
4 changes: 1 addition & 3 deletions CQRS_Flow/.NET/Core/Core/Events/Config.cs
@@ -1,5 +1,4 @@
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

namespace Core.Events
{
Expand All @@ -12,7 +11,6 @@ public static class Config
where TEventHandler : class, IEventHandler<TEvent>
{
return services.AddTransient<TEventHandler>()
.AddTransient<INotificationHandler<TEvent>>(sp => sp.GetRequiredService<TEventHandler>())
.AddTransient<IEventHandler<TEvent>>(sp => sp.GetRequiredService<TEventHandler>());
}
}
Expand Down

0 comments on commit cdc1423

Please sign in to comment.