
import (
"errors"
"fmt"
)
//entity: plain fields, no base type and no framework
type OrderLine struct {
SKU string
Price int
Count int
}
//go has no exceptions: a broken rule is a value
var errBadCount = errors.New("bad count")
//a line that breaks the rule is never built at all
func NewLine(sku string, price, n int) (OrderLine, error) {
if n <= 0 {
return OrderLine{}, errBadCount
}
return OrderLine{SKU: sku, Price: price, Count: n}, nil
}
//value receiver: counting changes nothing
func (l OrderLine) Cost() int {
return l.Price * l.Count
}
//entity: lines keep their order, so a slice and not a map
type Order struct {
ID int
lines []OrderLine
paid bool
}
//pointer receiver: the order itself changes here
func (o *Order) Add(line OrderLine) {
o.lines = append(o.lines, line)
}
func (o Order) Total() int {
sum := 0
for _, line := range o.lines {
sum += line.Cost()
}
return sum
}
//business rule: 10% off from 200
func (o Order) Discount() int {
if o.Total() >= 200 {
return o.Total() / 10
}
return 0
}
func (o Order) Payable() int {
return o.Total() - o.Discount()
}
//business rule: an empty order is never paid
var errEmpty = errors.New("empty order")
func (o *Order) Pay() error {
if len(o.lines) == 0 {
return errEmpty
}
o.paid = true
return nil
}
func (o Order) Paid() bool {
return o.paid
}
func (o Order) Size() int {
return len(o.lines)
}
//client code
//no database and no screen anywhere above
order := &Order{ID: 1001}
wanted := []OrderLine{
{SKU: "pen", Price: 30, Count: 2},
{SKU: "lamp", Price: 190, Count: 1},
}
for _, w := range wanted {
line, err := NewLine(w.SKU, w.Price, w.Count)
if err != nil {
fmt.Println("refused:", err)
continue
}
order.Add(line)
}
fmt.Printf("order %d\n", order.ID)
fmt.Printf("lines %d\n", order.Size())
fmt.Printf("total %d\n", order.Total())
fmt.Printf("discount %d\n", order.Discount())
fmt.Printf("payable %d\n", order.Payable())
if err := order.Pay(); err == nil {
fmt.Printf("paid %v\n", order.Paid())
}
//a wrong count never becomes a line
if _, err := NewLine("pen", 30, 0); err != nil {
fmt.Println("refused:", err)
}
//an empty order refuses to be paid
empty := &Order{ID: 1002}
fmt.Println("refused:", empty.Pay())
//printed: order 1001
// lines 2
// total 250
// discount 25
// payable 225
// paid true
// refused: bad count
// refused: empty order
An entity holds a business rule of the shop and knows nothing about a database, a screen or the network, so it can be tested without them.Read moreThe problem. A shop gives 10% off every order from 200. If this rule lives in a web page, and again in a database query, the two copies soon disagree. To check the rule, you must start the page or the database. The most important knowledge of the shop is scattered and hard to test. The idea. An entity is a piece of data together with the business rules about it: what an order contains, how its total is counted, when it may be paid. It knows nothing about a database, a screen or the network. Clean architecture has one main rule, the dependency rule: outer code may know inner code, but inner code never knows outer code. Entities are the innermost code, so they depend on nothing. How to read the example. There are two entities: a line of an order and the order itself. The order sums its lines, gives 10% off from 200 and counts the amount to pay. It refuses to be paid while it is empty. The client code builds order 1001 with two lines and prints its numbers: the total is 250, the discount 25 and the amount to pay 225. It also tries to pay an empty order, and the entity refuses. In some languages the entity is a plain value with functions, not a class; its role is the same. When to use it. Make an entity for each concept of the business that has its own rules, such as an order or an account. Data without rules needs no entity: a simple structure is enough. A common mistake. An entity that saves itself to the database or formats itself for the screen. Then it depends on outer code, and its rules can no longer be tested alone. |