Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,17 @@ The one deliberate break from Clean Architecture is that the Presentation projec

### Project structure

1. **Presentation** - Exposes the Application layer to AI clients as an MCP server over HTTP. Tools are grouped by feature, validate their input and hand the work to MediatR.
1. **Presentation** - Exposes the Application layer to AI clients as an MCP server over HTTP. Tools are grouped by feature, send commands and queries through Mediator, and turn Application errors into MCP tool errors.
1. **Application** - Owns the domain and business logic. Commands and queries are validated and handled here, and each domain type has its own repository interface. This layer doesn't care what fulfils those contracts, as long as something does.
1. **Infrastructure** - Owns the data. It implements the repository interfaces to fetch, create, update and remove data, and maps its own data models onto the Application entities so persistence details don't leak upwards.

## Features

- MCP server using the [MCP C# SDK](https://github.com/modelcontextprotocol/csharp-sdk)
- Logging using [Serilog](https://github.com/serilog/serilog), with [Seq](https://datalust.co/seq) when running in Docker
- Mediator pattern using [MediatR](https://github.com/jbogard/MediatR)
- Validation using [FluentValidation](https://github.com/FluentValidation/FluentValidation)
- Mediator pattern using [Mediator](https://github.com/martinothamar/Mediator)
- Validation using [FluentValidation](https://github.com/FluentValidation/FluentValidation) in an Application pipeline behaviour
- Tool errors mapped from Application exceptions by a single call-tool filter
- Object mapping using [Mapperly](https://mapperly.riok.app)
- In-memory data using [Entity Framework Core](https://learn.microsoft.com/ef/core/providers/in-memory/) seeded with [Bogus](https://github.com/bchavez/Bogus)
- Testing using [xUnit](https://xunit.net), [Shouldly](https://github.com/shouldly/shouldly) and [NSubstitute](https://github.com/nsubstitute/NSubstitute), with coverage from [Coverlet](https://github.com/coverlet-coverage/coverlet)
Expand Down
3 changes: 2 additions & 1 deletion src/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.1.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="12.1.1" />
<PackageReference Include="Mediator.Abstractions" Version="3.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace CleanModelContextProtocol.Application.Authors.Queries.GetAuthorById;
using Common.Enums;
using Common.Exceptions;
using Entities;
using MediatR;
using Mediator;

public class GetAuthorByIdHandler(IAuthorsRepository repository) : IRequestHandler<GetAuthorByIdQuery, Author>
{
public async Task<Author> Handle(GetAuthorByIdQuery request, CancellationToken cancellationToken)
public async ValueTask<Author> Handle(GetAuthorByIdQuery request, CancellationToken cancellationToken)
{
var result = await repository.GetAuthorById(request.Id, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace CleanModelContextProtocol.Application.Authors.Queries.GetAuthorById;

using System.ComponentModel.DataAnnotations;
using Entities;
using MediatR;
using Mediator;

public class GetAuthorByIdQuery : IRequest<Author>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CleanModelContextProtocol.Application.Authors.Queries.GetAuthorById;

using FluentValidation;

public class GetAuthorByIdValidator : AbstractValidator<GetAuthorByIdQuery>
{
public GetAuthorByIdValidator()
{
_ = this.RuleFor(r => r.Id).NotNull().NotEqual(Guid.Empty).WithMessage("An Author Id was not supplied.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ namespace CleanModelContextProtocol.Application.Authors.Queries.GetAuthors;
using System.Threading;
using System.Threading.Tasks;
using Entities;
using MediatR;
using Mediator;

public class GetAuthorsHandler(IAuthorsRepository repository) : IRequestHandler<GetAuthorsQuery, List<Author>>
{
public async Task<List<Author>> Handle(GetAuthorsQuery request, CancellationToken cancellationToken)
public async ValueTask<List<Author>> Handle(GetAuthorsQuery request, CancellationToken cancellationToken)
{
return await repository.GetAuthors(cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace CleanModelContextProtocol.Application.Authors.Queries.GetAuthors;

using Entities;
using MediatR;
using Mediator;

public class GetAuthorsQuery : IRequest<List<Author>>
{
Expand Down
29 changes: 29 additions & 0 deletions src/Application/Common/Behaviours/ValidationBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace CleanModelContextProtocol.Application.Common.Behaviours;

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FluentValidation;
using Mediator;

public class ValidationBehaviour<TMessage, TResponse>(IEnumerable<IValidator<TMessage>> validators) : IPipelineBehavior<TMessage, TResponse>
where TMessage : IMessage
{
public async ValueTask<TResponse> Handle(TMessage message, MessageHandlerDelegate<TMessage, TResponse> next, CancellationToken cancellationToken)
{
if (validators.Any())
{
var context = new ValidationContext<TMessage>(message);
var validationResults = await Task.WhenAll(validators.Select(v => v.ValidateAsync(context, cancellationToken)));
var failures = validationResults.SelectMany(r => r.Errors).Where(f => f != null).ToList();

if (failures.Count != 0)
{
throw new ValidationException(failures);
}
}

return await next(message, cancellationToken);
}
}
7 changes: 5 additions & 2 deletions src/Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ namespace CleanModelContextProtocol.Application;

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using MediatR;
using Common.Behaviours;
using FluentValidation;
using Mediator;
using Microsoft.Extensions.DependencyInjection;

[ExcludeFromCodeCoverage]
public static class DependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services)
{
_ = services.AddMediatR(Assembly.GetExecutingAssembly());
_ = services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly(), ServiceLifetime.Transient);
_ = services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace CleanModelContextProtocol.Application.Movies.Queries.GetMovieById;
using Common.Enums;
using Common.Exceptions;
using Entities;
using MediatR;
using Mediator;

public class GetMovieByIdHandler(IMoviesRepository repository) : IRequestHandler<GetMovieByIdQuery, Movie>
{
public async Task<Movie> Handle(GetMovieByIdQuery request, CancellationToken cancellationToken)
public async ValueTask<Movie> Handle(GetMovieByIdQuery request, CancellationToken cancellationToken)
{
var result = await repository.GetMovieById(request.Id, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace CleanModelContextProtocol.Application.Movies.Queries.GetMovieById;

using System.ComponentModel.DataAnnotations;
using Entities;
using MediatR;
using Mediator;

public class GetMovieByIdQuery : IRequest<Movie>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CleanModelContextProtocol.Application.Movies.Queries.GetMovieById;

using FluentValidation;

public class GetMovieByIdValidator : AbstractValidator<GetMovieByIdQuery>
{
public GetMovieByIdValidator()
{
_ = this.RuleFor(r => r.Id).NotEqual(Guid.Empty).WithMessage("A Movie Id was not supplied.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ namespace CleanModelContextProtocol.Application.Movies.Queries.GetMovies;
using System.Threading;
using System.Threading.Tasks;
using Entities;
using MediatR;
using Mediator;

public class GetMoviesHandler(IMoviesRepository repository) : IRequestHandler<GetMoviesQuery, List<Movie>>
{
public async Task<List<Movie>> Handle(GetMoviesQuery request, CancellationToken cancellationToken)
public async ValueTask<List<Movie>> Handle(GetMoviesQuery request, CancellationToken cancellationToken)
{
return await repository.GetMovies(cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace CleanModelContextProtocol.Application.Movies.Queries.GetMovies;

using Entities;
using MediatR;
using Mediator;

public class GetMoviesQuery : IRequest<List<Movie>>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace CleanModelContextProtocol.Application.Reviews.Commands.CreateReview;

using Entities;
using MediatR;
using Mediator;

public class CreateReviewCommand : IRequest<Review>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace CleanModelContextProtocol.Application.Reviews.Commands.CreateReview;
using Common.Enums;
using Common.Exceptions;
using Entities;
using MediatR;
using Mediator;
using Movies;

public class CreateReviewHandler(
IAuthorsRepository authorsRepository,
IMoviesRepository moviesRepository,
IReviewsRepository reviewsRepository) : IRequestHandler<CreateReviewCommand, Review>
{
public async Task<Review> Handle(CreateReviewCommand request, CancellationToken cancellationToken)
public async ValueTask<Review> Handle(CreateReviewCommand request, CancellationToken cancellationToken)
{
if (!await authorsRepository.AuthorExists(request.AuthorId, cancellationToken))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace CleanModelContextProtocol.Presentation.Validators;
namespace CleanModelContextProtocol.Application.Reviews.Commands.CreateReview;

using CleanModelContextProtocol.Presentation.Requests;
using FluentValidation;

public class CreateReviewValidator : AbstractValidator<CreateReviewRequest>
public class CreateReviewValidator : AbstractValidator<CreateReviewCommand>
{
public CreateReviewValidator()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CleanModelContextProtocol.Application.Reviews.Commands.DeleteReview;

using MediatR;
using Mediator;

public class DeleteReviewCommand : IRequest<bool>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace CleanModelContextProtocol.Application.Reviews.Commands.DeleteReview;
using System.Threading.Tasks;
using Common.Enums;
using Common.Exceptions;
using MediatR;
using Mediator;

public class DeleteReviewHandler(IReviewsRepository repository) : IRequestHandler<DeleteReviewCommand, bool>
{
public async Task<bool> Handle(DeleteReviewCommand request, CancellationToken cancellationToken)
public async ValueTask<bool> Handle(DeleteReviewCommand request, CancellationToken cancellationToken)
{
if (!await repository.ReviewExists(request.Id, cancellationToken))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CleanModelContextProtocol.Application.Reviews.Commands.DeleteReview;

using FluentValidation;

public class DeleteReviewValidator : AbstractValidator<DeleteReviewCommand>
{
public DeleteReviewValidator()
{
_ = this.RuleFor(r => r.Id).NotEqual(Guid.Empty).WithMessage("A review Id was not supplied.");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CleanModelContextProtocol.Application.Reviews.Commands.UpdateReview;

using MediatR;
using Mediator;

public class UpdateReviewCommand : IRequest<bool>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace CleanModelContextProtocol.Application.Reviews.Commands.UpdateReview;
using CleanModelContextProtocol.Application.Movies;
using Common.Enums;
using Common.Exceptions;
using MediatR;
using Mediator;

public class UpdateReviewHandler(
IAuthorsRepository authorsRepository,
IMoviesRepository moviesRepository,
IReviewsRepository reviewsRepository) : IRequestHandler<UpdateReviewCommand, bool>
{
public async Task<bool> Handle(UpdateReviewCommand request, CancellationToken cancellationToken)
public async ValueTask<bool> Handle(UpdateReviewCommand request, CancellationToken cancellationToken)
{
if (!await reviewsRepository.ReviewExists(request.Id, cancellationToken))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace CleanModelContextProtocol.Presentation.Validators;
namespace CleanModelContextProtocol.Application.Reviews.Commands.UpdateReview;

using CleanModelContextProtocol.Presentation.Requests;
using FluentValidation;

public class UpdateReviewValidator : AbstractValidator<UpdateReviewRequest>
public class UpdateReviewValidator : AbstractValidator<UpdateReviewCommand>
{
public UpdateReviewValidator()
{
_ = this.RuleFor(r => r.Id).NotEqual(Guid.Empty).WithMessage("A review id was not supplied to Update the review.");
_ = this.RuleFor(r => r.AuthorId).NotEqual(Guid.Empty).WithMessage("An author id was not supplied to Update the review.");
_ = this.RuleFor(r => r.MovieId).NotEqual(Guid.Empty).WithMessage("A movie id was not supplied to Update the review.");
_ = this.RuleFor(r => r.Stars).InclusiveBetween(1, 5).WithMessage("A star rating must be between 1 and 5.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace CleanModelContextProtocol.Application.Reviews.Queries.GetReviewById;
using Common.Enums;
using Common.Exceptions;
using Entities;
using MediatR;
using Mediator;

public class GetReviewByIdHandler(IReviewsRepository repository) : IRequestHandler<GetReviewByIdQuery, Review>
{
public async Task<Review> Handle(GetReviewByIdQuery request, CancellationToken cancellationToken)
public async ValueTask<Review> Handle(GetReviewByIdQuery request, CancellationToken cancellationToken)
{
var result = await repository.GetReviewById(request.Id, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace CleanModelContextProtocol.Application.Reviews.Queries.GetReviewById;

using Entities;
using MediatR;
using Mediator;

public class GetReviewByIdQuery : IRequest<Review>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CleanModelContextProtocol.Application.Reviews.Queries.GetReviewById;

using FluentValidation;

public class GetReviewByIdValidator : AbstractValidator<GetReviewByIdQuery>
{
public GetReviewByIdValidator()
{
_ = this.RuleFor(r => r.Id).NotNull().NotEqual(Guid.Empty).WithMessage("A Review Id was not supplied.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ namespace CleanModelContextProtocol.Application.Reviews.Queries.GetReviews;
using System.Threading;
using System.Threading.Tasks;
using Entities;
using MediatR;
using Mediator;

public class GetReviewsHandler(IReviewsRepository repository) : IRequestHandler<GetReviewsQuery, List<Review>>
{
public async Task<List<Review>> Handle(GetReviewsQuery request, CancellationToken cancellationToken)
public async ValueTask<List<Review>> Handle(GetReviewsQuery request, CancellationToken cancellationToken)
{
return await repository.GetReviews(cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace CleanModelContextProtocol.Application.Reviews.Queries.GetReviews;

using Entities;
using MediatR;
using Mediator;

public class GetReviewsQuery : IRequest<List<Review>>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace CleanModelContextProtocol.Application.Versions.Queries.GetVersion;
using System.Threading;
using System.Threading.Tasks;
using Entities;
using MediatR;
using Mediator;

public class GetVersionHandler : IRequestHandler<GetVersionQuery, Version>
{
public Task<Version> Handle(GetVersionQuery request, CancellationToken cancellationToken)
public ValueTask<Version> Handle(GetVersionQuery request, CancellationToken cancellationToken)
{
var assembly = Assembly.GetEntryAssembly();

Expand All @@ -20,6 +20,6 @@ public Task<Version> Handle(GetVersionQuery request, CancellationToken cancellat
$"{assembly?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}"
};

return Task.FromResult(version);
return ValueTask.FromResult(version);
}
}
Loading
Loading