This project demonstrates a comprehensive manual mapping system using static classes and methods with expression-bodied syntax in C# 13.0 and .NET 9.
All mapping methods use modern C# expression-bodied member syntax (=>) for concise, readable code.
- Person with nested Address, PhoneNumbers collection, and Orders collection
- Order with OrderItems collection
- Multiple enum types with automatic conversion
- Bi-directional mapping (Entity ↔ DTO)
- Entity to DTO and DTO to Entity mapping
- Bulk operations with
IEnumerable<T> - Safe null-aware mapping
- Partial update functionality
- Property name transformation (e.g.,
StreetAddress→Street) - Address formatting utilities
- Validation helpers
- Enum to string conversion with switch expressions
- Phone number formatting and cleaning
- Type-safe phone type parsing
- Order status enum handling
- Advanced filtering by date ranges and status
- Total value calculations
- Comprehensive validation
- Line total calculations
- Discount calculations
- High-value item filtering
- Aggregation operations (sum, average, count)
The MappingService provides:
- Generic mapping with
Func<TSource, TDestination> - Safe null-aware mapping with generic constraints
- Business rule-based filtering
- Summary mapping with selective data inclusion
- Statistical calculations (lifetime value, order counts)
public static PersonDto ToDto(Person entity) => new()
{
Id = entity.PersonId,
FirstName = entity.FirstName,
LastName = entity.LastName,
Email = entity.EmailAddress,
// ... more properties
};public static IEnumerable<PersonDto> ToDtoList(IEnumerable<Person> entities) =>
entities.Select(ToDto);public static string GetPhoneTypeString(PhoneType phoneType) => phoneType switch
{
PhoneType.Mobile => "Mobile",
PhoneType.Home => "Home",
PhoneType.Work => "Work",
PhoneType.Fax => "Fax",
_ => "Unknown"
};public static IEnumerable<OrderDto> GetRecentOrders(IEnumerable<Order> orders, int days) =>
orders.Where(o => o.CreatedDate >= DateTime.Now.AddDays(-days))
.Select(ToDto);public static bool IsActiveOrder(Order order) =>
order.OrderStatus is not OrderStatus.Cancelled and not OrderStatus.Delivered;The program demonstrates:
- Basic Entity-DTO Mapping - Converting between domain entities and DTOs
- Reverse Mapping - Converting DTOs back to entities
- Phone Number Processing - Formatting, validation, and cleaning
- Order Calculations - Total values, item counts, validation
- Advanced Service Operations - Customer lifetime value, filtering
- Bulk Operations - Mapping collections efficiently
- Conditional Mapping - Adult persons, active orders
- Summary Mapping - Selective data inclusion
- Advanced Filtering - High-value items, product-specific items
- Statistical Analysis - Averages, totals, counts
- C# 13.0 Features: Expression-bodied members, pattern matching, switch expressions
- .NET 9: Modern runtime with performance optimizations
- Static Classes: No instantiation needed, purely functional approach
- Generic Methods: Type-safe operations with compile-time checking
- LINQ Integration: Seamless collection transformations
- Null Safety: Proper null handling throughout
- Validation: Comprehensive business rule validation
- Formatting: User-friendly string representations
The demonstration produces detailed output showing:
- Person details with formatted addresses and phone numbers
- Order processing with calculations and validation
- Advanced filtering and statistical operations
- Performance metrics and validation results
This example showcases how to build robust, maintainable mapping systems using modern C# features while maintaining clean, readable code through expression-bodied syntax.