bxcodec / go-clean-arch

Go (Golang) Clean Architecture based on Reading Uncle Bob's Clean Architecture
MIT License
9.06k stars 1.19k forks source link

How to avoid cyclic dependencies? #7

Closed danf0rth closed 6 years ago

danf0rth commented 6 years ago

Hi!

Lets say, we have a:

package state

import "city"

type State struct {
    ID uint64
    Name string
    Cities []city.City // The list of all cities in this state
}

...

package city

import "state"

type City struct {
    ID uint64
    Name string
    State *state.State // The state to which the city belongs
}

There is a problem of cyclic import, how to handle this kind of situations? You can say, that we can put state and city into the same package, but... if we add more realistic cases and examples, this is not a solution anymore:

type Department struct {
    City *city.City // Where is department located
}

and modify type City struct to following:

type City struct {
     // previous fields
    Departments []department.Department
}

Thank you!

bxcodec commented 6 years ago

Hello there,

Sorry for late response, So after figuring this problem a few months ago, I make many changes to all my entire project. But I forgot to update this one.

You can see the proposed changes in #8