Soft Delete Example
Overview
This document demonstrates the complete Soft Delete flow using the SoftDeleteShipping feature from the EBusiness application. The flow starts with a DELETE request to the controller and ends with event publishing and subscriber processing. Soft delete marks the entity as deleted without physically removing it from the database.
Complete Flow Architecture
DELETE Request → Controller → Service → PreBus Plugins → Command Handler → RESTClient → External System
↓
Response ← Controller ← Service ← Command Handler ← RESTClient ← External SystemDetailed Flow Breakdown
1. DELETE Request
↓
2. Controller (API Entry Point)
↓
3. Service Layer (Business Orchestration)
↓
4. PreBus Processing (Validation Pipeline)
├── SoftDeleteShippingSequence (Plugin Registration)
├── SoftDeleteShippingDataPacket (Validation Context)
└── IsValidShippingForSoftDelete Plugin (Business Rules)
↓
5. Command Handler (RESTClient Integration)
├── Internal DTO → Request DTO Mapping
├── RESTClient Call to External System
└── Response DTO Processing
↓
6. Event Publishing (Asynchronous Processing)
↓
7. Subscribers (Side Effects)Step-by-Step Implementation
1. API Controller - The Entry Point
File: ShippingController_SoftDeleteShipping.cs
What Happens:
HTTP Method:
DELETE /api/Shipping/SoftDeleteShipping/{id}Input: Shipping ID from URL parameter
DTO Creation: Creates
SoftDeleteShippingDtowith the IDAction: Calls the service layer to process the soft delete
Response: HTTP 200 OK with success status
2. Service Layer - Business Orchestration
File: ProcessShippingService_SoftDeleteShipping.cs
What Happens:
PreBus Processing: Executes business rule sequences (plugins)
Validation: Processes business rule sequences
ID Generation: Generates unique key for tracking
Command Creation: Creates
SoftDeleteShippingCommandwith DTOCommand Processing: Calls the command handler
Result: Returns success status
2.1. PreBus Business Rule Sequence - Validation Pipeline
File: SoftDeleteShippingSequence.cs
What Happens:
Plugin Registration: Registers validation plugins in execution order
Sequential Processing: Executes plugins one by one
Error Collection: Collects validation errors from all plugins
Early Exit: Stops processing if any plugin fails
2.2. PreBus Data Packet - Validation Context
File: SoftDeleteShippingDataPacket.cs
What Happens:
Context Container: Holds DTO and application context
Error Collection: Collects validation errors from plugins
Data Sharing: Allows plugins to share data during validation
Logging: Provides logging capabilities for plugins
2.3. PreBus Validation Plugin - Business Rules
File: IsValidShippingForSoftDelete.cs
What Happens:
Business Rule Validation: Implements specific validation logic
Database Access: Can access repository for data validation
Error Reporting: Adds errors to the data packet if validation fails
Async Support: Supports asynchronous validation operations
Dependency Injection: Receives logger and repository factory
3. Command Handler - RESTClient Integration
File: SoftDeleteShippingHandler.cs
What Happens:
DTO Mapping: Converts internal DTO to Request DTO for external system
RESTClient Call: Calls external shipping service via RESTClient
Response Processing: Handles success/failure responses from external system
Logging: Logs success/failure of external service call
Event Publishing: Fires events based on external service result
4. Domain Model - Business Logic
File: Shipping/SoftDeleteShipping.cs
What Happens:
Validation: Guards against null commands
ID Assignment: Sets the shipping ID from DTO
State Management: Marks entity as soft deleted
Child Objects: Processes child object soft deletions
5. NServiceBus Handler - Message Processing
File: SoftDeleteShippingNsbHandler.cs
What Happens:
Message Reception: Receives
SoftDeleteShippingCommandfrom message busLogging: Logs handler execution
Delegation: Calls the actual command handler
Context Bridge: Converts NServiceBus context to FlexBase context
6. Event Publishing - Asynchronous Processing
Event: ShippingSoftDeletedEvent (if implemented)
What Happens:
Event Creation: FlexBase creates event with shipping data
Message Bus: Event is published to message bus
Subscriber Notification: All subscribers are notified
7. Event Subscribers - Side Effects
File: NotifyLogisticsOnShippingSoftDeleted.cs (if implemented)
What Happens:
Event Reception: Receives
ShippingSoftDeletedEventfrom message busSide Effects: Executes business logic (logistics updates, notifications, etc.)
Additional Events: Can fire more events if needed
Data Transfer Objects (DTOs)
Input DTO: SoftDeleteShippingDto
SoftDeleteShippingDtoCommand: SoftDeleteShippingCommand
SoftDeleteShippingCommandKey Differences from Hard Delete
Soft Delete vs Hard Delete Characteristics
Data Persistence
✅ Data remains in database
❌ Data physically removed
Recovery
✅ Can be restored
❌ Cannot be recovered
Audit Trail
✅ Complete history maintained
❌ History lost
Performance
✅ Fast (just flag update)
✅ Fast (row deletion)
Storage
❌ Uses more storage
✅ Frees storage
Queries
❌ Need to filter deleted items
✅ No filtering needed
Business Rules
✅ Can validate before delete
✅ Can validate before delete
Soft Delete-Specific Features
Data Preservation: Entity remains in database with deleted flag
Recovery: Can be restored if needed
Audit Trail: Maintains complete history
Query Filtering: Queries must filter out soft-deleted items
Business Logic: Can implement complex deletion rules
Soft Delete Pattern Benefits
Data Safety: No accidental data loss
Compliance: Meets regulatory requirements for data retention
Audit Trail: Complete history of all operations
Recovery: Can restore deleted data if needed
Business Rules: Can implement complex deletion logic
Flow Summary
Synchronous Flow (Immediate Response)
DELETE Request → Controller receives request with ID
Service Processing → Business orchestration and PreBus validation
PreBus Plugins → Sequential validation of business rules
Command Handler → Data processing and soft delete
Domain Logic → Business rules and state management
Response → HTTP 200 OK with success status
Asynchronous Flow (Event Processing)
Event Publishing → ShippingSoftDeletedEvent published to message bus
Subscriber Processing → NotifyLogisticsOnShippingSoftDeleted executes
Side Effects → Logistics updates, carrier notifications, analytics
Query Filtering for Soft Deleted Items
Standard Query Filtering
Include Soft Deleted Items (Admin Queries)
Key Benefits
Data Safety: No accidental data loss
Compliance: Meets regulatory requirements
Audit Trail: Complete history of operations
Recovery: Can restore deleted data
Business Rules: Complex deletion logic support
Event-Driven: Notifies other systems of deletions
Testable: Each component can be tested independently
Maintainable: Clear separation of concerns
This SoftDeleteShipping example demonstrates how FlexBase enables clean, maintainable, and scalable soft delete operations with proper validation, data preservation, and event-driven architecture! 🚀
Last updated