DDD: service contains two repository

by tikhop   Last Updated January 10, 2018 14:05 PM

Does it correct way to have two repository inside one service and will it be an application or domain service?

Suppose I have a Passenger object that should contains Passport (government id) object. I am getting Passenger from PassengerRepository. PassengerRepository create request to server and obtain data (json) than parse received data and store inside repository.

I have confused because I want to store Passport as Entity and put it to PassportRepository but all information about password contains inside json than i received above.

I guess that I should create a PassengerService that will be include PassengerRepository and PassportRepository with several methods like removePassport, addPassport, getAllPassenger and etc.

UPDATE:

So I guess that the better way is represent Passport as VO and store all passports inside Passenger aggregate. However there is another question: Where I should put the methods (methods calls server api) for management passenger's passport. I think the better place is so within Passenger aggregate.



Answers 2


It's not unheard of for a service to have two repositories, but it is often a hint of a poor design. It's worth looking at, to see if you could improve your design but, if you look and you can't improve it, you shouldn't worry too much.

In your case, I think you need to look at the concept of Aggregates in DDD.

Aggregates are groups of things that belong together. An Aggregate Root is the thing that holds them all together.

If Passenger and Passport don't belong together, I don't know what does. Your aggregate root in this case should obviously be Passenger.

pdr
pdr
November 07, 2012 14:10 PM

Whats up!!

Well, I'm totally agree with @pdr, that you don't need to blame yourself because an additional repository into your service.

About the passport' methods, such as addPassport, I think they should stay either in PassengerService and in Passport Repository. Something like:

public class PassengerService : ServiceBase<Passenger>, IPassengerService {

    private readonly IPassportRepository _passport_repository;
    private readonly IPassengerRepository _pass_repository

    public PassengerService(IPassportRepository passport_repository,     IPassengerRepository passenger_repository) {
        ...

    }


   public void removePassport(...) {
       _passport_repository.remove(...);

   }


}

:-)! Sorry about my English!

Peace.

Gabriel Lopes
Gabriel Lopes
January 10, 2018 13:51 PM

Related Questions