Update Example
Overview
This document demonstrates the complete Update flow using the UpdateShipping feature from the EBusiness application. The flow starts with a PUT request to the controller and ends with event publishing and subscriber processing.
Complete Flow Architecture
PUT Request → Controller → Service → PreBus Plugins → Command Handler → RESTClient → External System
↓
Response ← Controller ← Service ← Command Handler ← RESTClient ← External SystemDetailed Flow Breakdown
1. PUT Request
↓
2. Controller (API Entry Point)
↓
3. Service Layer (Business Orchestration)
↓
4. PreBus Processing (Validation Pipeline)
├── UpdateShippingSequence (Plugin Registration)
├── UpdateShippingDataPacket (Validation Context)
└── IsValidShipping 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_UpdateShipping.cs
What Happens:
HTTP Method:
PUT /api/Shipping/UpdateShippingInput:
UpdateShippingDtofrom request bodyAction: Calls the service layer to process the shipping update
Response: HTTP 200 OK with success status
2. Service Layer - Business Orchestration
File: ProcessShippingService_UpdateShipping.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
UpdateShippingCommandwith DTOCommand Processing: Calls the command handler
Result: Returns success status
2.1. PreBus Business Rule Sequence - Validation Pipeline
File: UpdateShippingSequence.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: UpdateShippingDataPacket.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: IsValidShipping.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: UpdateShippingHandler.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/UpdateShipping.cs
What Happens:
Validation: Guards against null commands
Data Mapping: Converts DTO to domain model
Audit Fields: Sets last modified by user
State Management: Marks entity as modified
Child Objects: Processes child object updates
5. NServiceBus Handler - Message Processing
File: UpdateShippingNsbHandler.cs
What Happens:
Message Reception: Receives
UpdateShippingCommandfrom 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: ShippingUpdatedEvent
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: UpdateLogisticsOnUpdateShipping.cs
What Happens:
Event Reception: Receives
ShippingUpdatedEventfrom message busSide Effects: Executes business logic (notifications, tracking updates, etc.)
Additional Events: Can fire more events if needed
Data Transfer Objects (DTOs)
Input DTO: UpdateShippingDto
UpdateShippingDtoCommand: UpdateShippingCommand
UpdateShippingCommandKey Differences from Insert Flow
Update-Specific Characteristics
Entity Lookup: Must find existing entity before updating
Null Check: Validates entity exists before processing
State Management: Uses
SetModified()instead ofSetAdded()Audit Fields: Updates
LastModifiedByinstead ofCreatedByHTTP Method: Uses
PUTinstead ofPOSTResponse Code: Returns
200 OKinstead of201 Created
PreBus Validation Focus
IsValidShipping: Validates shipping exists and is in updatable state
Business Rules: Ensures shipping can be modified (not delivered, not cancelled)
Data Integrity: Validates update doesn't violate business constraints
Flow Summary
Synchronous Flow (Immediate Response)
PUT Request → Controller receives request
Service Processing → Business orchestration and PreBus validation
PreBus Plugins → Sequential validation of business rules
Command Handler → Entity lookup and data processing
Domain Logic → Business rules and state management
Response → HTTP 200 OK with success status
Asynchronous Flow (Event Processing)
Event Publishing → ShippingUpdatedEvent published to message bus
Subscriber Processing → UpdateLogisticsOnUpdateShipping executes
Side Effects → Logistics notifications, tracking updates, analytics
Key Benefits
Data Integrity: Ensures entity exists before updating
Audit Trail: Tracks who made changes and when
Business Rules: Validates update is allowed
Event-Driven: Notifies other systems of changes
Testable: Each component can be tested independently
Maintainable: Clear separation of concerns
This UpdateShipping example demonstrates how FlexBase enables clean, maintainable, and scalable update operations with proper validation, audit trails, and event-driven architecture! 🚀
Last updated