Organizing By Layer
The first approach to organizing our code is by layer. We might organize the code like this:
For each of our layers – web, domain, and persistence – we have a dedicated package. As discussed in Chapter 2, What’s Wrong with Layers?, simple layers may not be the best structure for our code for several reasons, so we have already applied the Dependency Inversion Principle here, only allowing dependencies toward the domain code in the domain package. We did this by introducing the AccountRepository interface in the domain package and implementing it in the persistence package.
We can find at least three reasons why this package structure is suboptimal, however:
- First, we have no package boundary between functional slices or features of our application. If we add a feature for managing users, we’ll add a UserController to the web package; a UserService, UserRepository, and User to the domain package; and...