sarulabs / di-example

MIT License
10 stars 4 forks source link

Circular Dependency Issue with multiple models #1

Open LordBoBCUP opened 3 years ago

LordBoBCUP commented 3 years ago

Hi,

Using this project layout how do you propose to handle the circular dependency issue of multiple different models such as say car and perhaps a driver? Using that as an example, a driver could drive many cars therefore a car can be driven by many people. Using an ORM like GORM would require you to import the garage model package and the driver model package into each other causing a circular dependency to create tables, do associated queries etc. You can chuck everything into one package (likely to grow very large for a decent sized project) which isn't an ideal design. How would you properly handle this in a project?

sarulabs commented 3 years ago

Hello,

I'm not sure to understand your problem. Do you have something like this:

type Car struct {
    Drivers []*Driver
}

type Driver struct {
    Cars []*Car
}

As shown in https://gorm.io/docs/many_to_many.html#Back-Reference

In this case the structures can not be in different packages because they depend on each other. If you want two separate packages you need to remove a dependency.