This document demonstrates the complete Get By ID flow using the GetProductById 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 by its unique identifier.
Complete Flow Architecture
GET Request → Controller → Service → Query Handler → Database → Response
ID-Based Lookup: Uses unique identifier for fast retrieval
404 Handling: Returns 404 when entity not found
Complete Data: Returns all entity details
Fast Performance: Optimized for indexed lookups
Common Use Cases
Entity Details: Display detailed information about a specific entity
Edit Forms: Load entity data for editing
View Pages: Show complete entity information
API Integration: Retrieve specific entity for external systems
Validation: Check if entity exists before operations
Flow Summary
Synchronous Flow (Data Retrieval)
GET Request → Controller receives request with ID parameter
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
Error Handling Patterns
Performance Considerations
Optimization Strategies
Database Indexing: Ensure ID field is properly indexed
Selective Fields: Only return necessary fields in DTO
Related Data: Use Include() judiciously for related entities
Caching: Consider caching for frequently accessed entities
Query Optimization: Use FirstOrDefault() for single entity retrieval
When to Use Get By ID vs Other Queries
Scenario
Use Get By ID
Use Get List
Use Get Paged List
Entity Details Page
✅ Yes
❌ No
❌ No
Edit Form Loading
✅ Yes
❌ No
❌ No
API Integration
✅ Yes
❌ No
❌ No
Dropdown Population
❌ No
✅ Yes
❌ No
Data Grids
❌ No
❌ No
✅ Yes
Search Results
❌ No
✅ Yes
✅ Yes
Lookup Tables
❌ No
✅ Yes
❌ No
Error Handling
HTTP Status Codes
200 OK: Entity found and returned successfully
404 Not Found: Entity with specified ID does not exist
400 Bad Request: Invalid ID format or missing ID parameter
500 Internal Server Error: Database or server error
Controller Error Handling
Key Benefits
Performance: Very fast retrieval using indexed ID lookup
Simplicity: Simple single entity retrieval
Complete Data: Returns full entity details
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 GetProductById example demonstrates how FlexBase enables clean, maintainable, and scalable single entity retrieval operations with proper error handling and performance optimization! 🚀
protected override IQueryable<T> Build<T>()
{
_repoFactory.Init(_params);
IQueryable<T> query = _repoFactory.GetRepo().FindAll<T>();
// Filter by ID
query = query.Where(t => t.Id == _params.Id);
// Exclude soft deleted items
query = query.Where(t => !t.IsSoftDeleted);
// Include related entities if needed
// query = query.Include(t => t.Category);
return query;
}
public override GetProductByIdDto Fetch()
{
var result = Build<Product>().SelectTo<GetProductByIdDto>().FirstOrDefault();
if (result == null)
{
_logger.LogWarning("Product with ID {ProductId} not found", _params.Id);
}
else
{
_logger.LogDebug("Product with ID {ProductId} retrieved successfully", _params.Id);
}
return result;
}
[HttpGet()]
[Route("GetProductById/{id}")]
[ProducesResponseType(typeof(GetProductByIdDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetProductById(string id)
{
if (string.IsNullOrEmpty(id))
{
return BadRequest("Product ID is required");
}
GetProductByIdParams parameters = new GetProductByIdParams();
parameters.Id = id;
return RunQuerySingleService<GetProductByIdParams, GetProductByIdDto>(
parameters, _processProductsService.GetProductById);
}