Skip to content

Commit

Permalink
'Initial' commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dgmjr committed Oct 1, 2023
1 parent 1dc3ea0 commit 6fa0965
Show file tree
Hide file tree
Showing 25 changed files with 141 additions and 98 deletions.
5 changes: 2 additions & 3 deletions src/Abstractions/ICreate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http.Headers;

/*
* ICreateCommand.cs
*
Expand All @@ -14,6 +15,4 @@
namespace Dgmjr.MediatR.Abstractions;

public interface ICreateCommand<TId, TCreateDto, TDto> : ICommand<TDto>
where TId : IComparable, IEquatable<TId>
{
}
where TId : IComparable, IEquatable<TId> { }
8 changes: 3 additions & 5 deletions src/Abstractions/ICreateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

namespace Dgmjr.MediatR.Handlers.Abstractions;

public interface ICreateHandler<TCreateCommand, TId, TCreateDto, TDto> : IRequestHandler<TCreateCommand, TDto>
public interface ICreateHandler<TCreateCommand, TId, TCreateDto, TDto>
: IRequestHandler<TCreateCommand, TDto>
where TId : IComparable, IEquatable<TId>
where TCreateCommand : ICreateCommand<TId, TCreateDto, TDto>
{

}
where TCreateCommand : ICreateCommand<TId, TCreateDto, TDto> { }
2 changes: 1 addition & 1 deletion src/Abstractions/ICreatedNotification.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net.Http.Headers;
/*
* ICreateCommand.cs
*
Expand All @@ -12,6 +11,7 @@
*/

namespace Dgmjr.MediatR.Abstractions;
using System.Net.Http.Headers;

public interface ICreatedNotification<TId, TCreateDto, TDto> : INotification
where TId : IComparable, IEquatable<TId>
Expand Down
10 changes: 2 additions & 8 deletions src/Abstractions/IDelete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@
namespace Dgmjr.MediatR.Abstractions;

public interface IDeleteCommand<TId, TDto> : IDeleteCommand<TId>
where TId : IComparable, IEquatable<TId>
{

}
where TId : IComparable, IEquatable<TId> { }

public interface IDeleteCommand<TId> : ICommand<Unit>
where TId : IComparable, IEquatable<TId>
{

}
where TId : IComparable, IEquatable<TId> { }
10 changes: 2 additions & 8 deletions src/Abstractions/IDeleteHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@ namespace Dgmjr.MediatR.Handlers.Abstractions;

public interface IDeleteHandler<TDeleteCommand, TId, TDto> : IDeleteHandler<TDeleteCommand, TId>
where TId : IComparable, IEquatable<TId>
where TDeleteCommand : IDeleteCommand<TId, TDto>
{

}
where TDeleteCommand : IDeleteCommand<TId, TDto> { }

public interface IDeleteHandler<TDeleteCommand, TId> : IRequestHandler<TDeleteCommand, Unit>
where TId : IComparable, IEquatable<TId>
where TDeleteCommand : IDeleteCommand<TId>
{

}
where TDeleteCommand : IDeleteCommand<TId> { }
10 changes: 2 additions & 8 deletions src/Abstractions/IDeletedNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@
namespace Dgmjr.MediatR.Abstractions;

public interface IDeletedNotification<TId, TDto> : INotification
where TId : IComparable, IEquatable<TId>
{

}
where TId : IComparable, IEquatable<TId> { }

public interface IDeletedNotification<TId> : INotification
where TId : IComparable, IEquatable<TId>
{

}
where TId : IComparable, IEquatable<TId> { }
5 changes: 1 addition & 4 deletions src/Abstractions/IPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,4 @@
namespace Dgmjr.MediatR.Abstractions;

public interface IPatchCommand<TId, TPatchDto, TDto> : ICommand<TDto>
where TId : IComparable, IEquatable<TId>
{

}
where TId : IComparable, IEquatable<TId> { }
8 changes: 3 additions & 5 deletions src/Abstractions/IPatchHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

namespace Dgmjr.MediatR.Handlers.Abstractions;

public interface IPatchHandler<TPatchCommand, TId, TPatchDto, TDto> : IRequestHandler<TPatchCommand, TDto>
public interface IPatchHandler<TPatchCommand, TId, TPatchDto, TDto>
: IRequestHandler<TPatchCommand, TDto>
where TPatchCommand : IPatchCommand<TId, TPatchDto, TDto>
where TId : IComparable, IEquatable<TId>
{

}
where TId : IComparable, IEquatable<TId> { }
4 changes: 1 addition & 3 deletions src/Abstractions/IQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
namespace Dgmjr.MediatR.Abstractions;

public interface IQuery<TId, TDto> : IQueryBase<TDto>, IRequest<TDto>
where TId : IComparable, IEquatable<TId>
{
}
where TId : IComparable, IEquatable<TId> { }

public interface IQuery<TDto> : IQueryBase<TDto>, IRequest<IEnumerable<TDto>>
{
Expand Down
9 changes: 2 additions & 7 deletions src/Abstractions/IQueryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ public interface IQueryHandler<TQuery, TId, TModel, TDto> : IRequestHandler<TQue
where TQuery : IQuery<TId, TDto>
where TId : IComparable, IEquatable<TId>
where TDto : IIdentifiable<TId>
where TModel : IIdentifiable<TId>
{
where TModel : IIdentifiable<TId> { }

}
public interface IQueryHandler<TQuery, TDto> : IRequestHandler<TQuery, IEnumerable<TDto>>
where TQuery : IQuery<TDto>
{

}
where TQuery : IQuery<TDto> { }
6 changes: 2 additions & 4 deletions src/Abstractions/IUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
*/

namespace Dgmjr.MediatR.Abstractions;

using Dgmjr.Abstractions;

public interface IUpdateCommand<TModel, TId, TUpdateDto, TDto> : ICommand<TDto>
where TModel : class, IIdentifiable<TId>
where TId : IComparable, IEquatable<TId>
{

}
where TId : IComparable, IEquatable<TId> { }
8 changes: 3 additions & 5 deletions src/Abstractions/IUpdateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@

namespace Dgmjr.MediatR.Handlers.Abstractions;

public interface IUpdateHandler<TUpdateCommand, TId, TModel, TUpdateDto, TDto> : IRequestHandler<TUpdateCommand, TDto>
public interface IUpdateHandler<TUpdateCommand, TId, TModel, TUpdateDto, TDto>
: IRequestHandler<TUpdateCommand, TDto>
where TUpdateCommand : IUpdateCommand<TModel, TId, TUpdateDto, TDto>
where TModel : class, IIdentifiable<TId>
where TId : IComparable, IEquatable<TId>
{

}
where TId : IComparable, IEquatable<TId> { }
5 changes: 3 additions & 2 deletions src/MediatR/Commands/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

namespace Dgmjr.MediatR.Commands;

public record struct CreateCommand<TId, TCreateDto, TDto>(TCreateDto Create) : ICreateCommand<TId, TCreateDto, TDto>
where TId : IComparable, IEquatable<TId>
public record struct CreateCommand<TId, TCreateDto, TDto>(TCreateDto Create)
: ICreateCommand<TId, TCreateDto, TDto>
where TId : IComparable, IEquatable<TId>
{
// public CreateCommand() { }
// public CreateCommand(TCreateDto createDto) => Create = createDto;
Expand Down
4 changes: 2 additions & 2 deletions src/MediatR/Commands/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public record struct DeleteCommand<TId, TDto>(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>(TId Id) : IDeleteCommand<TId>
Expand Down
6 changes: 4 additions & 2 deletions src/MediatR/Commands/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
* Created: 2022-12-21-10:02:09
* Modified: 2022-12-21-10:02:09
*
* Author: David G. Moore, Jr. <[email protected]>
* Author: David G. Moore, Jr. <[email protected]>
*
* 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<TId, TPatchDto, TDto>(JsonPatchDocument<TDto> Patch, TId Id) : IPatchCommand<TId, TPatchDto, TDto>
public record struct PatchCommand<TId, TPatchDto, TDto>(JsonPatchDocument<TDto> Patch, TId Id)
: IPatchCommand<TId, TPatchDto, TDto>
where TId : IComparable, IEquatable<TId>
where TDto : class
{
Expand Down
5 changes: 3 additions & 2 deletions src/MediatR/Commands/Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
* Created: 2022-12-21-10:01:45
* Modified: 2022-12-21-10:02:43
*
* Author: David G. Moore, Jr. <[email protected]>
* Author: David G. Moore, Jr. <[email protected]>
*
* Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved
* License: MIT (https://opensource.org/licenses/MIT)
*/

namespace Dgmjr.MediatR.Commands;

public record struct UpdateCommand<TModel, TId, TUpdateDto, TDto>(TUpdateDto Update) : IUpdateCommand<TModel, TId, TUpdateDto, TDto>
public record struct UpdateCommand<TModel, TId, TUpdateDto, TDto>(TUpdateDto Update)
: IUpdateCommand<TModel, TId, TUpdateDto, TDto>
where TModel : class, IIdentifiable<TId>
where TId : IComparable, IEquatable<TId>
{
Expand Down
1 change: 1 addition & 0 deletions src/MediatR/DI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static IServiceCollection AddMediatR<T>(this IServiceCollection services)
services.AddMediatR(typeof(T).Assembly);
return services;
}

#if NET6_0_OR_GREATER
public static WebApplicationBuilder AddMediatR(this WebApplicationBuilder builder)
{
Expand Down
10 changes: 8 additions & 2 deletions src/MediatR/Handlers/CreateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ public CreateHAndler(IMediator mediator, TDbContext dbContext, IMapper mapper)
Mapper = mapper;
}

public async Task<TViewDto> Handle(CreateCommand<TId, TInsertDto, TViewDto> request, CancellationToken cancellationToken)
public async Task<TViewDto> Handle(
CreateCommand<TId, TInsertDto, TViewDto> request,
CancellationToken cancellationToken
)
{
var model = Mapper.Map<TModel>(request.Create);
Db.Set<TModel>().Add(model);
await Db.SaveChangesAsync();
var dto = Mapper.Map<TViewDto>(model);
await Mediator.Publish(new CreatedNotification<TId, TInsertDto, TViewDto>(model.Id, dto), cancellationToken);
await Mediator.Publish(
new CreatedNotification<TId, TInsertDto, TViewDto>(model.Id, dto),
cancellationToken
);
return dto;
}
}
75 changes: 59 additions & 16 deletions src/MediatR/Handlers/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created: 2022-12-21-10:51:28
* Modified: 2022-12-21-10:51:28
*
* Author: David G. Moore, Jr. <[email protected]>
* Author: David G. Moore, Jr. <[email protected]>
*
* Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved
* License: MIT (https://opensource.org/licenses/MIT)
Expand All @@ -20,7 +20,13 @@ namespace Dgmjr.MediatR;
public class CrudHandler<TModel, TDbContext, TInsertDto, TUpdateDto, TViewDto, TId>
: IHaveADbContext<TDbContext>,
ICreateHandler<CreateCommand<TId, TInsertDto, TViewDto>, TId, TInsertDto, TViewDto>,
IUpdateHandler<UpdateCommand<TModel, TId, TUpdateDto, TViewDto>, TId, TModel, TUpdateDto, TViewDto>,
IUpdateHandler<
UpdateCommand<TModel, TId, TUpdateDto, TViewDto>,
TId,
TModel,
TUpdateDto,
TViewDto
>,
IDeleteHandler<DeleteCommand<TId>, TId>,
IDeleteHandler<DeleteCommand<TId, TViewDto>, TId, TViewDto>,
IQueryHandler<Query<TId, TViewDto>, TId, TModel, TViewDto>,
Expand All @@ -42,17 +48,26 @@ public CrudHandler(IMediator mediator, TDbContext dbContext, IMapper mapper)
Mapper = mapper;
}

public async Task<TViewDto> Handle(CreateCommand<TId, TInsertDto, TViewDto> request, CancellationToken cancellationToken)
public async Task<TViewDto> Handle(
CreateCommand<TId, TInsertDto, TViewDto> request,
CancellationToken cancellationToken
)
{
var model = Mapper.Map<TModel>(request.Create);
Db.Set<TModel>().Add(model);
await Db.SaveChangesAsync();
var dto = Mapper.Map<TViewDto>(model);
await Mediator.Publish(new CreatedNotification<TId, TInsertDto, TViewDto>(model.Id, dto), cancellationToken);
await Mediator.Publish(
new CreatedNotification<TId, TInsertDto, TViewDto>(model.Id, dto),
cancellationToken
);
return dto;
}

public async Task<TViewDto> Handle(UpdateCommand<TModel, TId, TUpdateDto, TViewDto> request, CancellationToken cancellationToken)
public async Task<TViewDto> Handle(
UpdateCommand<TModel, TId, TUpdateDto, TViewDto> request,
CancellationToken cancellationToken
)
{
var model = Db.Set<TModel>().Find(request.Update);
model = Mapper.Map(request.Update, model);
Expand All @@ -69,11 +84,17 @@ public Task<Unit> Handle(DeleteCommand<TId> request, CancellationToken cancellat
Db.Set<TModel>().Remove(model);
Db.SaveChanges();
var dto = Mapper.Map<TViewDto>(model);
Mediator.Publish(new DeletedNotification<TId, TViewDto>(dto, request.Id), cancellationToken);
Mediator.Publish(
new DeletedNotification<TId, TViewDto>(dto, request.Id),
cancellationToken
);
return Task.FromResult(Unit.Value);
}

public async Task<Unit> Handle(DeleteCommand<TId, TViewDto> request, CancellationToken cancellationToken)
public async Task<Unit> Handle(
DeleteCommand<TId, TViewDto> request,
CancellationToken cancellationToken
)
{
if (!request.Id.Equals(request.Dto.Id))
throw new ArgumentException("The Id and Dto.Id must match.");
Expand All @@ -82,22 +103,44 @@ public async Task<Unit> Handle(DeleteCommand<TId, TViewDto> request, Cancellatio
Db.Set<TModel>().Remove(model);
Db.SaveChanges();
var dto = Mapper.Map<TViewDto>(model);
await Mediator.Publish(new DeletedNotification<TId, TViewDto>(dto, request.Id), cancellationToken);
await Mediator.Publish(
new DeletedNotification<TId, TViewDto>(dto, request.Id),
cancellationToken
);
return Unit.Value;
}

public async Task<TViewDto> Handle(Query<TId, TViewDto> request, CancellationToken cancellationToken)
public async Task<TViewDto> Handle(
Query<TId, TViewDto> request,
CancellationToken cancellationToken
)
{
var model = await Db.Set<TModel>().FirstOrDefaultAsync(Mapper.MapExpression<Expression<Func<TViewDto, bool>>, Expression<Func<TModel, bool>>>(request.Predicate));
var model = await Db.Set<TModel>()
.FirstOrDefaultAsync(
Mapper.MapExpression<
Expression<Func<TViewDto, bool>>,
Expression<Func<TModel, bool>>
>(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<TViewDto>(model);
return dto;
}

public async Task<IEnumerable<TViewDto>> Handle(Query<TViewDto> request, CancellationToken cancellationToken)
=> await Db.Set<TModel>()
.Where(Mapper.MapExpression<Expression<Func<TViewDto, bool>>, Expression<Func<TModel, bool>>>(request.Predicate))
.ProjectTo<TViewDto>(Mapper.ConfigurationProvider)
.ToListAsync();
public async Task<IEnumerable<TViewDto>> Handle(
Query<TViewDto> request,
CancellationToken cancellationToken
) =>
await Db.Set<TModel>()
.Where(
Mapper.MapExpression<
Expression<Func<TViewDto, bool>>,
Expression<Func<TModel, bool>>
>(request.Predicate)
)
.ProjectTo<TViewDto>(Mapper.ConfigurationProvider)
.ToListAsync();
}
4 changes: 3 additions & 1 deletion src/MediatR/Handlers/NotificationsLogger.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace Dgmjr.MediatR.Notifications;

using Microsoft.Extensions.Logging;

public class NotificationsLogger<TNotification> : global::MediatR.INotificationHandler<TNotification>
public class NotificationsLogger<TNotification>
: global::MediatR.INotificationHandler<TNotification>
where TNotification : INotification
{
private readonly ILogger<NotificationsLogger<TNotification>> _logger;
Expand Down
Loading

0 comments on commit 6fa0965

Please sign in to comment.