Search code examples
c#.net-coredesign-patternsmediatr

C# Mediatr Request Handler Injected with multiple Repositories


Im using MediatR in .Net Core and kind of confused if injecting multiple repositories to handle a business logics is an acceptable/clean way of doing it?

My sample Code:

public class MyRequestHandler: IRequestHandler<...>
{
  public IHeaderRepository _headerRepository;
  public IChildRepository _childRepository;
  ///constructor dependency injection happening here

  public async Task<...>Handle(.....request,..... cancellationToken)
  {
     var header = await _headerRepository.GetHeader(..headerId);
     if(header != null) await _headerRepository.Insert(...):

     await _childRepository.Insert(..., ...headerId)
  }
}

Solution

  • It looks like you are inserting just one aggregate. So you are not breaking "aggregate" contract. So as long as this condition is satisfied, yeah, it is eligible to read from other repositories.

    However, I think, the way to go is events. So when header is created, then event should be published and then MyRequestHandler should insert data by using childRepository.