src/Application/Common/Interfaces/Repositories
including necessary operations
public interface ISampleRepository
{
Task<int> GetTests();
}
src/Application/Common/Interfaces/IUnitOfWork.cs
like this:public interface IUnitOfWork : IDisposable
{
...
ISampleRepository SampleRepository { get; }
...
}
Implement that repo interface in src/Infrastructure/Repositories/YourRepository.cs
public class SampleRepository : ISampleRepository
{
private readonly IDbConnection _connection;
private readonly IDbTransaction _transaction;
public SampleRepository(IDbConnection connection, IDbTransaction transaction)
{
_connection = connection;
_transaction = transaction;
}
public async Task<int> GetTests()
{
var sql = "CREATE TABLE Documents(id INTEGER)";
return await _connection.ExecuteAsync(sql, transaction: _transaction);
}
}
Register that repo in unit of work:
public class UnitOfWork : IUnitOfWork
{
...
public UnitOfWork(... , ISampleRepository sampleRepository)
{
// Baseline
...
// Inject repositories here
SampleRepository = sampleRepository;
}
// Add repositories here
public ISampleRepository SampleRepository { get; }
// End of adding repositories
Register that repo again in src/Infrastructure/ConfigureServices.cs
:
public static class ConfigureServices
{
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)
{
...
services.RegisterRepositories();
return services;
}
private static IServiceCollection RegisterRepositories(this IServiceCollection services) { // Always register with scoped lifetime here services.AddScoped<ISampleRepository, SampleRepository>(); return services; } }
/src/Application/Entities/Commands/Function/FunctionCommand
. src/Application/Tests/Commands/GetTests/GetTestsCommand
, the same with queriesGetTestsCommand.cs
to the folder created:
public record GetTestsCommand : IRequest<int>
{
}
- A command/query implements an IRequest< TResult> with TResult being the result of the operation you want
- You can use either record or class, but record is more preferred
- Add any properties needed for the operation
- Add a request handler to the same file:
```c#
public record GetTestsCommand : IRequest<int>
{
}
public class GetTestsCommandHandler : IRequestHandler<GetTestsCommand, int>
{
private readonly IUnitOfWork _uow;
public GetTestsCommandHandler(IUnitOfWork uow)
{
_uow = uow;
}
public async Task<int> Handle(GetTestsCommand request, CancellationToken cancellationToken)
{
// Handle
}
}
_uow
in your Handle
method:
public async Task<int> Handle(GetTestsCommand request, CancellationToken cancellationToken)
{
return await _uow.SampleRepository.GetTests();
}