You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 here
SampleRepository =sampleRepository;}// Add repositories herepublicISampleRepositorySampleRepository{get;}// End of adding repositories
Register that repo again in src/Infrastructure/ConfigureServices.cs:
publicstaticclassConfigureServices{publicstatic IServiceCollection AddInfrastructureServices(thisIServiceCollectionservices,IConfigurationconfiguration){
...
services.RegisterRepositories();returnservices;}privatestatic IServiceCollection RegisterRepositories(thisIServiceCollectionservices){// Always register with scoped lifetime here
services.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
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