Skip to content

Commit

Permalink
Convert more constructors to primary constructors (#225)
Browse files Browse the repository at this point in the history
### Summary & Motivation

Refactoring the remaining parts of the codebase to make use of primary
constructors wherever possible.

### Checklist

- [x] I have added a Label to the pull-request
- [x] I have added tests, and done manual regression tests
- [x] I have updated the documentation, if necessary
  • Loading branch information
tjementum authored Nov 21, 2023
2 parents bdee9fe + 287339c commit 75f545b
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 82 deletions.
11 changes: 2 additions & 9 deletions application/account-management/Application/Users/CreateUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ public sealed record CreateUserCommand(TenantId TenantId, string Email, UserRole
: ICommand, IUserValidation, IRequest<Result<UserId>>;

[UsedImplicitly]
public sealed class CreateUserHandler : IRequestHandler<CreateUserCommand, Result<UserId>>
public sealed class CreateUserHandler(IUserRepository userRepository) : IRequestHandler<CreateUserCommand, Result<UserId>>
{
private readonly IUserRepository _userRepository;

public CreateUserHandler(IUserRepository userRepository)
{
_userRepository = userRepository;
}

public async Task<Result<UserId>> Handle(CreateUserCommand command, CancellationToken cancellationToken)
{
var user = User.Create(command.TenantId, command.Email, command.UserRole);
await _userRepository.AddAsync(user, cancellationToken);
await userRepository.AddAsync(user, cancellationToken);
return user.Id;
}
}
Expand Down
13 changes: 3 additions & 10 deletions application/account-management/Application/Users/DeleteUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@ namespace PlatformPlatform.AccountManagement.Application.Users;
public sealed record DeleteUserCommand(UserId Id) : ICommand, IRequest<Result>;

[UsedImplicitly]
public sealed class DeleteUserHandler : IRequestHandler<DeleteUserCommand, Result>
public sealed class DeleteUserHandler(IUserRepository userRepository) : IRequestHandler<DeleteUserCommand, Result>
{
private readonly IUserRepository _userRepository;

public DeleteUserHandler(IUserRepository userRepository)
{
_userRepository = userRepository;
}

public async Task<Result> Handle(DeleteUserCommand command, CancellationToken cancellationToken)
{
var user = await _userRepository.GetByIdAsync(command.Id, cancellationToken);
var user = await userRepository.GetByIdAsync(command.Id, cancellationToken);
if (user is null) return Result.NotFound($"User with id '{command.Id}' not found.");

_userRepository.Remove(user);
userRepository.Remove(user);
return Result.Success();
}
}
11 changes: 2 additions & 9 deletions application/account-management/Application/Users/GetUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ namespace PlatformPlatform.AccountManagement.Application.Users;
public sealed record GetUserQuery(UserId Id) : IRequest<Result<UserResponseDto>>;

[UsedImplicitly]
public sealed class GetUserHandler : IRequestHandler<GetUserQuery, Result<UserResponseDto>>
public sealed class GetUserHandler(IUserRepository userRepository) : IRequestHandler<GetUserQuery, Result<UserResponseDto>>
{
private readonly IUserRepository _userRepository;

public GetUserHandler(IUserRepository userRepository)
{
_userRepository = userRepository;
}

public async Task<Result<UserResponseDto>> Handle(GetUserQuery request, CancellationToken cancellationToken)
{
var user = await _userRepository.GetByIdAsync(request.Id, cancellationToken);
var user = await userRepository.GetByIdAsync(request.Id, cancellationToken);
return user?.Adapt<UserResponseDto>()
?? Result<UserResponseDto>.NotFound($"User with id '{request.Id}' not found.");
}
Expand Down
13 changes: 3 additions & 10 deletions application/account-management/Application/Users/UpdateUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,15 @@ public sealed record UpdateUserCommand : ICommand, IUserValidation, IRequest<Res
}

[UsedImplicitly]
public sealed class UpdateUserHandler : IRequestHandler<UpdateUserCommand, Result>
public sealed class UpdateUserHandler(IUserRepository userRepository) : IRequestHandler<UpdateUserCommand, Result>
{
private readonly IUserRepository _userRepository;

public UpdateUserHandler(IUserRepository userRepository)
{
_userRepository = userRepository;
}

public async Task<Result> Handle(UpdateUserCommand command, CancellationToken cancellationToken)
{
var user = await _userRepository.GetByIdAsync(command.Id, cancellationToken);
var user = await userRepository.GetByIdAsync(command.Id, cancellationToken);
if (user is null) return Result.NotFound($"User with id '{command.Id}' not found.");

user.Update(command.Email, command.UserRole);
_userRepository.Update(user);
userRepository.Update(user);
return Result.Success();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@

namespace PlatformPlatform.SharedKernel.ApiCore.Middleware;

public sealed class GlobalExceptionHandlerMiddleware : IMiddleware
public sealed class GlobalExceptionHandlerMiddleware(ILogger<GlobalExceptionHandlerMiddleware> logger,
IOptions<JsonOptions> jsonOptions) : IMiddleware
{
private readonly JsonSerializerOptions _jsonSerializerOptions;
private readonly ILogger _logger;

public GlobalExceptionHandlerMiddleware(ILogger<GlobalExceptionHandlerMiddleware> logger,
IOptions<JsonOptions> jsonOptions)
{
_logger = logger;
_jsonSerializerOptions = jsonOptions.Value.SerializerOptions;
}
private readonly JsonSerializerOptions _jsonSerializerOptions = jsonOptions.Value.SerializerOptions;

Check warning on line 13 in application/shared-kernel/ApiCore/Middleware/GlobalExceptionHandlerMiddleware.cs

View workflow job for this annotation

GitHub Actions / Test and Code Coverage

Remove the member initializer, all constructors set an initial value for the member. (https://rules.sonarsource.com/csharp/RSPEC-3604)
private readonly ILogger _logger = logger;

Check warning on line 14 in application/shared-kernel/ApiCore/Middleware/GlobalExceptionHandlerMiddleware.cs

View workflow job for this annotation

GitHub Actions / Test and Code Coverage

Remove the member initializer, all constructors set an initial value for the member. (https://rules.sonarsource.com/csharp/RSPEC-3604)

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@

namespace PlatformPlatform.SharedKernel.ApiCore.Middleware;

public sealed class ModelBindingExceptionHandlerMiddleware : IMiddleware
public sealed class ModelBindingExceptionHandlerMiddleware(IOptions<JsonOptions> jsonOptions) : IMiddleware
{
private readonly JsonSerializerOptions _jsonSerializerOptions;

public ModelBindingExceptionHandlerMiddleware(IOptions<JsonOptions> jsonOptions)
{
_jsonSerializerOptions = jsonOptions.Value.SerializerOptions;
}
private readonly JsonSerializerOptions _jsonSerializerOptions = jsonOptions.Value.SerializerOptions;

Check warning on line 12 in application/shared-kernel/ApiCore/Middleware/ModelBindingExceptionHandlerMiddleware.cs

View workflow job for this annotation

GitHub Actions / Test and Code Coverage

Remove the member initializer, all constructors set an initial value for the member. (https://rules.sonarsource.com/csharp/RSPEC-3604)

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@

namespace PlatformPlatform.SharedKernel.InfrastructureCore.Persistence;

public sealed class DomainEventCollector : IDomainEventCollector
public sealed class DomainEventCollector(DbContext dbContext) : IDomainEventCollector
{
private readonly DbContext _dbContext;

public DomainEventCollector(DbContext dbContext)
{
_dbContext = dbContext;
}

public IEnumerable<IAggregateRoot> GetAggregatesWithDomainEvents()
{
return _dbContext.ChangeTracker
return dbContext.ChangeTracker
.Entries<IAggregateRoot>()
.Where(e => e.Entity.DomainEvents.Any())
.Select(e => e.Entity);
Expand Down
9 changes: 2 additions & 7 deletions application/shared-kernel/Tests/TestEntities/TestAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

namespace PlatformPlatform.SharedKernel.Tests.TestEntities;

public sealed class TestAggregate : AggregateRoot<long>
public sealed class TestAggregate(string name) : AggregateRoot<long>(IdGenerator.NewId())
{
private TestAggregate(string name) : base(IdGenerator.NewId())
{
Name = name;
}

public string Name { get; set; }
public string Name { get; set; } = name;

public static TestAggregate Create(string name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@

namespace PlatformPlatform.SharedKernel.Tests.TestEntities;

public class TestAggregateRepository : RepositoryBase<TestAggregate, long>, ITestAggregateRepository
{
public TestAggregateRepository(TestDbContext testDbContext) : base(testDbContext)
{
}
}
public class TestAggregateRepository(TestDbContext testDbContext)
: RepositoryBase<TestAggregate, long>(testDbContext), ITestAggregateRepository;
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@

namespace PlatformPlatform.SharedKernel.Tests.TestEntities;

public class TestDbContext : SharedKernelDbContext<TestDbContext>
public class TestDbContext(DbContextOptions<TestDbContext> options) : SharedKernelDbContext<TestDbContext>(options)
{
public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
{
}

public DbSet<TestAggregate> TestAggregates => Set<TestAggregate>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down

0 comments on commit 75f545b

Please sign in to comment.