Skip to main content

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.Current
  • ICreatedAuditable: Sets DateCreated to DateTime.UtcNow
  • IUpdaterTrackable<T>: Sets UpdatedById from ClaimsPrincipal.Current
  • IUpdatedAuditable: Sets DateUpdated to DateTime.UtcNow

CRUD Operations

Insert Operations

Update Operations

Delete Operations

Performance Features

  • Deferred Save: Pass save: false to 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 (implementing IIdentifiable<TId>).

Key Feature

  • Automatic GUID Generation: Creates new GUIDs for entities with empty IDs during insertion

Example

StatusEntityManager

Manages entities with status tracking (implementing IHasStatus<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 (implementing IHasState<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

  1. Inherit from Appropriate Manager: Choose the most specific manager for your needs
  2. Override Hooks Sparingly: Only override what you need; call base implementations
  3. Use Direct Operations for Bulk: DirectUpdate/DirectDelete for performance
  4. Initialize Collections Early: Call Initialize() in manager constructors for status/state
  5. Leverage Deferred Save: Batch operations with save: false then SaveChangesAsync()
  6. Handle Exceptions in Hooks: Ensure robust error handling in lifecycle methods
  7. 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 property
  • ICreatedAuditable: Track creation timestamp
  • IUpdatedAuditable: Track update timestamp
  • ICreatorTrackable<T>: Track user who created
  • IUpdaterTrackable<T>: Track user who updated
  • IHasStatus<T>: Entity has status type
  • IHasState<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