-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.cs
More file actions
98 lines (87 loc) · 2.91 KB
/
Example.cs
File metadata and controls
98 lines (87 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using CompactMapper;
namespace CompactMapperExample
{
// Source entities
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
public List<Order> Orders { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
}
public class Order
{
public int Id { get; set; }
public DateTime OrderDate { get; set; }
public decimal Total { get; set; }
}
// Target DTOs
public class CustomerDto
{
public int Id { get; set; }
public string FullName { get; set; } // Custom mapping
public AddressDto Address { get; set; }
public List<OrderDto> Orders { get; set; }
}
public class AddressDto
{
public string Street { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
}
public class OrderDto
{
public int Id { get; set; }
public DateTime OrderDate { get; set; }
public decimal Total { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Create sample data
var customer = new Customer
{
Id = 1,
FirstName = "John",
LastName = "Doe",
Address = new Address
{
Street = "123 Main St",
City = "Example City",
PostalCode = "12345"
},
Orders = new List<Order>
{
new Order { Id = 101, OrderDate = DateTime.Now, Total = 125.99m },
new Order { Id = 102, OrderDate = DateTime.Now.AddDays(-5), Total = 249.95m }
}
};
// Register custom mapping for Customer to CustomerDto
CompactMapperExtension.AddCustomMapping<Customer, CustomerDto>((src, dest) =>
{
dest.FullName = $"{src.FirstName} {src.LastName}";
});
// Map customer to DTO
var customerDto = customer.MapTo<CustomerDto>();
// Display result
Console.WriteLine($"Customer ID: {customerDto.Id}");
Console.WriteLine($"Full Name: {customerDto.FullName}");
Console.WriteLine($"Address: {customerDto.Address.Street}, {customerDto.Address.City}, {customerDto.Address.PostalCode}");
Console.WriteLine($"Orders: {customerDto.Orders.Count}");
foreach (var order in customerDto.Orders)
{
Console.WriteLine($" - Order #{order.Id}: {order.OrderDate:d} - ${order.Total}");
}
}
}
}