I'm currently trying to build my app around the Domain driven design , entities , services , repos,...
All the basic crud operation are simple where basically 1 entity => 1 table => 1 repository => 1 service
But i can't figure out the cleanest way to handle join table between two entities.
It would be possible to make 1 query by table inside of the join and it would be " clean" ( so to say ) but it wouldn't be efficient as a simple join would have resulted in one query.
Where do tables join live in this pattern ?
I have been thinking of building now entities that would encapsulate the answer but that would effectivitly create 1 entity + repository for just 1 query...
I'm also thinking that merging multiple entities into a single interface might partially solve it but it would result in many empty param of my entities ( it's rare that you require ALL field from ALL tabes when you're performing a join )
What is the proper way/pattern to solve this issue that would fit in the DDD or at least be clean ?
-- Edit example :
type User struct {
ID int `db:"id"`
ProjectID int `db:"project_id"`
RoleID int `db:"role_id"`
Email string `db:"email"`
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Password string `db:"password"`
}
type UserRepository interface {
FindById(int) (*User, error)
FindByEmail(string) (*User, error)
Create(user *User) error
Update(user *User) error
Delete(int) errorr
}
type Project struct {
ID int `db:"id"``
Name string `db:"name"`
Description string `db:"description"`
}
Here i have a simple user repository. I have something similar for the "Project" table. can create table , get all info on the project , delete , etc etc.
As you can see UserID has the foreign key of the project ID it belongs to.
My issue is when i need to retrieve all the information from the user and , say the " project name " and description. ( i reality the table/entity has much more parameters )
I need to do a simple join in user.project_id and project.id and retreive all the information of the user + project name + description in one query.
Sometimes it's more complex because there will be 3-4 entities linked like this. ( user , project , project_additional_information, roles , etc)
Ofcourse i could make N queryies, one per entity.
user := userRepo.Find(user_id)
project := projectRepo.FindByuser(user.deal_id)
And that would "Work " but i'm trying to find a way to do it in one query. since a simple sql join on user.project_id and project.id would give me all data in on query.