Data Operations and Audit Documentation
Overview
EasyAF provides comprehensive data operation management with automatic audit trail creation, user tracking, and data integrity maintenance through a system of interfaces and manager classes. This document explains how these components work together to ensure data consistency and traceability.Core Interfaces for Data Operations
Identity Management
IIdentifiable<T>
Ensures entities have a unique identifier.
Audit Trail Interfaces
ICreatedAuditable
Tracks when an entity was created.IUpdatedAuditable
Tracks when an entity was last updated.User Tracking Interfaces
ICreatorTrackable<T>
Tracks which user created an entity.
IUpdaterTrackable<T>
Tracks which user last updated an entity.
Automatic Audit Field Population
The EntityManager automatically detects and populates audit fields based on implemented interfaces.During Insert Operations
During Update Operations
Entity Definition Best Practices
Complete Auditable Entity
Data Operation Flow
Insert Flow
- Client creates entity → New instance with business data
- Manager.InsertAsync() called → Initiates insert operation
- OnInsertingAsync() executed →
- ID generated (if
IIdentifiable<Guid>) - CreatedById set (if
ICreatorTrackable) - DateCreated set (if
ICreatedAuditable)
- ID generated (if
- Entity added to context → EntityState.Added
- SaveChangesAsync() → Database insert
- OnInsertedAsync() executed → Post-insert logic (events, notifications)
Update Flow
- Entity retrieved and modified → Property changes tracked
- Manager.UpdateAsync() called → Initiates update operation
- OnUpdatingAsync() executed →
- UpdatedById set (if
IUpdaterTrackable) - DateUpdated set (if
IUpdatedAuditable)
- UpdatedById set (if
- Entity marked modified → EntityState.Modified
- SaveChangesAsync() → Database update
- OnUpdatedAsync() executed → Post-update logic
Delete Flow
- Entity marked for deletion → Soft or hard delete decision
- Manager.DeleteAsync() called → Initiates delete operation
- OnDeletingAsync() executed → Pre-delete validation/logic
- Entity marked deleted → EntityState.Deleted
- SaveChangesAsync() → Database delete
- OnDeletedAsync() executed → Post-delete cleanup
Advanced Data Operations
Batch Operations
Audit fields are populated for each entity in batch operations:Direct Operations (Performance)
Direct operations bypass entity loading and audit field population:- Bulk status updates
- Cleanup operations
- Performance-critical scenarios
- No automatic audit trail
- No business logic hooks
- Better performance
Manual Audit Reset
For special scenarios like entity duplication:Data Integrity Patterns
Soft Delete Pattern
Versioning Pattern
Active Record Pattern
Security Considerations
User Context
The framework relies onClaimsPrincipal.Current for user identification:
Audit Trail Immutability
Once set, audit fields should not be modified:Integration with Change Tracking
DbObservableObject’s change tracking works with audit fields:Best Practices
- Always implement audit interfaces: Provides crucial traceability
- Use nullable types for update fields: Not all entities get updated
- Leverage automatic population: Don’t manually set audit fields
- Consider soft deletes: Maintain data history and recovery options
- Use direct operations judiciously: Balance performance vs audit needs
- Implement versioning for critical entities: Detect concurrent modifications
- Validate user context: Ensure ClaimsPrincipal.Current is available
- Test audit trail: Verify fields are populated correctly
- Document bypass scenarios: When audit fields won’t be populated
- Consider timezone handling: Store as UTC, display in local time