CzarnaWoda
CzarnaWoda

Reputation: 33

Restrict class to specific packages with Spring Modulith

I want to create an application using Spring Boot Modulith with a DDD (Domain-Driven Design) approach in the infrastructure layer. I am having issues with class visibility. In a Spring Modulith setup, I believe I shouldn't have any public classes related to Spring Boot implementations, but I am unsure how to achieve this.

In the domain layer, it’s impossible to make classes private because I need to use them in the infrastructure layer. However, I want to know if there is a way to hide certain classes, like mappers, for example. I need a mapper to convert objects between database entities (UserEntity in the infrastructure layer) and domain models (User in the domain layer). Currently, I am only using this mapper in UserRepositoryImpl (which implements UserRepository from the domain layer).

Is there a way to restrict access to a class like the mapper so that it is only visible within the infrastructure package? In the attached image, you can see the current structure (where '+' = public and 'o' = package-private).

module: user : me.example.com.user
+ ….domain.exception.InvalidEmailException
+ ….domain.exception.InvalidPasswordException
+ ….domain.exception.InvalidPhoneValueException
+ ….domain.exception.InvalidUserNameException
+ ….domain.exception.UserNotFoundException
+ ….domain.model.ContactCredentials
+ ….domain.model.Email
+ ….domain.model.Phone
+ ….domain.model.User
+ ….domain.model.enums.UserType
+ ….domain.repository.UserRepository
+ ….domain.service.UserService
+ ….infrastructure.model.ContactCredentialsEntity
+ ….infrastructure.model.EmailEntity
+ ….infrastructure.model.PhoneEntity
+ ….infrastructure.model.UserEntity
o ….infrastructure.repository.UserEntityMapper
o ….infrastructure.repository.UserJpaRepository
o ….infrastructure.repository.UserRepositoryImpl
o ….infrastructure.service.UserServiceImpl

I considered placing the mapper in the repository package, but I believe that would not be in line with DDD principles. Therefore, I am exploring alternative ways to implement it.

So, my main question is the following: Is there any way to hide for example UserEntityMapper to be only accessed only among User module (still used by repository class) and if there is, how can i do that?

Upvotes: 0

Views: 116

Answers (0)

Related Questions