0

Microservices

  • Identity
    • Api
    • Application
      • Interfaces
        • IUserService.cs
      • Services
        • UserService.cs
    • Data
    • Domain
      • CommandHandlers
        • CreateUserCommandHandler.cs
      • Commands
        • CreateUserCommand.cs
      • Interfaces
        • IUserRepository.cs
      • Events

Following this approach I cannot inject IUserService interface into my CreateUserCommandHandler (circular ref). Obviously this is not right way, how would you restructure this folder organization?

public class CreateUserCommandHandler : IRequestHandler<CreateUserCommand, bool>
{
     private readonly IEventBus _bus;
     // private readonly IUserService _userService; cannot access this

      public CreateUserCommandHandler(IEventBus bus)
      {
          _bus = bus;
      }

      public Task<bool> Handle(CreateUserCommand request, CancellationToken cancellationToken)
      {
         _bus.Publish(new UserCreatedEvent(request.Name, request.Email));
          return Task.FromResult(true);
      }
}
user1765862
  • 13,635
  • 28
  • 115
  • 220

1 Answers1

2

Handler classes is typically part of the application project.

Domain entities should not have any dependencies to any other project. They should be considered to be the heart of your project. Their single responsibility is to wrap the business logic and make sure that their state always is consistent.

Services can mean both application services and domain services. Domain services should be part of the domain project. Domain vs application services

I would put command and event classes in the API project.

So in your case, the Domain project would have a User class that wraps all business logic for users, the repository interface to represent the persistence abstraction and finally a User domain service to be able to coordinate logic between multiple user entities.

jgauffin
  • 99,844
  • 45
  • 235
  • 372