#circular dependency

2 messages · Page 1 of 1 (latest)

inland stump
#

i am trying to build a modular monolith.
each module has its own interface, struct etc.

import employeeService "myproject/internal/employee/service"
type AuthService interface {
    SignUp(ctx context.Context, signUpRequest requests.SignUpRequest) (userId string, companyId string, role sharedModels.Role, err error)
    CreateUser(ctx context.Context, tx *gorm.DB, createUserRequest *requests.UserCreationRequest) (user *models.User, err error)
}

type authService struct {
    cfg                 *config.Config
    userRepo            repository.UserRepository
    employeeService     employeeService.EmployeeService
    logger              *log.Logger
}

func NewAuthService(cfg *config.Config, userRepo repository.UserRepository, employeeService employeeService.EmployeeService, logger *log.Logger) AuthService {
    return &authService{
        cfg:                 cfg,
        userRepo:            userRepo,
        employeeService:     employeeService,
        logger:              logger,
    }
}

and

import authService "myproject/internal/auth/service"
type EmployeeService interface {
    CreateUser(....
}

type employeeService struct {
    cfg          *config.Config
    employeeRepo repository.EmployeeRepository
    authService  authService.AuthService
    logger *log.Logger
}

func NewEmployeeService(cfg *config.Config, employeeRepo repository.EmployeeRepository, authService authService.AuthService, logger *log.Logger) EmployeeService {
    return &employeeService{
        cfg:          cfg,
        employeeRepo: employeeRepo,
        authService:  authService,
        logger: logger,
    }
}

I think maybe creating a new package called interfaces and moving the interfaces into it and then importing from there may work. But this would make me move my domain based packed logic move into "shared" packages.
Any idea ?

Thank you.

forest hedge
#

there are several ways to solve it.

  1. you can make a common service which is used in both auth and employee services
  2. in AuthService, inject employeeRepo instead of employeeService

i do have a question actually, why do u need employeeService to be injected in authService? In my opinions, you should keep authService clean, as in it shouldn't use any business service inside it. purely for authentication operation.
if you need to register or insert any data for the employee,
i suggest the flow into:
EmployeeService -> AuthService

I hope it helps. 👍