Business Layer Documentation
Overview
The Business namespace provides a hierarchy of manager classes that encapsulate business logic, data operations, and entity lifecycle management. These managers integrate with Entity Framework, provide automatic audit trail support, and include hooks for custom business logic.Class Hierarchy
ManagerBase
The foundation class providing database context and message publishing capabilities.Properties
- DataContext: The Entity Framework DbContext for database operations
- MessagePublisher: IMessagePublisher for event-driven architecture
Usage
EntityManager
Core manager providing CRUD operations with automatic audit trail support and lifecycle hooks.Key Features
- Automatic Audit Fields: Populates created/updated timestamps and user IDs
- Lifecycle Hooks: Virtual methods for pre/post operation business logic
- Batch Operations: Efficient handling of multiple entities
- Direct Operations: Bypass entity loading for performance-critical updates/deletes
- Interface Caching: Performance optimization using TypeDictionary
Lifecycle Hooks
Each CRUD operation provides pre and post hooks:Audit Field Population
Automatically handles interfaces:ICreatorTrackable<T>: Sets CreatedById from ClaimsPrincipal.CurrentICreatedAuditable: Sets DateCreated to DateTime.UtcNowIUpdaterTrackable<T>: Sets UpdatedById from ClaimsPrincipal.CurrentIUpdatedAuditable: Sets DateUpdated to DateTime.UtcNow
CRUD Operations
Insert Operations
Update Operations
Delete Operations
Performance Features
- Deferred Save: Pass
save: falseto batch multiple operations - Direct Operations: Update/delete without loading entities into context
- Interface Caching: Static dictionary prevents repeated reflection
IdentifiableEntityManager
Extends EntityManager for entities with ID properties (implementingIIdentifiable<TId>).
Key Feature
- Automatic GUID Generation: Creates new GUIDs for entities with empty IDs during insertion
Example
StatusEntityManager
Manages entities with status tracking (implementingIHasStatus<TStatusType>).
Features
- Status Type Management: Loads and caches available status types
- Status Updates: Type-safe status transitions with logging
Properties
- StatusTypes: Collection of available TStatusType instances
Methods
Example Implementation
StateMachineEntityManager
Manages entities with state machine workflows (implementingIHasState<TStateType>).
Features
- State Type Management: Loads and caches available state types
- Predefined State Transitions: Common workflow states with standard sort orders
- State Update Logging: Automatic tracing of state transitions
Properties
- StateTypes: Collection of available TStateType instances
Standard State Methods
State Machine Convention
- 0: Created/Initial state
- 1-97: Custom intermediate states
- 98: Cancelled (terminal state)
- 99: Failed (terminal state)
- 100: Completed (terminal state)
Example Workflow
Best Practices
- Inherit from Appropriate Manager: Choose the most specific manager for your needs
- Override Hooks Sparingly: Only override what you need; call base implementations
- Use Direct Operations for Bulk: DirectUpdate/DirectDelete for performance
- Initialize Collections Early: Call Initialize() in manager constructors for status/state
- Leverage Deferred Save: Batch operations with
save: falsethen SaveChangesAsync() - Handle Exceptions in Hooks: Ensure robust error handling in lifecycle methods
- Use Message Publishing: Publish events for downstream systems in OnInserted/OnUpdated
Integration with Interfaces
The managers automatically detect and handle these interfaces:IIdentifiable<T>: Entity has an ID propertyICreatedAuditable: Track creation timestampIUpdatedAuditable: Track update timestampICreatorTrackable<T>: Track user who createdIUpdaterTrackable<T>: Track user who updatedIHasStatus<T>: Entity has status typeIHasState<T>: Entity participates in state machine
Thread Safety
- Static Interface Dictionary: Thread-safe type caching
- Instance Methods: Not thread-safe; use separate manager instances per request/scope