Entity

Design patterns / Clean architecture patterns
image

//C has no vector: an order keeps its lines in an array
//of fixed size and counts them
constexpr size_t maxLines = 8;

//entity
typedef struct {
    char sku[16];
    int price;
    int count;
} OrderLine;

int lineCost(const OrderLine *line) {
    return line->price * line->count;
}

//entity: C has no private fields, so the struct is open
//for reading, and only the functions below change it
typedef struct {
    int id;
    OrderLine lines[maxLines];
    size_t size;
    bool paid;
} Order;

Order orderMake(int id) {
    return (Order){.id = id};
}

//C has no exceptions: a rule that refuses returns the
//reason, and nullptr means done
const char *orderAdd(Order *order, const char *sku,
                     int price, int count) {
    if (count <= 0) {
        return "bad count";
    }
    if (order->size == maxLines) {
        return "too many lines";
    }
    OrderLine *line = &order->lines[order->size++];
    snprintf(line->sku, sizeof line->sku, "%s", sku);
    line->price = price;
    line->count = count;
    return nullptr;
}

int orderTotal(const Order *order) {
    int sum = 0;
    for (size_t i = 0; i < order->size; i++) {
        sum += lineCost(&order->lines[i]);
    }
    return sum;
}

//business rule: 10% off from 200
int orderDiscount(const Order *order) {
    int total = orderTotal(order);
    return total >= 200 ? total / 10 : 0;
}

int orderPayable(const Order *order) {
    return orderTotal(order) - orderDiscount(order);
}

//business rule: an empty order is never paid
const char *orderPay(Order *order) {
    if (order->size == 0) {
        return "empty order";
    }
    order->paid = true;
    return nullptr;
}

//client code
//no database and no screen anywhere above, and no
//header beyond the standard library
Order order = orderMake(1001);
orderAdd(&order, "pen", 30, 2);
orderAdd(&order, "lamp", 190, 1);
printf("order %d\n", order.id);
printf("lines %zu\n", order.size);
printf("total %d\n", orderTotal(&order));
printf("discount %d\n", orderDiscount(&order));
printf("payable %d\n", orderPayable(&order));
orderPay(&order);
printf("paid %s\n", order.paid ? "true" : "false");

//a line that breaks the rule never gets into the order,
//so the order below stays empty and is refused too
Order empty = orderMake(1002);
const char *reason = orderAdd(&empty, "pen", 30, 0);
if (reason != nullptr) {
    printf("refused: %s\n", reason);
}
reason = orderPay(&empty);
if (reason != nullptr) {
    printf("refused: %s\n", reason);
}
//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 more

The 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.