0

1.When an agent is created, a token is automatically generated.

2.When a token is created, a corresponding UI is also automatically generated.

3.Since the token and UI are tightly coupled, a combined TokenWithUiService was created (perhaps considered a use case).

4.When deleting a token, you need to check permissions, which requires accessing the AgentRepository to verify whether the user has the right to perform the operation. (This is because the token itself doesn’t contain enough information to validate permissions.)

public async deleteToken(user: Users, agenitId: string, tokenId: string) {
const agent = await this.agentRepository.getAgent(user, agentId);
if (!agent) {
  throw new BusinessLogicException('You do not have sufficient permissions.');
  }
}

Object model

The AgentToken module directly accessing the AgentRepository feels architecturally inappropriate. To improve this, I tried switching to use AgentService.getAgent() to perform the permission check instead. However, doing this introduces a circular dependency between AgentService and AgentToken, which is also undesirable.

1.Since everything is part of the same "Agent" domain in a broad sense, it might not be inherently wrong for AgentToken to access AgentRepository directly. So, perhaps there’s no real need to go through AgentService to perform the permission check.

→ However, it still feels structurally wrong, and I’m not fully confident about this approach.

  1. Maybe the real issue lies in how AgentTokenService handles authorization. The fact that it depends on external services for things like permission checking during token deletion or modification might point to a design issue.

→ But on second thought, this idea doesn’t hold up either — external dependency for things like permission and validation is perfectly natural. In this case, since a token fundamentally "belongs" to an agent, depending on Agent for permission checking makes sense.

  1. Similar to how we created a TokenWithUiService, we could simply create use case services that orchestrate the creation of agents, tokens, and UIs.

→ At one point, I started wondering if this “explosion of use case services” is a poor design choice.

Another object model

  1. But thinking in terms of object-oriented design (like DDD, Clean Architecture, etc.), this kind of separation and orchestration through use case services feels like the right way to structure things and to resolve circular dependencies. That said, maybe the root problem is that I haven’t come up with a truly solid solution yet.

I’m still unsure whether option 1 or 2 is definitively wrong. Option 3 feels like a reasonably good solution. Ultimately, I’m starting to realize the need to study object-oriented design principles, especially DDD, Clean Architecture, and the ideas of separating domain, use cases, and persistence layers. I’m not sure if my current approach is "wrong" simply because I lack a solid understanding of these concepts.

1
  • Yes, study OO design principles - start with SOLID (its an acronym). en.wikipedia.org/wiki/SOLID There's plenty of good videos online that will help explain them to you. Commented Apr 29 at 3:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.