Skip to content

Repository files navigation

ProFile - Backend

To create new repository

Steps

  • Add new repository interface to src/Application/Common/Interfaces/Repositories including necessary operations
publicinterfaceISampleRepository{Task<int>GetTests();}
  • Add a property of that interface type to src/Application/Common/Interfaces/IUnitOfWork.cs like this:
publicinterfaceIUnitOfWork:IDisposable{
...ISampleRepositorySampleRepository{get;}
...}
  • Implement that repo interface in src/Infrastructure/Repositories/YourRepository.cs
publicclassSampleRepository:ISampleRepository{privatereadonlyIDbConnection_connection;privatereadonlyIDbTransaction_transaction;publicSampleRepository(IDbConnectionconnection,IDbTransactiontransaction){_connection=connection;_transaction=transaction;}publicasyncTask<int>GetTests(){varsql="CREATE TABLE Documents(id INTEGER)";returnawait_connection.ExecuteAsync(sql,transaction:_transaction);}}
  • THIS IS A BIG NOTE: Always inject IDbConnection into the repository, if your repo include command operations (Add, Update, Delete) then inject IDbTransaction also, and do as sample
  • With query operations: Use QueryAsync<>()
  • With command operations: Use ExecuteAsync()
  • Register that repo in unit of work:
publicclassUnitOfWork:IUnitOfWork{
...publicUnitOfWork(...,ISampleRepositorysampleRepository){// Baseline
...// Inject repositories hereSampleRepository=sampleRepository;}// Add repositories herepublicISampleRepositorySampleRepository{get;}// End of adding repositories
  • Register that repo again in src/Infrastructure/ConfigureServices.cs:
publicstaticclassConfigureServices{publicstaticIServiceCollectionAddInfrastructureServices(thisIServiceCollectionservices,IConfigurationconfiguration){
...services.RegisterRepositories();returnservices;}privatestaticIServiceCollectionRegisterRepositories(thisIServiceCollectionservices){// Always register with scoped lifetime hereservices.AddScoped<ISampleRepository,SampleRepository>();returnservices;}}

To use repository with MediatR and CQRS

Steps

  • To create a command/query related to an entity: create folders in structure of /src/Application/Entities/Commands/Function/FunctionCommand.
    For example: src/Application/Tests/Commands/GetTests/GetTestsCommand, the same with queries
  • Add a GetTestsCommand.cs to the folder created:
publicrecordGetTestsCommand: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:
publicrecordGetTestsCommand:IRequest<int>{}publicclassGetTestsCommandHandler:IRequestHandler<GetTestsCommand,int>{privatereadonlyIUnitOfWork_uow;publicGetTestsCommandHandler(IUnitOfWorkuow){_uow=uow;}publicasyncTask<int>Handle(GetTestsCommandrequest,CancellationTokencancellationToken){// Handle}}
  • This handler must implement IRequestHandler with type parameters being
    • The command/query you created
    • The TResult you used in the IRequest implementation
  • Remember to inject the IUnitOfWork interface, as this interface is your single point of entry to operate on your databases
  • Use this _uow in your Handle method:
publicasyncTask<int>Handle(GetTestsCommandrequest,CancellationTokencancellationToken){returnawait_uow.SampleRepository.GetTests();}
  • As your repository has been registered, and all services needed for your repositories and unit of work have also been registered, you can safely call the abstract methods without worrying about DI not working

About

No description, website, or topics provided.

Resources

Stars

5 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages