mercredi 23 juin 2021

What to do in "cyclic import" situation using DDD and Go?

I'm learning DDD and Golang.

I have a big doubt.

I'm building an app for tennis players and I have structured files like this:

pkg/reservation
  /app
    /query
      /get_reservation.go
    /command
      /create_reservation.go
    /app.go
  /domain
    /reservation
      /repository.go
      /reservation.go
  /adapters
    /postgres
      /repository.go
      /reservation.go
  /ports
    /http
      /reservation.go
      /service.go

pkg/game
  /app
    /query
      /get_game.go
    /command
      /create_game.go
    /app.go
  /domain
    /game
      /game.go
      /repository.go
  /adapters
    /postgres
      /game.go
      /repository.go
  /ports
    /http
      /game.go
      /service.go

pkg/player/... is the same

with /pkg/reservation/domain/reservation/reservation.go:

import (
  "pkg/game/domain/game"
  "pkg/game/domain/player"
)

type Reservation struct {
  id int

  start time.Time
  end time.Time

  game *game.Game
  player *player.Player
}

and /pkg/game/domain/game/game.go:

import (
  "pkg/reservation/domain/reservation"
  "pkg/game/domain/player"
)

type Game struct {
  id int

  finalScore string

  reservation *reservation.Reservation
  players []player.Player
}

At least for the moment this application is a simple CRUD and perhaps this structure is excessive. But soon the application will grow and I would like to be ready.

As you can see there is a cyclic import:

reservation.go imports pkg/game/domain/game and game.go imports pkg/reservation/domain/reservation.

The big doubt is how to fix this cyclic import.

As read here I can:

  • duplicate Game and Reservation structs: but I don't like this very much because they are perfectly the same, but maybe I'm wrong here;

  • use everywhere IDs only: but this is not a fix, because sometimes I really need *Reservation in my Game struct.

What do you recommend?

This is a very common situation I think.

Aucun commentaire:

Enregistrer un commentaire