0

I have two aggregate roots: User and Sketches.

In my application, users can clap for a Sketch. In the service layer, it looks as so:

class SketchService {
  constructor ({ SketchRepository }) {
    this.sketchRepository = SketchRepository
  }
  
  async addClaps ({ sketchID, author, count }) {
    const sketch = await this.sketchRepository.findById(sketchID)
    
    sketch.addClaps(author, count)
    
    await this.sketchRepository.update(sketch)
  }

  // other use cases
  // ...

This works great. But what if every User has a karma and every time a user claps for a sketch, the author should receive +1 karma?

Now the issue is that SketchService would have to now contain UserRepository; I am not really sure if that's a good thing.

Then it would look like this:

class SketchService {
  constructor ({ SketchRepository, UserRepository }) {
    this.sketchRepository = SketchRepository
    this.userRepository = UserRepository
  }
  
  async addClaps ({ sketchID, author, count }) {
    const sketch = await this.sketchRepository.findById(sketchID)
    const sketchAuthor = await this.userRepository.findByUsername(sketch.author)

    sketch.addClaps(author, count)
    sketchAuthor.addKarma(count)
    
    await this.sketchRepository.update(sketch)
    await this.userRepository.update(sketchAuthor)
  }

I am new to DDD so I am not sure if this would be the best approach. Should I use domain events? Is using two repositories like in this situation a good approach?

Thank you.

1 Answer 1

3

Adding two responsibilities to the addClaps methods is a violation of the single responsibility principle.

I think going with domain events is a good way. It would allow you to centralize all the logic giving karma. It is not hard to imagine that a user could get karma in different ways than receiving claps. For example, being posting sketches or being an active user in other ways. Chasing that logic across many different services is going to be difficult when the system grows.

Domain events will also make it easy to make a log available to the user to showcase where the karma originated.

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.