liveBook · Manning

Business logic

As explained in Practices of the Python Pro

A business logic layer is a software layer that contains application rules and operations governing how inputs are transformed into business outcomes.

Business logic contains the application’s task-specific rules—the “smarts” that turn user choices and input into useful outcomes. It connects the presentation layer, which communicates with users, to the persistence layer, which stores data. section 6.3 section 10.3

A clean design keeps business logic separate from menus, screens, and database details. This separation lets each layer evolve independently and makes the application easier to maintain. section 6.3 section 6.4

In a bookmark application, business logic appears as commands with a consistent execute operation:

  • Create the bookmarks table by asking the persistence layer to create the required fields. section 6.3.2
  • Add a bookmark by supplying a timestamp when needed, storing the data, and reporting the result. section 6.3.2
  • List bookmarks by selecting records with a configurable ordering. section 6.3.2
  • Delete a bookmark by passing its identifier to the persistence layer. section 6.3.2

A command mediates an application action

class AddBookmarkCommand: def execute(self, data): data['date_added'] = datetime.utcnow().isoformat() db.add('bookmarks', data) return 'Bookmark added!' The command owns the action’s behavior while the presentation layer only needs to trigger execute. A later refinement returns a status and result, allowing presentation code to control user-facing messages. section 6.3.2 section 10.4.1

The command pattern reduces coupling between presentation and business logic: menu options invoke commands without knowing their internal work, while commands need only the data they receive. Interfaces can also reduce coupling to persistence details. section 6.3.2 section 10.3 section 10.4.1

Figure 6.7. The business logic layer determines when and how data is read from or written to the persistence layer.

Use the layer diagram to trace how business logic decides when and how data moves between presentation and persistence. section 6.3.2

How Practices of the Python Pro covers it

How deep it goes
Demonstrates commands for CRUD operations and discusses coupling, while deferring broader implementation beyond the application example.
Examples
Uses Bark’s bookmark commands for creating tables, adding, listing, deleting, and importing or extending application functionality.
What it assumes
Assumes basic application inputs and databases, while teaching layered separation, commands, and loose coupling through a small bookmark application.
What you can do after
Build it from scratch by separating application actions into commands that coordinate presentation input and persistence operations.
How it connects
Connects business logic with presentation layers, persistence layers, separation of concerns, encapsulation, abstraction, loose coupling, and the command pattern.

Also explained in

Compare how 10 books explain business logic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.