Search code examples
domain-driven-designddd-repositories

DDD repository input parameters


Which is suggested way to implement _customRoleRepository in the following examples? Following code is executed in the application service.

var user = _userRepository.GetById(1);
var customRole = _customRoleRepository.GetById(user.CustomRoleId);

or

var user = _userRepository.GetById(1);
var customRole = _customRoleRepository.GetForUser(user);

Solution

  • Given the two options I would probably go for the first one, which keeps consistency of accessing by an ID.

    If possible, it might be preferable to load the custom role when you load the user to avoid another round trip to the database, especially if this is a common operation. This could be implemented as a read model.

    This is presuming you have modelled your aggregates correctly... :)