Use Cases and Business Logic
Reading time: ~15 minutes
Introduction​
A critical aspect of Prism Architecture is how it organizes and structures business logic, particularly through its Orchestration Layer. This document explores the hierarchical approach to use cases and business logic in Prism Architecture, explaining how the architecture maintains separation of concerns while providing a clear structure for organizing application functionality.
Unlike simpler architectures that might place business logic in various places throughout the application, Prism Architecture creates a structured approach with clear boundaries and responsibilities. This organization not only improves maintainability but also enhances testability and makes the system more adaptable to changing requirements.
Business Logic Placement​
Domain vs. Orchestration Logic​
Prism Architecture makes a clear distinction between two types of business logic:
- Domain Logic: Pure business rules, validations, and calculations that represent the core knowledge of the business domain
- Orchestration Logic: The coordination and sequencing of operations across different parts of the system
These two types of logic are deliberately separated in Prism Architecture:
- Domain Logic resides in the Domain Layer, typically within Domain Services and Entities
- Orchestration Logic resides in the Orchestration Layer, within Use Cases, Orchestration Services, and Workflows
This separation allows domain experts to focus on capturing business rules correctly while application developers can focus on implementing the flow of operations without mixing concerns.
The Hierarchical Orchestration Structure​
One of the key innovations in Prism Architecture is its hierarchical approach to orchestration components. Rather than treating all use cases equally, Prism defines a clear hierarchy with progressively expanding capabilities and scope.
This hierarchy consists of three levels of components, each with specific responsibilities and boundaries:
- Use Cases: Entity-focused, atomic operations
- Orchestration Services: Feature/aggregate-level coordination
- Workflows: Cross-domain processes
Let's explore each level in detail.
Use Cases​
Definition and Characteristics​
Use Cases in Prism Architecture implement specific, well-defined operations that focus on a single entity within an aggregate. They represent atomic business operations with a clear, narrow focus.
Key characteristics of Use Cases include:
- Single Entity Focus: Each Use Case works with a single entity within an aggregate
- Atomic Operations: They handle specific, indivisible operations
- Clear Naming: Named with verb-noun combinations (e.g.,
RegisterUserUseCase
) - Coordination Role: They coordinate between Domain, Infrastructure, and Presentation layers
- Minimal Logic: They contain minimal logic beyond coordination
Boundaries and Constraints​
Use Cases operate within strict boundaries:
- Limited to operations on a single entity within an aggregate
- Cannot call other Use Cases
- Can use Domain Services for business logic
- Can use Infrastructure Services for external operations
These constraints ensure that Use Cases remain focused and maintainable, handling only one specific operation at a time.
Common Use Case Patterns​
Typical patterns for Use Cases include:
- CRUD Operations: Create, Read, Update, Delete operations for entities
- State Transitions: Moving entities through defined state changes
- Validation and Processing: Validating input and performing specific operations
- Simple Queries: Retrieving data for specific use cases
Example Use Cases​
Examples of well-defined Use Cases include:
RegisterUserUseCase
: Handles user registrationUpdateProductDetailsUseCase
: Updates a product's informationPlaceOrderUseCase
: Processes an order placementCancelSubscriptionUseCase
: Handles subscription cancellation
Orchestration Services​
Definition and Characteristics​
Orchestration Services manage related operations within a particular feature area or aggregate. They can compose multiple Use Cases but only within the same feature boundary.
Key characteristics of Orchestration Services include:
- Feature Area Focus: Each service manages a cohesive feature area
- Composition Capability: Can compose multiple Use Cases within a feature
- Naming Convention: Named after feature domains (e.g.,
AuthenticationOrcService
) - State Management: May maintain feature-specific state or context
- Feature API: Provide a cohesive API for related operations
Boundaries and Constraints​
Orchestration Services operate within defined boundaries:
- Limited to operations within a single feature domain or aggregate
- Can compose and call multiple Use Cases
- Cannot call or depend on Orchestration Services from different domains
- Serve as the middle tier in the component hierarchy
These constraints ensure that Orchestration Services maintain clear feature-based cohesion while being able to coordinate more complex operations than individual Use Cases.
Common Orchestration Patterns​
Typical patterns for Orchestration Services include:
- Feature Composition: Combining multiple Use Cases into feature workflows
- State Management: Maintaining context across multiple related operations
- Transaction Management: Ensuring consistency across multiple operations
- Feature-Level Validation: Validating complex constraints across related entities
Example Orchestration Services​
Examples of well-defined Orchestration Services include:
AuthenticationOrcService
: Manages authentication-related operationsOrderManagementOrcService
: Coordinates various order-related operationsSubscriptionOrcService
: Handles subscription-related functionalityProfileManagementOrcService
: Manages user profile-related operations
Workflow Coordinators​
Definition and Characteristics​
Workflow Coordinators manage complex, multi-step processes that may involve multiple feature areas and aggregates. They can compose both Use Cases and Services from different domains.
Key characteristics of Workflow Coordinators include:
- Cross-Domain Scope: Work across different aggregates and domains
- Process Focus: Handle end-to-end business processes
- Naming Convention: Named after processes (e.g.,
CheckoutWorkflow
) - State Management: Manage state transitions throughout a process
- Complex Coordination: Coordinate across multiple feature areas
Boundaries and Constraints​
Workflow Coordinators have the broadest scope in the hierarchy:
- Can work across multiple feature domains and aggregates
- Can compose both Use Cases and Orchestration Services
- Represent the highest level in the component hierarchy
- Used only for genuinely complex processes that span multiple domains
Despite their broader scope, Workflows should still be focused on specific business processes rather than becoming catch-all components.
Common Workflow Patterns​
Typical patterns for Workflow Coordinators include:
- Multi-Stage Processes: Managing processes with distinct stages
- Cross-Domain Coordination: Coordinating actions across domain boundaries
- Saga Pattern: Managing compensating transactions for complex operations
- State Machine Implementation: Implementing complex state transitions
Example Workflow Coordinators​
Examples of well-defined Workflow Coordinators include:
CheckoutWorkflow
: Manages the complete checkout processOnboardingWorkflow
: Coordinates user onboarding across multiple featuresPaymentProcessingWorkflow
: Handles the end-to-end payment processContentPublishingWorkflow
: Manages the content creation and publishing process
Business Logic Implementation Patterns​
Domain-Driven Logic​
Prism Architecture emphasizes implementing core business rules in the Domain Layer:
- Domain Services: For business logic that doesn't belong to a single entity
- Entity Methods: For entity-specific business rules
- Validation Services: For complex validation rules
- Domain Events: For reactive business processes
This approach keeps the core business rules centralized and consistent.
Use Case Structure Pattern​
A typical Use Case in Prism Architecture follows this pattern:
- Input Validation: Validating input parameters
- Permission Checking: Verifying the operation is allowed
- Domain Logic Delegation: Calling appropriate Domain Services
- Infrastructure Coordination: Using repositories or other services
- Result Composition: Creating the appropriate response
This consistent structure makes Use Cases predictable and maintainable.
Orchestration Service Composition​
Orchestration Services typically compose Use Cases in one of several patterns:
- Sequential Composition: Executing Use Cases in sequence
- Conditional Branching: Selecting Use Cases based on conditions
- Parallel Execution: Running independent Use Cases concurrently
- Aggregation: Combining results from multiple Use Cases
These composition patterns allow for flexible coordination of functionality.
Communication Patterns​
Command-Based Communication​
Prism Architecture typically uses command objects for communication with the Orchestration Layer:
- Command Objects: Immutable objects representing operation requests
- Result Objects: Structured responses containing operation results
- Validation Results: Specialized results for validation outcomes
- Error Handling: Standardized approach to error communication
This pattern creates explicit, traceable communication between layers.
Event-Based Communication​
For asynchronous processes, Prism Architecture uses an event-based approach:
- Domain Events: Generated by Domain Layer to indicate significant occurrences
- Event Handlers: In the Orchestration Layer to respond to events
- Workflow Triggers: Events that initiate or advance workflows
- Presentation Notifications: Events sent to the Presentation Layer
This approach enables reactive behavior while maintaining layer separation.
Testing Business Logic​
Domain Logic Testing​
Testing Domain Layer components focuses on business rules:
- Domain Service Tests: Verify business rule implementation
- Entity Tests: Ensure entity constraints and behavior
- Validation Logic Tests: Confirm validation rule correctness
- Domain Event Tests: Verify event generation under appropriate conditions
These tests focus on business correctness, not technical integration.
Orchestration Component Testing​
Testing Orchestration Layer components focuses on coordination:
- Use Case Tests: Verify correct orchestration of dependencies
- Orchestration Service Tests: Confirm proper composition of Use Cases
- Workflow Tests: Ensure correct process flow and state management
- Event Handler Tests: Verify appropriate reactions to events
These tests use mocks for dependencies to isolate orchestration logic.
Practical Implementation Guidance​
Balancing Business Logic Placement​
When deciding where to place business logic, follow these guidelines:
-
Domain Layer for:
- Core business rules and invariants
- Entity validation and constraints
- Business calculations and algorithms
- Domain-specific operations
-
Orchestration Layer for:
- Operation sequencing and coordination
- Transaction management
- Cross-entity orchestration
- Integration with external systems
Maintaining the Hierarchy​
To maintain a clean hierarchical structure:
- Start Simple: Begin with Use Cases for individual operations
- Group by Feature: Create Orchestration Services for related functionality
- Identify Workflows: Extract cross-domain processes into Workflows
- Refactor Continuously: Adjust the hierarchy as the application evolves
Component Organization​
The Orchestration Layer typically follows this folder structure:
This structure reflects the hierarchical organization of components.
Common Anti-Patterns to Avoid​
Use Case Anti-Patterns​
- Bloated Use Cases: Including too much functionality in a single Use Case
- Domain Logic in Use Cases: Placing business rules that belong in the Domain Layer
- Direct Infrastructure Access: Bypassing abstractions to access infrastructure
- Use Case Chaining: Having Use Cases call other Use Cases
Orchestration Service Anti-Patterns​
- Aggregate-Based Services: Creating services based solely on Aggregates (e.g.,
UserOrcService
) rather than features - Operation-Splitting Services: Creating separate services for CRUD operations on the same entity
- Cross-Domain Services: Services that span multiple unrelated domains
- Stateless-Only Thinking: Failing to maintain necessary context between operations
Workflow Anti-Patterns​
- Monolithic Workflows: Trying to handle too many processes in one Workflow
- Missing State Management: Not properly tracking process state
- Hardcoded Flow: Building workflows that can't adapt to variations
- Technical Process Focus: Creating workflows around technical rather than business processes
Real-World Examples​
E-Commerce Order Processing​
In an e-commerce application, the order processing might be structured as:
Use Cases:
ValidateOrderUseCase
: Validates order detailsCalculateOrderTotalsUseCase
: Calculates order totalsReserveInventoryUseCase
: Reserves inventory for items
Orchestration Service:
OrderManagementOrcService
: Coordinates order-related operations
Workflow:
CheckoutWorkflow
: Manages the end-to-end checkout process across payment, inventory, and order domains
User Management​
In a SaaS application, user management might be structured as:
Use Cases:
RegisterUserUseCase
: Handles user registrationUpdateUserProfileUseCase
: Updates user profile detailsChangeUserPasswordUseCase
: Changes user password
Orchestration Service:
UserManagementOrcService
: Coordinates user-related operations
Workflow:
OnboardingWorkflow
: Manages the complete user onboarding process across authentication, profile setup, and permissions domains
Conclusion​
The hierarchical approach to use cases and business logic in Prism Architecture creates a structured, maintainable way to organize application functionality. By clearly separating domain logic from orchestration logic and using a tiered hierarchy of orchestration components, Prism Architecture provides both clarity and flexibility.
The three-level hierarchy of Use Cases, Orchestration Services, and Workflows creates natural boundaries that align with the complexity of operations. Simple, entity-focused operations are handled by Use Cases, feature-area coordination by Orchestration Services, and complex cross-domain processes by Workflows.
By following these patterns and avoiding anti-patterns, developers can create applications that are both well-structured and adaptable to changing requirements.
Next Steps​
- Domain Services: Explore how Domain Services implement core business logic
- Infrastructure Integration: Understand how the Orchestration Layer interfaces with the Infrastructure Layer
- Presentation Layer Integration: Learn how the Orchestration Layer communicates with the Presentation Layer
- Event-Driven Architecture: Discover how events enable reactive behavior in Prism Architecture