Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor your C# code with primary constructors #1196

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 17 additions & 17 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@
<PackageVersion Include="Ardalis.GuardClauses" Version="4.5.0" />
<PackageVersion Include="AutoMapper" Version="13.0.1" />
<PackageVersion Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.1" />
<PackageVersion Include="Azure.Identity" Version="1.11.1" />
<PackageVersion Include="Azure.Identity" Version="1.12.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="FluentAssertions" Version="7.0.0-alpha.4" />
<PackageVersion Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0" />
<PackageVersion Include="MediatR" Version="12.2.0" />
<PackageVersion Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.4" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.4" />
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.2" />
<PackageVersion Include="MediatR" Version="12.3.0" />
<PackageVersion Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.6" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.6" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.4" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.4" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.6" />
<!--#if (!UseApiOnly)-->
<PackageVersion Include="Microsoft.AspNetCore.SpaProxy" Version="8.0.4" />
<!--#endif-->
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.6" />
<!--#if (UseSQLite)-->
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.6" />
<!--#endif-->
<!--#if (UseLocalDB)-->
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
<!--#endif-->
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.4" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.6" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="Moq" Version="4.20.70" />
<PackageVersion Include="NSwag.AspNetCore" Version="14.0.7" />
<PackageVersion Include="NSwag.MSBuild" Version="14.0.7" />
<PackageVersion Include="NSwag.AspNetCore" Version="14.0.8" />
<PackageVersion Include="NSwag.MSBuild" Version="14.0.8" />
<PackageVersion Include="nunit" Version="3.14.0" />
<PackageVersion Include="NUnit.Analyzers" Version="3.9.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
Expand Down
16 changes: 5 additions & 11 deletions src/Application/Common/Behaviours/AuthorizationBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@

namespace CleanArchitecture.Application.Common.Behaviours;

public class AuthorizationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
public class AuthorizationBehaviour<TRequest, TResponse>(
IUser user,
IIdentityService identityService) : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
{
private readonly IUser _user;
private readonly IIdentityService _identityService;

public AuthorizationBehaviour(
IUser user,
IIdentityService identityService)
{
_user = user;
_identityService = identityService;
}
private readonly IUser _user = user;
private readonly IIdentityService _identityService = identityService;

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
Expand Down
15 changes: 4 additions & 11 deletions src/Application/Common/Behaviours/LoggingBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@

namespace CleanArchitecture.Application.Common.Behaviours;

public class LoggingBehaviour<TRequest> : IRequestPreProcessor<TRequest> where TRequest : notnull
public class LoggingBehaviour<TRequest>(ILogger<TRequest> logger, IUser user, IIdentityService identityService) : IRequestPreProcessor<TRequest> where TRequest : notnull
{
private readonly ILogger _logger;
private readonly IUser _user;
private readonly IIdentityService _identityService;

public LoggingBehaviour(ILogger<TRequest> logger, IUser user, IIdentityService identityService)
{
_logger = logger;
_user = user;
_identityService = identityService;
}
private readonly ILogger _logger = logger;
private readonly IUser _user = user;
private readonly IIdentityService _identityService = identityService;

public async Task Process(TRequest request, CancellationToken cancellationToken)
{
Expand Down
25 changes: 8 additions & 17 deletions src/Application/Common/Behaviours/PerformanceBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@

namespace CleanArchitecture.Application.Common.Behaviours;

public class PerformanceBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
public class PerformanceBehaviour<TRequest, TResponse>(
ILogger<TRequest> logger,
IUser user,
IIdentityService identityService) : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
{
private readonly Stopwatch _timer;
private readonly ILogger<TRequest> _logger;
private readonly IUser _user;
private readonly IIdentityService _identityService;

public PerformanceBehaviour(
ILogger<TRequest> logger,
IUser user,
IIdentityService identityService)
{
_timer = new Stopwatch();

_logger = logger;
_user = user;
_identityService = identityService;
}
private readonly Stopwatch _timer = new Stopwatch();
private readonly ILogger<TRequest> _logger = logger;
private readonly IUser _user = user;
private readonly IIdentityService _identityService = identityService;

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

namespace CleanArchitecture.Application.Common.Behaviours;

public class UnhandledExceptionBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
public class UnhandledExceptionBehaviour<TRequest, TResponse>(ILogger<TRequest> logger) : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
{
private readonly ILogger<TRequest> _logger;

public UnhandledExceptionBehaviour(ILogger<TRequest> logger)
{
_logger = logger;
}
private readonly ILogger<TRequest> _logger = logger;

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
Expand Down
9 changes: 2 additions & 7 deletions src/Application/Common/Behaviours/ValidationBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

namespace CleanArchitecture.Application.Common.Behaviours;

public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
public class ValidationBehaviour<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators) : IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull
{
private readonly IEnumerable<IValidator<TRequest>> _validators;

public ValidationBehaviour(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
private readonly IEnumerable<IValidator<TRequest>> _validators = validators;

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
Expand Down
10 changes: 2 additions & 8 deletions src/Application/Common/Exceptions/ValidationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ namespace CleanArchitecture.Application.Common.Exceptions;
public class ValidationException : Exception
{
public ValidationException()
: base("One or more validation failures have occurred.")
{
Errors = new Dictionary<string, string[]>();
}
: base("One or more validation failures have occurred.") => Errors = new Dictionary<string, string[]>();

public ValidationException(IEnumerable<ValidationFailure> failures)
: this()
{
Errors = failures
: this() => Errors = failures
.GroupBy(e => e.PropertyName, e => e.ErrorMessage)
.ToDictionary(failureGroup => failureGroup.Key, failureGroup => failureGroup.ToArray());
}

public IDictionary<string, string[]> Errors { get; }
}
18 changes: 5 additions & 13 deletions src/Application/Common/Models/PaginatedList.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
namespace CleanArchitecture.Application.Common.Models;

public class PaginatedList<T>
public class PaginatedList<T>(IReadOnlyCollection<T> items, int count, int pageNumber, int pageSize)
{
public IReadOnlyCollection<T> Items { get; }
public int PageNumber { get; }
public int TotalPages { get; }
public int TotalCount { get; }

public PaginatedList(IReadOnlyCollection<T> items, int count, int pageNumber, int pageSize)
{
PageNumber = pageNumber;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
TotalCount = count;
Items = items;
}
public IReadOnlyCollection<T> Items { get; } = items;
public int PageNumber { get; } = pageNumber;
public int TotalPages { get; } = (int)Math.Ceiling(count / (double)pageSize);
public int TotalCount { get; } = count;

public bool HasPreviousPage => PageNumber > 1;

Expand Down
10 changes: 2 additions & 8 deletions src/Application/Common/Models/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ internal Result(bool succeeded, IEnumerable<string> errors)

public string[] Errors { get; init; }

public static Result Success()
{
return new Result(true, Array.Empty<string>());
}
public static Result Success() => new Result(true, Array.Empty<string>());

public static Result Failure(IEnumerable<string> errors)
{
return new Result(false, errors);
}
public static Result Failure(IEnumerable<string> errors) => new Result(false, errors);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ public record CreateTodoItemCommand : IRequest<int>
public string? Title { get; init; }
}

public class CreateTodoItemCommandHandler : IRequestHandler<CreateTodoItemCommand, int>
public class CreateTodoItemCommandHandler(IApplicationDbContext context) : IRequestHandler<CreateTodoItemCommand, int>
{
private readonly IApplicationDbContext _context;

public CreateTodoItemCommandHandler(IApplicationDbContext context)
{
_context = context;
}
private readonly IApplicationDbContext _context = context;

public async Task<int> Handle(CreateTodoItemCommand request, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

public class CreateTodoItemCommandValidator : AbstractValidator<CreateTodoItemCommand>
{
public CreateTodoItemCommandValidator()
{
RuleFor(v => v.Title)
public CreateTodoItemCommandValidator() => RuleFor(v => v.Title)
.MaximumLength(200)
.NotEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ namespace CleanArchitecture.Application.TodoItems.Commands.DeleteTodoItem;

public record DeleteTodoItemCommand(int Id) : IRequest;

public class DeleteTodoItemCommandHandler : IRequestHandler<DeleteTodoItemCommand>
public class DeleteTodoItemCommandHandler(IApplicationDbContext context) : IRequestHandler<DeleteTodoItemCommand>
{
private readonly IApplicationDbContext _context;

public DeleteTodoItemCommandHandler(IApplicationDbContext context)
{
_context = context;
}
private readonly IApplicationDbContext _context = context;

public async Task Handle(DeleteTodoItemCommand request, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ public record UpdateTodoItemCommand : IRequest
public bool Done { get; init; }
}

public class UpdateTodoItemCommandHandler : IRequestHandler<UpdateTodoItemCommand>
public class UpdateTodoItemCommandHandler(IApplicationDbContext context) : IRequestHandler<UpdateTodoItemCommand>
{
private readonly IApplicationDbContext _context;

public UpdateTodoItemCommandHandler(IApplicationDbContext context)
{
_context = context;
}
private readonly IApplicationDbContext _context = context;

public async Task Handle(UpdateTodoItemCommand request, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

public class UpdateTodoItemCommandValidator : AbstractValidator<UpdateTodoItemCommand>
{
public UpdateTodoItemCommandValidator()
{
RuleFor(v => v.Title)
public UpdateTodoItemCommandValidator() => RuleFor(v => v.Title)
.MaximumLength(200)
.NotEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ public record UpdateTodoItemDetailCommand : IRequest
public string? Note { get; init; }
}

public class UpdateTodoItemDetailCommandHandler : IRequestHandler<UpdateTodoItemDetailCommand>
public class UpdateTodoItemDetailCommandHandler(IApplicationDbContext context) : IRequestHandler<UpdateTodoItemDetailCommand>
{
private readonly IApplicationDbContext _context;

public UpdateTodoItemDetailCommandHandler(IApplicationDbContext context)
{
_context = context;
}
private readonly IApplicationDbContext _context = context;

public async Task Handle(UpdateTodoItemDetailCommand request, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

namespace CleanArchitecture.Application.TodoItems.EventHandlers;

public class TodoItemCompletedEventHandler : INotificationHandler<TodoItemCompletedEvent>
public class TodoItemCompletedEventHandler(ILogger<TodoItemCompletedEventHandler> logger) : INotificationHandler<TodoItemCompletedEvent>
{
private readonly ILogger<TodoItemCompletedEventHandler> _logger;

public TodoItemCompletedEventHandler(ILogger<TodoItemCompletedEventHandler> logger)
{
_logger = logger;
}
private readonly ILogger<TodoItemCompletedEventHandler> _logger = logger;

public Task Handle(TodoItemCompletedEvent notification, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

namespace CleanArchitecture.Application.TodoItems.EventHandlers;

public class TodoItemCreatedEventHandler : INotificationHandler<TodoItemCreatedEvent>
public class TodoItemCreatedEventHandler(ILogger<TodoItemCreatedEventHandler> logger) : INotificationHandler<TodoItemCreatedEvent>
{
private readonly ILogger<TodoItemCreatedEventHandler> _logger;

public TodoItemCreatedEventHandler(ILogger<TodoItemCreatedEventHandler> logger)
{
_logger = logger;
}
private readonly ILogger<TodoItemCreatedEventHandler> _logger = logger;

public Task Handle(TodoItemCreatedEvent notification, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,14 @@ public record GetTodoItemsWithPaginationQuery : IRequest<PaginatedList<TodoItemB
public int PageSize { get; init; } = 10;
}

public class GetTodoItemsWithPaginationQueryHandler : IRequestHandler<GetTodoItemsWithPaginationQuery, PaginatedList<TodoItemBriefDto>>
public class GetTodoItemsWithPaginationQueryHandler(IApplicationDbContext context, IMapper mapper) : IRequestHandler<GetTodoItemsWithPaginationQuery, PaginatedList<TodoItemBriefDto>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IApplicationDbContext _context = context;
private readonly IMapper _mapper = mapper;

public GetTodoItemsWithPaginationQueryHandler(IApplicationDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}

public async Task<PaginatedList<TodoItemBriefDto>> Handle(GetTodoItemsWithPaginationQuery request, CancellationToken cancellationToken)
{
return await _context.TodoItems
public async Task<PaginatedList<TodoItemBriefDto>> Handle(GetTodoItemsWithPaginationQuery request, CancellationToken cancellationToken) => await _context.TodoItems
.Where(x => x.ListId == request.ListId)
.OrderBy(x => x.Title)
.ProjectTo<TodoItemBriefDto>(_mapper.ConfigurationProvider)
.PaginatedListAsync(request.PageNumber, request.PageSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public class TodoItemBriefDto

private class Mapping : Profile
{
public Mapping()
{
CreateMap<TodoItem, TodoItemBriefDto>();
}
public Mapping() => CreateMap<TodoItem, TodoItemBriefDto>();
}
}
Loading
Loading