I'm very new to DDD and i'm trying to implement my use case properly.
I have multiple entities that are all tied together, much like the usual aggregate example of an Order which encapsulates LineItem.
From what I understand from basic DDD I'm inclined to create 2 entities, one for an Order and another for a Line Item, but then there seem to be 2 options going from there:
Have both repositories return
Tx, instantiate them all into another interface/struct as an "aggregate" (probably not the correct name) to make the transaction to create a new orderCreate a new repository that would then do transactions on it's own for the whole scope of the Order ( creating the order + line items )
For me the 2nd option seems much easier to implement than the first one. I can make joins, tx, or anything needed in this repository to retrieve data from both tables ( order and lines items ) and also can create transaction and ensure their data integrity. But I'm not sure if it's good practice.
"pseudocode" would look like this :
type Order struct {
ID int
lineItems []LineItem
...
}
type LineItem struct {
ID int
...
}
type OrderRepoistory interface {
GetOrders()([]*Order,err)
GetOrder(id int)(*Order,err)
Create(order *Order) err
}
And inside the OrderRepository implementation the create function would look something like this:
func Create(order *Order) err {
tx := BeginTX
insert order into the orders table
insert the lineitems into the line_items table
tx.commit or rollback
return nil or err
}
func GetOrder(orderId int)*Order {
var order Order
var items []*LineItem
row,_ := db.Query("select * from orders where id = $1",orderId)
row.Scan(&order)
rows ,_ := := db.Query("select * from line_items where order_id = $1",orderId)
loop and get append each rows to items slice
// copy slice to
order.LineItems = items
return &order
}
So both Create and Getorder of the repository include queries on both tables to retrieve one Order.
Does this implementation make sense as it seems very broad for an "entity"? If not, what's the proper way to make transaction and queries over multiple tables at once?