Skip to content

Commit

Permalink
#1 Add MassTransit and RabbitMq
Browse files Browse the repository at this point in the history
  • Loading branch information
nkz-soft committed Oct 24, 2022
1 parent 42162ac commit 5c4fa67
Show file tree
Hide file tree
Showing 80 changed files with 524 additions and 314 deletions.
14 changes: 14 additions & 0 deletions NKZSoft.MicroserviceTemplate.sln
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NKZSoft.Template.Presentati
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NKZSoft.Template.Presentation.GraphQL.Tests", "tests\NKZSoft.Template.Presentation.GraphQL.Tests\NKZSoft.Template.Presentation.GraphQL.Tests.csproj", "{EBF07FE3-4F11-4EF2-AF4E-5F748BAB5AA1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NKZSoft.Template.Events", "src\NKZSoft.Template.Events\NKZSoft.Template.Events.csproj", "{D02A6791-818B-4BFA-A958-65D933FC7F2E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NKZSoft.Template.MessageBrokers.RabbitMq", "src\NKZSoft.Template.MessageBrokers.RabbitMq\NKZSoft.Template.MessageBrokers.RabbitMq.csproj", "{748E8208-2140-40FB-BDD2-145F27C762BC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -125,6 +129,14 @@ Global
{EBF07FE3-4F11-4EF2-AF4E-5F748BAB5AA1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBF07FE3-4F11-4EF2-AF4E-5F748BAB5AA1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBF07FE3-4F11-4EF2-AF4E-5F748BAB5AA1}.Release|Any CPU.Build.0 = Release|Any CPU
{D02A6791-818B-4BFA-A958-65D933FC7F2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D02A6791-818B-4BFA-A958-65D933FC7F2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D02A6791-818B-4BFA-A958-65D933FC7F2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D02A6791-818B-4BFA-A958-65D933FC7F2E}.Release|Any CPU.Build.0 = Release|Any CPU
{748E8208-2140-40FB-BDD2-145F27C762BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{748E8208-2140-40FB-BDD2-145F27C762BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{748E8208-2140-40FB-BDD2-145F27C762BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{748E8208-2140-40FB-BDD2-145F27C762BC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -151,6 +163,8 @@ Global
{D62D0A2D-B69C-40AE-AC5C-94E9A6D5F5E8} = {664D406C-2F83-48F0-BFC3-408D5CB53C65}
{4DA2FA43-758F-4BB1-B34B-EF652DBDFE92} = {19AA42FA-3860-4D08-B5B0-9D26D751D5B8}
{EBF07FE3-4F11-4EF2-AF4E-5F748BAB5AA1} = {664D406C-2F83-48F0-BFC3-408D5CB53C65}
{D02A6791-818B-4BFA-A958-65D933FC7F2E} = {D707344C-5732-4DE6-92B5-16B0FCE6016F}
{748E8208-2140-40FB-BDD2-145F27C762BC} = {315E54AD-1A73-4CBD-8322-59C226A38724}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3CB609D9-5D54-4C11-A371-DAAC8B74E430}
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ I would like this project to help you find simplified and effortless solutions.
- [x] GRPC API samples
- [x] Improve integration tests to use Testcontainers
- [x] GraphQL API samples
- [x] Add MassTransit and RabbitMq
- [ ] OpenTelemetry
- [ ] HealthCheck
- [ ] Add caching for EF Core
- [ ] MongoDB data provider
- [ ] MassTransit and RabbitMQ

## Technologies used

Expand All @@ -49,3 +51,7 @@ I would like this project to help you find simplified and effortless solutions.
[protobuf-net](https://github.com/protobuf-net/protobuf-net): protobuf-net is a contract based serializer for .NET code, that happens to write data in the "protocol buffers" serialization format engineered by Google.

[hotchocolate](https://github.com/ChilliCream/hotchocolate): A GraphQL server to create GraphQL endpoints and merge schemas.

[MassTransit](https://github.com/MassTransit/MassTransit): MassTransit is a free, open-source distributed application framework for .NET.

[FluentResults](https://github.com/altmann/FluentResults): FluentResults is a lightweight .NET library developed to solve a common problem. It returns an object indicating success or failure of an operation instead of throwing/using exceptions.
21 changes: 20 additions & 1 deletion deployment/docker/docker-compose-infrastructure.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: '3.9'

volumes:
postgres:
rabbitmq:

services:

Expand All @@ -18,7 +19,25 @@ services:
healthcheck:
test: ["CMD-SHELL", "PGPASSWORD=postgres pg_isready -U postgres -h postgres"]
interval: 10s
timeout: 5s
timeout: 30s
retries: 15

rabbitmq:
image: rabbitmq:3.11-management
environment:
RABBITMQ_DEFAULT_USER: rabbitmq
RABBITMQ_DEFAULT_PASS: rabbitmq
ports:
- 0.0.0.0:5672:5672
- 0.0.0.0:15672:15672
networks:
- dev-network
volumes:
- rabbitmq:/var/lib/rabbitmq
healthcheck:
test: rabbitmq-diagnostics -q ping
interval: 10s
timeout: 30s
retries: 15

networks:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel.DataAnnotations.Schema;
using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;

namespace NKZSoft.Template.Application.Common.Mappings;

Expand Down
10 changes: 4 additions & 6 deletions src/NKZSoft.Template.Application/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
global using System.Linq.Expressions;
global using Ardalis.Specification;
global using Ardalis.Specification.EntityFrameworkCore;

global using MassTransit;
global using NKZSoft.Template.Common;
global using NKZSoft.Template.Application.Common.Exceptions;
global using NKZSoft.Template.Application.Common.Filters;
global using NKZSoft.Template.Application.Common.Paging;
global using NKZSoft.Template.Application.Common.Handlers;
global using NKZSoft.Template.Application.Common.Interfaces;

global using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;
global using NKZSoft.Template.Domain.Events;
global using NKZSoft.Template.Events.Event.ToDoItem.Create;
global using NKZSoft.Template.Events.Event.ToDoItem.Update;
6 changes: 3 additions & 3 deletions src/NKZSoft.Template.Application/Mapper/MappingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;

namespace NKZSoft.Template.Application.Mapper;
namespace NKZSoft.Template.Application.Mapper;

using Models;

Expand All @@ -10,5 +8,7 @@ public void Register(TypeAdapterConfig config)
{
config.NewConfig<ToDoItem, ToDoItemDto>();
config.NewConfig<ToDoList, ToDoListDto>();

config.NewConfig<ToDoItemCreatedDomainEvent, ToDoItemCreatedIntegrationEvent>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\NKZSoft.Template.Common\NKZSoft.Template.Common.csproj" />
<ProjectReference Include="..\NKZSoft.Template.Domain\NKZSoft.Template.Domain.csproj" />
<ProjectReference Include="..\NKZSoft.Template.MessageBrokers.RabbitMq\NKZSoft.Template.MessageBrokers.RabbitMq.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;

namespace NKZSoft.Template.Application.TodoItems.Commands.Create;
namespace NKZSoft.Template.Application.TodoItems.Commands.Create;

using Common.Interfaces;

public sealed class CreateTodoItemCommandHandler : IRequestHandler<CreateToВoItemCommand, IResult<Guid>>
public sealed class CreateTodoItemCommandHandler : IRequestHandler<CreateToВoItemCommand, Result<Guid>>
{
private readonly IApplicationDbContext _context;

public CreateTodoItemCommandHandler(IApplicationDbContext context) => _context = context;

public async Task<IResult<Guid>> Handle(CreateToВoItemCommand request, CancellationToken cancellationToken)
public async Task<Result<Guid>> Handle(CreateToВoItemCommand request, CancellationToken cancellationToken)
{
var entity = new ToDoItem(request.Title);

await _context.Set<ToDoItem>().AddAsync(entity, cancellationToken);
await _context.Set<ToDoItem>()
.AddAsync(entity, cancellationToken)
.ConfigureAwait(false);

await _context.SaveChangesAsync(cancellationToken);
await _context.SaveChangesAsync(cancellationToken)
.ConfigureAwait(false);

return Result.Ok(entity.Id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace NKZSoft.Template.Application.TodoItems.Commands.Create;

public sealed record CreateToВoItemCommand(string Title, int? ListId) : IRequest<IResult<Guid>>;
public sealed record CreateToВoItemCommand(string Title, int? ListId) : IRequest<Result<Guid>>;
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;

namespace NKZSoft.Template.Application.TodoItems.Commands.Delete;
namespace NKZSoft.Template.Application.TodoItems.Commands.Delete;

using Common.Exceptions;
using Common.Interfaces;
Expand All @@ -9,23 +7,22 @@ public sealed class DeleteTodoItemCommandHandler : IRequestHandler<DeleteTodoIte
{
private readonly IApplicationDbContext _context;

public DeleteTodoItemCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public DeleteTodoItemCommandHandler(IApplicationDbContext context) => _context = context;

public async Task<Unit> Handle(DeleteTodoItemCommand request, CancellationToken cancellationToken)
{
var entity = await _context.Set<ToDoItem>()
.FindAsync(new object[] { request.Id }, cancellationToken);
.FindAsync(new object[] { request.Id }, cancellationToken)
.ConfigureAwait(false);

if (entity == null)
{
throw new NotFoundException(nameof(ToDoItem), request.Id);
}

_context.Set<ToDoItem>().Remove(entity);
await _context.SaveChangesAsync(cancellationToken);
await _context.SaveChangesAsync(cancellationToken)
.ConfigureAwait(false);

return Unit.Value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;

namespace NKZSoft.Template.Application.TodoItems.Commands.Update;
namespace NKZSoft.Template.Application.TodoItems.Commands.Update;

using Common.Exceptions;
using Common.Interfaces;
Expand All @@ -17,7 +15,8 @@ public UpdateTodoItemCommandHandler(IApplicationDbContext context)
public async Task<Unit> Handle(UpdateTodoItemCommand request, CancellationToken cancellationToken)
{
var entity = await _context.Set<ToDoItem>()
.FindAsync(new object[] { request.Id }, cancellationToken);
.FindAsync(new object[] { request.Id }, cancellationToken)
.ConfigureAwait(false);

if (entity == null)
{
Expand All @@ -26,7 +25,8 @@ public async Task<Unit> Handle(UpdateTodoItemCommand request, CancellationToken

entity.Update(request.Title, request.Description);

await _context.SaveChangesAsync(cancellationToken);
await _context.SaveChangesAsync(cancellationToken)
.ConfigureAwait(false);

return Unit.Value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace NKZSoft.Template.Application.TodoItems.EventHandlers;

public sealed class ToDoItemCreatedDomainEventBusHandler : INotificationHandler<ToDoItemCreatedDomainEvent>
{
private readonly ILogger<ToDoItemCreatedDomainEventBusHandler> _logger;
private readonly IPublishEndpoint _publishEndpoint;
private readonly IMapper _mapper;

public ToDoItemCreatedDomainEventBusHandler(ILogger<ToDoItemCreatedDomainEventBusHandler> logger,
IPublishEndpoint publishEndpoint, IMapper mapper)
{
_mapper = mapper.ThrowIfNull();
_logger = logger.ThrowIfNull();
_publishEndpoint = publishEndpoint.ThrowIfNull();
}

public async Task Handle(ToDoItemCreatedDomainEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);

var createEvent = await notification
.BuildAdapter(_mapper.Config)
.AdaptToTypeAsync<ToDoItemCreatedIntegrationEvent>()
.ConfigureAwait(false);

await _publishEndpoint.Publish(createEvent, cancellationToken)
.ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NKZSoft.Template.Application.TodoItems.EventHandlers;

public sealed class ToDoItemUpdatedDomainEventBusHandler : INotificationHandler<ToDoItemUpdatedDomainEvent>
{
private readonly ILogger<ToDoItemUpdatedDomainEventBusHandler> _logger;

public ToDoItemUpdatedDomainEventBusHandler(ILogger<ToDoItemUpdatedDomainEventBusHandler> logger) => _logger = logger;

public async Task Handle(ToDoItemUpdatedDomainEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("CleanArchitecture Domain Event: {DomainEvent}", notification.GetType().Name);

await Task.CompletedTask;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace NKZSoft.Template.Application.TodoItems.Queries.Get;

using Domain.AggregatesModel.ToDoAggregates.Entities;
using Application.Models;
using Common.Exceptions;
using Common.Handlers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using NKZSoft.Template.Application.Models;
using Specifications;
using Domain.AggregatesModel.ToDoAggregates.Entities;
using Common.Handlers;
using Common.Interfaces;
using Common.Paging;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace NKZSoft.Template.Application.TodoItems.Queries.GetQueryable;

using Common.Handlers;
using Common.Interfaces;

public class GetQueryableQueryHandler : HandlerQueryBase<GetQueryableQuery, IQueryable<ToDoItem>>
{
public GetQueryableQueryHandler(IApplicationDbContext applicationDbContext, IMapper mapper, ICurrentUserService currentUserService)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;

namespace NKZSoft.Template.Application.TodoItems.Specifications;
namespace NKZSoft.Template.Application.TodoItems.Specifications;

using Common.Exceptions;
using Common.Filters;
using Common.Paging;
using Models;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;

using Common;
using Events;

public sealed class ToDoItem : BaseAuditableEntity, IAggregateRoot
{
Expand All @@ -15,8 +16,11 @@ public ToDoItem(string title) : this(title, null)

public ToDoItem(string title, string? note)
{
Id = Guid.NewGuid();
Title = title;
Note = note;

AddCreateDomainEvent();
}

public string Title { get; set; }
Expand All @@ -28,4 +32,10 @@ public void Update(string title, string note)
Title = title;
Note = note;
}

private void AddCreateDomainEvent()
{
var createEvent = new ToDoItemCreatedDomainEvent(Id, Title, Note);
AddDomainEvent(createEvent);
}
}
Loading

0 comments on commit 5c4fa67

Please sign in to comment.