Author:
Daniel Antos
I believe that we all have good intentions.
And even the flawed approach is better than inactivity.
I was working on a feature. There was this architectural issue. One thing led to the other, and...
3 stages
Domain Driven Design?
bids.go seems more important
func init() {
var bids auction.Bids
var tricks int
contract := bids[len(bids)-1]
CountPoints(contract, tricks)
}
func CountPoints(
contract auction.Bid,
tricks int,
) int {
return 0
}
Nothing stops us from passing the wrong thing
// Domain
type Contract Bid
func (bs Bids) Contract() Contract
// Scoring
func init() {
var bids auction.Bids
var tricks int
CountPoints(bids.Contract(), tricks)
}
func CountPoints(
contract auction.Contract,
tricks int,
) int {
return 0
}
Little work to create a new type, but developer will be warned about a wrong type usage
package scoring
var tricks int
contract := infra.Repo{}.Find().Contract()
CountPoints(contract, tricks)
package scoring
var tricks int
contract := infra.Logs{Repo: infra.Repo{}}.
Find().Contract()
CountPoints(contract, tricks)
package scoring
var tricks int
contract := infra.New().Find().Contract()
CountPoints(contract, tricks)
package infra
func New() Repo {
return logs{Repo: repo{}}
}
type Repo interface {
Find() internal.Bids
}
// ...
We can restrict developers from misusing our creations
package scoring
import (
"bridge/pkg/auction"
"bridge/pkg/auction/api"
)
func init() {
var tricks int
contract := api.API{}.FindContract()
CountPoints(contract, tricks)
}
package scoring
import (
"bridge/pkg/auction"
// compilation error!
"bridge/pkg/auction/internal/infra"
)
func init() {
var tricks int
contract := infra.New().Find().Contract()
CountPoints(contract, tricks)
}
Or even hide entire packages!
package auction
type Contract = internal.Contract
// ...
package scoring
import (
"bridge/pkg/auction"
)
func init() {
var tricks int
contract := auction.API{}.FindContract()
CountPoints(contract, tricks)
}
func CountPoints(
contract auction.Contract,
tricks int,
) int {
return 0
}
Let's have some questions!
☝️My blog
danielantos.com
☝️Slides