This article explores an architectural approach to externalize business rules from application code into a database using expression languages like MVEL. This strategy enhances system flexibility by enabling dynamic updates to logic (e.g., discount rates, loyalty points) without requiring code deployments. It highlights the benefits of separating data from code, focusing on reducing maintenance burdens and accelerating responsiveness to business changes.
Read original on DZone MicroservicesTraditional software development often embeds business rules directly into application code using conditional statements. While straightforward for simple logic, this approach becomes a significant maintenance burden as rules evolve. Any change, no matter how minor, necessitates a full development lifecycle: unit testing, code analysis, CI/CD pipeline execution, and ultimately, a new deployment. This rigidity slows down responsiveness to business needs and increases operational overhead. The core issue is treating frequently changing business values as code rather than configurable data.
The proposed architectural solution involves decoupling business logic from the application's codebase by storing rules as data in a database and using an expression evaluation engine (like MVEL) at runtime. In this model, the application code doesn't "know" how to calculate specific rules; it only knows how to retrieve a formula from the database and feed it to the formula engine for evaluation. This shifts the responsibility of rule management from developers to business users (via an admin interface), allowing dynamic updates without code changes or deployments.
Key Benefits of Externalized Business Logic
Externalizing business rules significantly reduces deployment frequency, increases system flexibility, and empowers business users to adapt logic more rapidly. This separation of concerns also streamlines development by keeping core application logic cleaner and less susceptible to frequent business-driven modifications.
| point_type_id | point_type_name | point_formula | description |
|---|
CREATE TABLE t_point_type (
point_type_id NUMBER PRIMARY KEY,
point_type_name VARCHAR2(100),
point_formula VARCHAR2(500),
description VARCHAR2(1000)
);The database schema for storing rules should be flexible enough to accommodate various formula types. A simple table with columns for `point_type_id`, `point_type_name`, and `point_formula` suffices. The `point_formula` column stores the actual expression that the MVEL engine will evaluate. This design allows for different types of calculations (e.g., initial points, birthday bonuses, tenure-based multipliers) to be configured as data.
The application layer integrates the MVEL utility, which handles formula evaluation. A dedicated `MvelUtil` class centralizes the logic for evaluating formulas, incorporating performance optimizations like expression compilation and caching. The service layer then retrieves the relevant formula from the repository and delegates its evaluation to `MvelUtil`, ensuring a clear separation of concerns.