From 6fa09652589ea51e8e4562c7807a605947286a3a Mon Sep 17 00:00:00 2001 From: "David G. Moore, Jr" Date: Sun, 1 Oct 2023 09:08:00 -0400 Subject: [PATCH] 'Initial' commit --- src/Abstractions/ICreate.cs | 5 +- src/Abstractions/ICreateHandler.cs | 8 +- src/Abstractions/ICreatedNotification.cs | 2 +- src/Abstractions/IDelete.cs | 10 +-- src/Abstractions/IDeleteHandler.cs | 10 +-- src/Abstractions/IDeletedNotification.cs | 10 +-- src/Abstractions/IPatch.cs | 5 +- src/Abstractions/IPatchHandler.cs | 8 +- src/Abstractions/IQuery.cs | 4 +- src/Abstractions/IQueryHandler.cs | 9 +-- src/Abstractions/IUpdate.cs | 6 +- src/Abstractions/IUpdateHandler.cs | 8 +- src/MediatR/Commands/Create.cs | 5 +- src/MediatR/Commands/Delete.cs | 4 +- src/MediatR/Commands/Patch.cs | 6 +- src/MediatR/Commands/Update.cs | 5 +- src/MediatR/DI.cs | 1 + src/MediatR/Handlers/CreateHandler.cs | 10 ++- src/MediatR/Handlers/Handler.cs | 75 +++++++++++++++---- src/MediatR/Handlers/NotificationsLogger.cs | 4 +- src/MediatR/Handlers/UpdateHandler.cs | 13 +++- src/MediatR/Notifications/Create.cs | 7 +- src/MediatR/Notifications/Delete.cs | 2 +- src/MediatR/Queries/Query.cs | 20 +++-- .../IsExternalInit.cs | 2 +- 25 files changed, 141 insertions(+), 98 deletions(-) diff --git a/src/Abstractions/ICreate.cs b/src/Abstractions/ICreate.cs index 9a39484..58c6855 100644 --- a/src/Abstractions/ICreate.cs +++ b/src/Abstractions/ICreate.cs @@ -1,4 +1,5 @@ using System.Net.Http.Headers; + /* * ICreateCommand.cs * @@ -14,6 +15,4 @@ namespace Dgmjr.MediatR.Abstractions; public interface ICreateCommand : ICommand - where TId : IComparable, IEquatable -{ -} + where TId : IComparable, IEquatable { } diff --git a/src/Abstractions/ICreateHandler.cs b/src/Abstractions/ICreateHandler.cs index 3c7acf9..0dd33e1 100644 --- a/src/Abstractions/ICreateHandler.cs +++ b/src/Abstractions/ICreateHandler.cs @@ -15,9 +15,7 @@ namespace Dgmjr.MediatR.Handlers.Abstractions; -public interface ICreateHandler : IRequestHandler +public interface ICreateHandler + : IRequestHandler where TId : IComparable, IEquatable - where TCreateCommand : ICreateCommand -{ - -} + where TCreateCommand : ICreateCommand { } diff --git a/src/Abstractions/ICreatedNotification.cs b/src/Abstractions/ICreatedNotification.cs index d9cdcc3..f13a589 100644 --- a/src/Abstractions/ICreatedNotification.cs +++ b/src/Abstractions/ICreatedNotification.cs @@ -1,4 +1,3 @@ -using System.Net.Http.Headers; /* * ICreateCommand.cs * @@ -12,6 +11,7 @@ */ namespace Dgmjr.MediatR.Abstractions; +using System.Net.Http.Headers; public interface ICreatedNotification : INotification where TId : IComparable, IEquatable diff --git a/src/Abstractions/IDelete.cs b/src/Abstractions/IDelete.cs index 8b070ac..f764ff9 100644 --- a/src/Abstractions/IDelete.cs +++ b/src/Abstractions/IDelete.cs @@ -13,13 +13,7 @@ namespace Dgmjr.MediatR.Abstractions; public interface IDeleteCommand : IDeleteCommand - where TId : IComparable, IEquatable -{ - -} + where TId : IComparable, IEquatable { } public interface IDeleteCommand : ICommand - where TId : IComparable, IEquatable -{ - -} + where TId : IComparable, IEquatable { } diff --git a/src/Abstractions/IDeleteHandler.cs b/src/Abstractions/IDeleteHandler.cs index b80d960..272d8b7 100644 --- a/src/Abstractions/IDeleteHandler.cs +++ b/src/Abstractions/IDeleteHandler.cs @@ -16,14 +16,8 @@ namespace Dgmjr.MediatR.Handlers.Abstractions; public interface IDeleteHandler : IDeleteHandler where TId : IComparable, IEquatable - where TDeleteCommand : IDeleteCommand -{ - -} + where TDeleteCommand : IDeleteCommand { } public interface IDeleteHandler : IRequestHandler where TId : IComparable, IEquatable - where TDeleteCommand : IDeleteCommand -{ - -} + where TDeleteCommand : IDeleteCommand { } diff --git a/src/Abstractions/IDeletedNotification.cs b/src/Abstractions/IDeletedNotification.cs index 6dbf35f..aa29f0b 100644 --- a/src/Abstractions/IDeletedNotification.cs +++ b/src/Abstractions/IDeletedNotification.cs @@ -13,13 +13,7 @@ namespace Dgmjr.MediatR.Abstractions; public interface IDeletedNotification : INotification - where TId : IComparable, IEquatable -{ - -} + where TId : IComparable, IEquatable { } public interface IDeletedNotification : INotification - where TId : IComparable, IEquatable -{ - -} + where TId : IComparable, IEquatable { } diff --git a/src/Abstractions/IPatch.cs b/src/Abstractions/IPatch.cs index cff13a9..6bc0165 100644 --- a/src/Abstractions/IPatch.cs +++ b/src/Abstractions/IPatch.cs @@ -13,7 +13,4 @@ namespace Dgmjr.MediatR.Abstractions; public interface IPatchCommand : ICommand - where TId : IComparable, IEquatable -{ - -} + where TId : IComparable, IEquatable { } diff --git a/src/Abstractions/IPatchHandler.cs b/src/Abstractions/IPatchHandler.cs index fc39003..4d60782 100644 --- a/src/Abstractions/IPatchHandler.cs +++ b/src/Abstractions/IPatchHandler.cs @@ -14,9 +14,7 @@ namespace Dgmjr.MediatR.Handlers.Abstractions; -public interface IPatchHandler : IRequestHandler +public interface IPatchHandler + : IRequestHandler where TPatchCommand : IPatchCommand - where TId : IComparable, IEquatable -{ - -} + where TId : IComparable, IEquatable { } diff --git a/src/Abstractions/IQuery.cs b/src/Abstractions/IQuery.cs index a0c57f8..fbb4ac3 100644 --- a/src/Abstractions/IQuery.cs +++ b/src/Abstractions/IQuery.cs @@ -16,9 +16,7 @@ namespace Dgmjr.MediatR.Abstractions; public interface IQuery : IQueryBase, IRequest - where TId : IComparable, IEquatable -{ -} + where TId : IComparable, IEquatable { } public interface IQuery : IQueryBase, IRequest> { diff --git a/src/Abstractions/IQueryHandler.cs b/src/Abstractions/IQueryHandler.cs index be3b92e..e3b41b8 100644 --- a/src/Abstractions/IQueryHandler.cs +++ b/src/Abstractions/IQueryHandler.cs @@ -19,12 +19,7 @@ public interface IQueryHandler : IRequestHandler where TId : IComparable, IEquatable where TDto : IIdentifiable - where TModel : IIdentifiable -{ + where TModel : IIdentifiable { } -} public interface IQueryHandler : IRequestHandler> - where TQuery : IQuery -{ - -} + where TQuery : IQuery { } diff --git a/src/Abstractions/IUpdate.cs b/src/Abstractions/IUpdate.cs index 6e06434..247d151 100644 --- a/src/Abstractions/IUpdate.cs +++ b/src/Abstractions/IUpdate.cs @@ -11,11 +11,9 @@ */ namespace Dgmjr.MediatR.Abstractions; + using Dgmjr.Abstractions; public interface IUpdateCommand : ICommand where TModel : class, IIdentifiable - where TId : IComparable, IEquatable -{ - -} + where TId : IComparable, IEquatable { } diff --git a/src/Abstractions/IUpdateHandler.cs b/src/Abstractions/IUpdateHandler.cs index 43e005f..9c4c274 100644 --- a/src/Abstractions/IUpdateHandler.cs +++ b/src/Abstractions/IUpdateHandler.cs @@ -15,10 +15,8 @@ namespace Dgmjr.MediatR.Handlers.Abstractions; -public interface IUpdateHandler : IRequestHandler +public interface IUpdateHandler + : IRequestHandler where TUpdateCommand : IUpdateCommand where TModel : class, IIdentifiable - where TId : IComparable, IEquatable -{ - -} + where TId : IComparable, IEquatable { } diff --git a/src/MediatR/Commands/Create.cs b/src/MediatR/Commands/Create.cs index 862a618..45d61e1 100644 --- a/src/MediatR/Commands/Create.cs +++ b/src/MediatR/Commands/Create.cs @@ -12,8 +12,9 @@ namespace Dgmjr.MediatR.Commands; -public record struct CreateCommand(TCreateDto Create) : ICreateCommand - where TId : IComparable, IEquatable +public record struct CreateCommand(TCreateDto Create) + : ICreateCommand + where TId : IComparable, IEquatable { // public CreateCommand() { } // public CreateCommand(TCreateDto createDto) => Create = createDto; diff --git a/src/MediatR/Commands/Delete.cs b/src/MediatR/Commands/Delete.cs index be9c8a2..23416bb 100644 --- a/src/MediatR/Commands/Delete.cs +++ b/src/MediatR/Commands/Delete.cs @@ -21,8 +21,8 @@ public record struct DeleteCommand(TDto Dto, TId Id) : IDeleteCommand // Id = id; // } - public TDto Dto { get; } = Dto; -public TId Id { get; } = Id; + public TDto Dto { get; } = Dto; + public TId Id { get; } = Id; } public record struct DeleteCommand(TId Id) : IDeleteCommand diff --git a/src/MediatR/Commands/Patch.cs b/src/MediatR/Commands/Patch.cs index 1d105ed..0d33731 100644 --- a/src/MediatR/Commands/Patch.cs +++ b/src/MediatR/Commands/Patch.cs @@ -4,17 +4,19 @@ * Created: 2022-12-21-10:02:09 * Modified: 2022-12-21-10:02:09 * - * Author: David G. Moore, Jr. + * Author: David G. Moore, Jr. * * Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved * License: MIT (https://opensource.org/licenses/MIT) */ namespace Dgmjr.MediatR.Commands; + #if NETSTANDARD2_0_OR_GREATER using Microsoft.AspNetCore.JsonPatch; -public record struct PatchCommand(JsonPatchDocument Patch, TId Id) : IPatchCommand +public record struct PatchCommand(JsonPatchDocument Patch, TId Id) + : IPatchCommand where TId : IComparable, IEquatable where TDto : class { diff --git a/src/MediatR/Commands/Update.cs b/src/MediatR/Commands/Update.cs index 7b0ae4d..21a04bd 100644 --- a/src/MediatR/Commands/Update.cs +++ b/src/MediatR/Commands/Update.cs @@ -4,7 +4,7 @@ * Created: 2022-12-21-10:01:45 * Modified: 2022-12-21-10:02:43 * - * Author: David G. Moore, Jr. + * Author: David G. Moore, Jr. * * Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved * License: MIT (https://opensource.org/licenses/MIT) @@ -12,7 +12,8 @@ namespace Dgmjr.MediatR.Commands; -public record struct UpdateCommand(TUpdateDto Update) : IUpdateCommand +public record struct UpdateCommand(TUpdateDto Update) + : IUpdateCommand where TModel : class, IIdentifiable where TId : IComparable, IEquatable { diff --git a/src/MediatR/DI.cs b/src/MediatR/DI.cs index 6cb6d33..da0f6b7 100644 --- a/src/MediatR/DI.cs +++ b/src/MediatR/DI.cs @@ -21,6 +21,7 @@ public static IServiceCollection AddMediatR(this IServiceCollection services) services.AddMediatR(typeof(T).Assembly); return services; } + #if NET6_0_OR_GREATER public static WebApplicationBuilder AddMediatR(this WebApplicationBuilder builder) { diff --git a/src/MediatR/Handlers/CreateHandler.cs b/src/MediatR/Handlers/CreateHandler.cs index 88e2817..cb47ae0 100644 --- a/src/MediatR/Handlers/CreateHandler.cs +++ b/src/MediatR/Handlers/CreateHandler.cs @@ -34,13 +34,19 @@ public CreateHAndler(IMediator mediator, TDbContext dbContext, IMapper mapper) Mapper = mapper; } - public async Task Handle(CreateCommand request, CancellationToken cancellationToken) + public async Task Handle( + CreateCommand request, + CancellationToken cancellationToken + ) { var model = Mapper.Map(request.Create); Db.Set().Add(model); await Db.SaveChangesAsync(); var dto = Mapper.Map(model); - await Mediator.Publish(new CreatedNotification(model.Id, dto), cancellationToken); + await Mediator.Publish( + new CreatedNotification(model.Id, dto), + cancellationToken + ); return dto; } } diff --git a/src/MediatR/Handlers/Handler.cs b/src/MediatR/Handlers/Handler.cs index 87943a6..afb5f5f 100644 --- a/src/MediatR/Handlers/Handler.cs +++ b/src/MediatR/Handlers/Handler.cs @@ -4,7 +4,7 @@ * Created: 2022-12-21-10:51:28 * Modified: 2022-12-21-10:51:28 * - * Author: David G. Moore, Jr. + * Author: David G. Moore, Jr. * * Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved * License: MIT (https://opensource.org/licenses/MIT) @@ -20,7 +20,13 @@ namespace Dgmjr.MediatR; public class CrudHandler : IHaveADbContext, ICreateHandler, TId, TInsertDto, TViewDto>, - IUpdateHandler, TId, TModel, TUpdateDto, TViewDto>, + IUpdateHandler< + UpdateCommand, + TId, + TModel, + TUpdateDto, + TViewDto + >, IDeleteHandler, TId>, IDeleteHandler, TId, TViewDto>, IQueryHandler, TId, TModel, TViewDto>, @@ -42,17 +48,26 @@ public CrudHandler(IMediator mediator, TDbContext dbContext, IMapper mapper) Mapper = mapper; } - public async Task Handle(CreateCommand request, CancellationToken cancellationToken) + public async Task Handle( + CreateCommand request, + CancellationToken cancellationToken + ) { var model = Mapper.Map(request.Create); Db.Set().Add(model); await Db.SaveChangesAsync(); var dto = Mapper.Map(model); - await Mediator.Publish(new CreatedNotification(model.Id, dto), cancellationToken); + await Mediator.Publish( + new CreatedNotification(model.Id, dto), + cancellationToken + ); return dto; } - public async Task Handle(UpdateCommand request, CancellationToken cancellationToken) + public async Task Handle( + UpdateCommand request, + CancellationToken cancellationToken + ) { var model = Db.Set().Find(request.Update); model = Mapper.Map(request.Update, model); @@ -69,11 +84,17 @@ public Task Handle(DeleteCommand request, CancellationToken cancellat Db.Set().Remove(model); Db.SaveChanges(); var dto = Mapper.Map(model); - Mediator.Publish(new DeletedNotification(dto, request.Id), cancellationToken); + Mediator.Publish( + new DeletedNotification(dto, request.Id), + cancellationToken + ); return Task.FromResult(Unit.Value); } - public async Task Handle(DeleteCommand request, CancellationToken cancellationToken) + public async Task Handle( + DeleteCommand request, + CancellationToken cancellationToken + ) { if (!request.Id.Equals(request.Dto.Id)) throw new ArgumentException("The Id and Dto.Id must match."); @@ -82,22 +103,44 @@ public async Task Handle(DeleteCommand request, Cancellatio Db.Set().Remove(model); Db.SaveChanges(); var dto = Mapper.Map(model); - await Mediator.Publish(new DeletedNotification(dto, request.Id), cancellationToken); + await Mediator.Publish( + new DeletedNotification(dto, request.Id), + cancellationToken + ); return Unit.Value; } - public async Task Handle(Query request, CancellationToken cancellationToken) + public async Task Handle( + Query request, + CancellationToken cancellationToken + ) { - var model = await Db.Set().FirstOrDefaultAsync(Mapper.MapExpression>, Expression>>(request.Predicate)); + var model = await Db.Set() + .FirstOrDefaultAsync( + Mapper.MapExpression< + Expression>, + Expression> + >(request.Predicate) + ); if (model is null) - throw new KeyNotFoundException($"A(n) {typeof(TModel).Name} meeting the requested criteria was not found."); + throw new KeyNotFoundException( + $"A(n) {typeof(TModel).Name} meeting the requested criteria was not found." + ); var dto = Mapper.Map(model); return dto; } - public async Task> Handle(Query request, CancellationToken cancellationToken) - => await Db.Set() - .Where(Mapper.MapExpression>, Expression>>(request.Predicate)) - .ProjectTo(Mapper.ConfigurationProvider) - .ToListAsync(); + public async Task> Handle( + Query request, + CancellationToken cancellationToken + ) => + await Db.Set() + .Where( + Mapper.MapExpression< + Expression>, + Expression> + >(request.Predicate) + ) + .ProjectTo(Mapper.ConfigurationProvider) + .ToListAsync(); } diff --git a/src/MediatR/Handlers/NotificationsLogger.cs b/src/MediatR/Handlers/NotificationsLogger.cs index 41b078d..1522979 100644 --- a/src/MediatR/Handlers/NotificationsLogger.cs +++ b/src/MediatR/Handlers/NotificationsLogger.cs @@ -1,7 +1,9 @@ namespace Dgmjr.MediatR.Notifications; + using Microsoft.Extensions.Logging; -public class NotificationsLogger : global::MediatR.INotificationHandler +public class NotificationsLogger + : global::MediatR.INotificationHandler where TNotification : INotification { private readonly ILogger> _logger; diff --git a/src/MediatR/Handlers/UpdateHandler.cs b/src/MediatR/Handlers/UpdateHandler.cs index d06d2d6..c31aa6f 100644 --- a/src/MediatR/Handlers/UpdateHandler.cs +++ b/src/MediatR/Handlers/UpdateHandler.cs @@ -16,7 +16,13 @@ namespace Dgmjr.MediatR; public class UpdateHandler : IHaveADbContext, - IUpdateHandler, TId, TModel, TUpdateDto, TViewDto> + IUpdateHandler< + UpdateCommand, + TId, + TModel, + TUpdateDto, + TViewDto + > where TDbContext : DbContext, IDbContext where TModel : class, IIdentifiable where TId : IComparable, IEquatable @@ -34,7 +40,10 @@ public UpdateHandler(IMediator mediator, TDbContext dbContext, IMapper mapper) Mapper = mapper; } - public Task Handle(UpdateCommand request, CancellationToken cancellationToken) + public Task Handle( + UpdateCommand request, + CancellationToken cancellationToken + ) { var model = Db.Set().Find(request.Update); Mapper.Map(request.Update, model); diff --git a/src/MediatR/Notifications/Create.cs b/src/MediatR/Notifications/Create.cs index 9b5956e..a63cf80 100644 --- a/src/MediatR/Notifications/Create.cs +++ b/src/MediatR/Notifications/Create.cs @@ -11,13 +11,18 @@ */ namespace Dgmjr.MediatR.Notifications; + using Dgmjr.MediatR.Abstractions; -public record struct CreatedNotification : ICreatedNotification +public record struct CreatedNotification + : ICreatedNotification where TId : IComparable, IEquatable { public CreatedNotification() { } + public CreatedNotification(TDto dto) => Created = dto; + public CreatedNotification(TId id, TDto dto) => Created = dto; + public TDto Created { get; set; } } diff --git a/src/MediatR/Notifications/Delete.cs b/src/MediatR/Notifications/Delete.cs index 6d5b531..5648055 100644 --- a/src/MediatR/Notifications/Delete.cs +++ b/src/MediatR/Notifications/Delete.cs @@ -4,7 +4,7 @@ * Created: 2022-12-21-10:01:23 * Modified: 2022-12-21-10:02:34 * - * Author: David G. Moore, Jr. + * Author: David G. Moore, Jr. * * Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved * License: MIT (https://opensource.org/licenses/MIT) diff --git a/src/MediatR/Queries/Query.cs b/src/MediatR/Queries/Query.cs index 37d0738..71f125e 100644 --- a/src/MediatR/Queries/Query.cs +++ b/src/MediatR/Queries/Query.cs @@ -18,7 +18,12 @@ namespace Dgmjr.MediatR.Queries; public record struct Query : IQuery { public Query() { } - public Query(Expression> predicate, int pageNumber = 1, int pageSize = int.MaxValue) + + public Query( + Expression> predicate, + int pageNumber = 1, + int pageSize = int.MaxValue + ) { Predicate = predicate; PageNumber = pageNumber; @@ -35,15 +40,20 @@ public record struct Query : IQuery where TDto : IIdentifiable { public Query() { } - public Query(Expression> predicate, int pageNumber = 1, int pageSize = int.MaxValue) + + public Query( + Expression> predicate, + int pageNumber = 1, + int pageSize = int.MaxValue + ) { Predicate = predicate; PageNumber = pageNumber; PageSize = pageSize; } - public Query(TId id) : this(_ => _.Id.Equals(id), 1, 1) - { - } + + public Query(TId id) + : this(_ => _.Id.Equals(id), 1, 1) { } public Expression>? Predicate { get; } = _ => true; public int PageSize { get; } = int.MaxValue; diff --git a/src/MediatR/System.Runtime.CompilerServices/IsExternalInit.cs b/src/MediatR/System.Runtime.CompilerServices/IsExternalInit.cs index 2a3bcbd..cc07055 100644 --- a/src/MediatR/System.Runtime.CompilerServices/IsExternalInit.cs +++ b/src/MediatR/System.Runtime.CompilerServices/IsExternalInit.cs @@ -4,7 +4,7 @@ * Created: 2022-12-26-10:32:43 * Modified: 2022-12-26-10:32:44 * - * Author: David G. Moore, Jr. + * Author: David G. Moore, Jr. * * Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved * License: MIT (https://opensource.org/licenses/MIT)