
;; Entity folds into a plain value plus the functions that
;; hold its rules: no class, and no database or screen here
;; Entity
(defn order-line [sku price qty]
(when-not (pos? qty)
(throw (ex-info "bad count" {:sku sku :qty qty})))
{:sku sku :price price :qty qty})
(defn cost [line]
(* (:price line) (:qty line)))
;; Entity
(defn new-order [id]
{:id id :lines [] :paid false})
(defn add-line [order sku price qty]
(update order :lines conj (order-line sku price qty)))
(defn total [order]
(reduce + 0 (map cost (:lines order))))
;; Business rule: 10% off from 200
(defn discount [order]
(let [sum (total order)]
(if (>= sum 200)
(quot sum 10)
0)))
(defn payable [order]
(- (total order) (discount order)))
;; Business rule: an empty order is never paid
(defn pay [order]
(when (empty? (:lines order))
(throw (ex-info "empty order" {:id (:id order)})))
(assoc order :paid true))
(defn paid? [order]
(:paid order))
;; Client code
(def order
(-> (new-order 1001)
(add-line "pen" 30 2)
(add-line "lamp" 190 1)))
(println "order" (:id order))
(println "lines" (count (:lines order)))
(println "total" (total order))
(println "discount" (discount order))
(println "payable" (payable order))
;; Paying gives a new value; the old one stays as it was
(def paid-order (pay order))
(println "paid" (paid? paid-order))
(println "before paying" (paid? order))
;; The same rule on a small order: no discount at all
(def small
(-> (new-order 1002)
(add-line "pen" 30 2)))
(println "order" (:id small)
"total" (total small)
"discount" (discount small))
;; Nothing locks the map: (assoc order :paid true) would
;; pass. The rule holds because every layer calls pay,
;; so the rules live in one place and nowhere else
(defn attempt [f]
(try
(f)
(catch clojure.lang.ExceptionInfo error
(println "refused:" (ex-message error)
(ex-data error)))))
(attempt #(pay (new-order 1003)))
(attempt #(add-line (new-order 1003) "pen" 30 0))
;; printed: order 1001
;; lines 2
;; total 250
;; discount 25
;; payable 225
;; paid true
;; before paying false
;; order 1002 total 60 discount 0
;; refused: empty order {:id 1003}
;; refused: bad count {:sku pen, :qty 0}
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. |