Application Logic
Use cases
- Definition
- Application logic implements the use cases of the application. It doesn't have any volatile dependencies.
Application logic is part of the Controller in MVC, and Control in ECB
How does it work?
Application logic implements a use case: a series of steps that a user goes through to fulfill an objective. If a use case is regarded as a state machine, then application logic makes the application transition from one state to the next, repeatedly. In this process data is accumulated and transformed It is also passed to/from the ui, the database and to other services, but the actual execution of these parts of the application is outside of the realm of application logic.
Domain logic is separated from the rest of the application (the boundary in ECB terms) to make it testable, understandable, and to discourage code duplication.
Application logic only depends on domain logic. All other dependencies are abstracted away by interfaces.
Volatile dependencies are services that don't behave the same in all circumstances. Examples: database, file system, sending email, random number generator, current time. A file may not exist, disk may be full, email may not be sent, etc. The opposite of volatile is stable. A stable depenency is dependable: it behaves the same in all circumstances.
Code that doesn't interact with any volatile dependency doesn't have any effects (except for taking up time). And domain logic can interact with many of these dependencies. But from the point of view of the domain logic, it just talks to a stable interface, and it doesn't know that this interface is implemented by a volatile service.
Single purpose logic and software library
Within application logic, we can distinguish between single-purpose logic and reusable logic.
Single purpose logic belongs to a single controller, component or view. It is formed by request handlers, event handlers, and initialization code, and can quickly become unwieldly.
Reusable logic is formed by a software library that provides functionality with a well-defined interface. A library function can be called multiple times. The software library code is usually located in a special directory called "service" or "lib" and contains a separate file for each library topic.
Apart from software reuse, an important benefit of the software library is that it keeps the single-purpose logic simple. By extracting parts of this code into well-defined functions and transferring these to the software library, the handler is kept simple. This can be done even when the handler is the only one using the function, that means: the library functions can be reused, but they don't have to be reused.
Examples
- User Authentication Logic
- Input Validation Logic
- Request Handling Logic
- Response Generation Logic
- Business Rule Execution Logic
- Session Management Logic
- Error Handling Logic
- Caching Logic
- Middleware Execution Logic
- Data Transformation Logic
- Concurrency Control Logic