-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModels.cs
More file actions
104 lines (87 loc) · 2.87 KB
/
Models.cs
File metadata and controls
104 lines (87 loc) · 2.87 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
99
100
101
102
103
104
using ExpressiveSharp;
// ── Domain models ────────────────────────────────────────────────────────────
public enum OrderStatus { Pending, Approved, Rejected }
public class Customer
{
public string? Name { get; set; }
public string? Email { get; set; }
}
public class Order
{
public int Id { get; set; }
public string? Tag { get; set; }
public double Price { get; set; }
public int Quantity { get; set; }
public OrderStatus Status { get; set; }
public Customer? Customer { get; set; }
/// Computed total — usable in LINQ-to-SQL queries, not just in-memory code.
[Expressive]
public double Total => Price * Quantity;
/// Null-conditional access — normally illegal in expression trees (CS8072).
[Expressive]
public string? CustomerName => Customer?.Name;
/// Switch expression with relational patterns — translates to CASE WHEN in SQL.
[Expressive]
public string GetGrade() => Price switch
{
>= 100 => "Premium",
>= 50 => "Standard",
_ => "Budget",
};
/// Block body with local variables and if/else — requires AllowBlockBody opt-in.
[Expressive(AllowBlockBody = true)]
public string GetCategory()
{
var threshold = Quantity * 10;
if (threshold > 100)
{
return "Bulk";
}
else
{
return "Regular";
}
}
}
// ── DTO with projectable constructor ─────────────────────────────────────────
public class OrderDto
{
public int Id { get; set; }
public string Description { get; set; } = "";
public double Total { get; set; }
public OrderDto() { }
/// Projectable constructor — expands to MemberInit so LINQ providers
/// can translate the projection to SQL instead of loading entire entities.
[Expressive]
public OrderDto(int id, string description, double total)
{
Id = id;
Description = description;
Total = total;
}
}
// ── Shape hierarchy for type-pattern switch demo ─────────────────────────────
public abstract class Shape
{
public string Name { get; set; } = "";
}
public class Circle : Shape
{
public double Radius { get; set; }
}
public class Rectangle : Shape
{
public double Width { get; set; }
public double Height { get; set; }
}
public static class ShapeExtensions
{
/// Extension method with type-pattern switch — works with [Expressive] too.
[Expressive]
public static double GetArea(this Shape shape) => shape switch
{
Circle c => Math.PI * c.Radius * c.Radius,
Rectangle r => r.Width * r.Height,
_ => 0,
};
}