Get Single Example
Overview
This document demonstrates the complete Get Single flow using the GetShippingSingle feature from the EBusiness application. The flow starts with a GET request to the controller and uses Query projects instead of Handlers in the DomainHandler section for data retrieval. This returns a single entity based on specific criteria, similar to Get By ID but with more flexible filtering.
Complete Flow Architecture
GET Request → Controller → Service → Query Handler → RESTClient → External System
↓
Response ← Controller ← Service ← Query Handler ← RESTClient ← External SystemDetailed Flow Breakdown
1. GET Request
↓
2. Controller (API Entry Point)
↓
3. Service Layer (Business Orchestration)
↓
4. Query Handler (RESTClient Integration)
├── Internal DTO → Request DTO Mapping
├── RESTClient Call to External System
└── Response DTO → Internal DTO Mapping
↓
5. Response (Single Entity)Step-by-Step Implementation
1. API Controller - The Entry Point
File: ShippingController_GetShippingSingle.cs
What Happens:
HTTP Method:
GET /api/Shipping/GetShippingSingleInput:
GetShippingSingleParamsfrom query parametersAction: Calls the service layer to process the query
Response: HTTP 200 OK with
GetShippingSingleDtoor 404 Not Found
2. Service Layer - Business Orchestration
File: ProcessShippingService_GetShippingSingle.cs
What Happens:
Query Resolution: Gets the FlexiQuery instance for GetShippingSingle
Parameter Assignment: Assigns query parameters to the query handler
Query Execution: Calls the Fetch() method to execute the query
Result: Returns single shipping or null if not found
3. Query Handler - RESTClient Integration
File: GetShippingSingle.cs
What Happens:
Parameter Assignment: Stores query parameters for use in RESTClient calls
Request Mapping: Converts internal parameters to Request DTO
RESTClient Call: Calls external shipping service via RESTClient
Response Mapping: Converts Response DTO to Internal DTO
Error Handling: Handles success/failure responses from external system
Data Transformation: Applies business logic during mapping
4. Query Parameters - Input DTO
File: GetShippingSingleParams.cs
What Happens:
Flexible Parameters: Multiple ways to identify the entity
Primary ID: Can use ID for direct lookup
Alternative Criteria: Can use OrderId, TrackingNumber, etc.
Query Parameters: Automatically bound from URL query string
5. Output DTO - Data Transfer Object
File: GetShippingSingleDto.cs
What Happens:
Complete Data: Contains all relevant fields for the entity
Detailed Structure: Includes all properties for full entity display
Related Data: May include related entity information (ShippingItems)
Audit Fields: Includes creation and modification tracking
6. AutoMapper Configuration - Data Transformation
File: GetShippingSingleMapperConfiguration.cs
What Happens:
Entity to DTO Mapping: Maps domain entities to DTOs
Custom Mappings: Handles complex property transformations
Related Data: Maps related entity properties
Computed Fields: Handles calculated properties
Key Differences from Get By ID
Get Single vs Get By ID Characteristics
Input
Query parameters
URL parameter (ID)
Filtering
Multiple criteria
Single ID only
Flexibility
High (multiple ways to find)
Low (ID only)
Use Case
Complex lookups
Direct ID lookup
Performance
Depends on criteria
Very fast (indexed)
Controller Method
RunQuerySingleService
RunQuerySingleService
Query Base
FlexiQueryBridge
FlexiQueryBridge
Get Single-Specific Features
Flexible Filtering: Multiple ways to identify the entity
Complex Criteria: Can combine multiple filter conditions
Business Logic: Can implement complex lookup rules
Alternative Keys: Can use non-primary key fields
Conditional Logic: Can apply different logic based on parameters
Common Use Cases
Lookup by Alternative Key: Find by tracking number, order ID, etc.
Conditional Lookups: Different logic based on user role or context
Business Rules: Complex business logic for entity identification
API Integration: Flexible lookup for external systems
Search Scenarios: Find entity based on multiple criteria
Flow Summary
Synchronous Flow (Data Retrieval)
GET Request → Controller receives request with query parameters
Service Processing → Business orchestration and query resolution
Query Handler → Database query building and execution
AutoMapper → Entity-to-DTO transformation
Response → HTTP 200 OK with entity or 404 Not Found
No Asynchronous Flow
No Events: Query operations don't publish events
No Subscribers: No side effects or event processing
Immediate Response: Data is returned immediately
Query Building Patterns
Basic Query Building
Advanced Query Building with Multiple Criteria
Business Logic Query Building
Error Handling
HTTP Status Codes
200 OK: Entity found and returned successfully
404 Not Found: Entity with specified criteria does not exist
400 Bad Request: Invalid parameters or missing required criteria
500 Internal Server Error: Database or server error
Controller Error Handling
Performance Considerations
Optimization Strategies
Database Indexing: Ensure proper indexes on filter fields
Query Optimization: Use efficient WHERE clauses
Parameter Validation: Validate parameters early
Caching: Consider caching for frequently accessed data
Query Planning: Plan queries based on most common use cases
When to Use Get Single vs Get By ID
Alternative Keys
✅ Yes
❌ No
Complex Lookups
✅ Yes
❌ No
Business Rules
✅ Yes
❌ No
Direct ID Lookup
❌ No
✅ Yes
Simple Lookups
❌ No
✅ Yes
Performance Critical
❌ No
✅ Yes
Key Benefits
Flexibility: Multiple ways to find entities
Business Logic: Can implement complex lookup rules
Alternative Keys: Support for non-primary key lookups
Type Safety: Strongly typed DTOs and parameters
AutoMapper: Automatic entity-to-DTO mapping
Error Handling: Built-in 404 handling for missing entities
No Side Effects: Read-only operations
Testable: Each component can be tested independently
Maintainable: Clear separation of concerns
This GetShippingSingle example demonstrates how FlexBase enables clean, maintainable, and scalable single entity retrieval operations with flexible filtering and business logic support! 🚀
Last updated