2

I'd like to start by saying that I'm new to DDD and I'm creating a hobby system to practice some concepts, so this question might be trivial, but not so much for me.

I have the following scenario:

I have a given user that should be able to add exercise to the system, so I have two models User and Exercise, and I have a method addExercise which should create a new exercise and save it to database by using a repository. This method would mostly validate Exercise data before saving to database, but there would be some validations regarding the user (such as verify if exercise wasn't previously created by this user)

I have thought about three ways to do that:

  1. Add an addExercise method to User model. Problems I see in this approach:

    a) User model would need to know about Exercise in order to implement addExercise.

    b) User model would need a repository to save the Exercise in database.

  2. Add an addExercise method to UserService, since adding exercise is an operation from user, this seems ok, but when I start thinking about implementation, I would have UserService to know about Exercise and ExerciseRepository.

  3. Add an addExercise method to ExerciseService, which I believe it is fine, but I'm afraid I might be missing something.

So, in my opinion, from a business point of view, addExercise should be a method in User model, but when I think about implementation details, it seems to make more sense option 3), but I want to know if this is in fact the correct option.

Also, I have some other questions:

I) Is it normal for a given model A to be able to create another model B or have a repository from a model B inside of it ? Or could a model A receive a repository that work with model A ?

II) Is it ok for a service A to receive repositories that work with model B ?

3
  • Maybe you’re missing a concept. You say a user should be able to add an exercise to the system but perhaps you can make that more explicit. Model the relation between user and exercise.
    – Rik D
    Dec 22 '20 at 18:48
  • 1
    Btw user is a bad name imo. In the ubiquitous language, what do you call a person who has some relationship with exercises? An athlete perhaps?
    – Rik D
    Dec 22 '20 at 18:52
  • There are other models in the system, there is a Routine model where it is possible to add exercises, however, the exercises to be added in a routine should be created by the user/athlete. And actually, creating a routine falls in the same problem as described in this question.
    – Vinicius
    Dec 22 '20 at 19:01
3

You want to use your third option. In systems with users basically everything is user focused, but putting that functionality in a user model doesn't make sense in most cases. If exercise is a domain model you want, then the ability to add one 100% belongs to the exercise domain/service. The reason for this is because a domain model should be responsible for creating/updating itself. If exercise requires a reference to user that's fine, DDD allows for models to reference other models. The best practice is basically using the same concept as foreign keys in a database.

2
  • Thanks for your answer. Now that you mentioned, I wonder if having Exercise as a model makes sense. I have two other models, Workout and Routine, a Routine is made of several workouts and day to train and a workout is made of several exercises and sets and reps for each exercise. Both Routine and Workout have few attributes on its own, it seems more like a hybrid between aggregate and a model, I will definitely need Exercise, but I'm not sure if it is a model since it seems to have very little behavior, the same could be said about a Workout. The user should deal only with Routine.
    – Vinicius
    Dec 22 '20 at 21:27
  • I forgot to mention, the user can create Workout and Routine and add Exercise to the system, so it is not exactly dealing only with a Routine, however, it all seems to be only CRUD operations, there is very few logic in these models, I wonder if I'm using the correct approach for this problem, since the models are probably going to be very anemic.
    – Vinicius
    Dec 22 '20 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.