|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Text; |
5 | | -using System.Threading.Tasks; |
6 | | -using EntityFrameworkCore.Projectables; |
7 | | - |
8 | | -namespace ReadmeSample.Entities |
| 1 | +using EntityFrameworkCore.Projectables; |
| 2 | + |
| 3 | +namespace ReadmeSample.Entities; |
| 4 | + |
| 5 | +public class Order |
9 | 6 | { |
10 | | - public class Order |
11 | | - { |
12 | | - public int Id { get; set; } |
13 | | - public int UserId { get; set; } |
14 | | - public DateTime CreatedDate { get; set; } |
15 | | - public DateTime? FulfilledDate { get; set; } |
| 7 | + public int Id { get; set; } |
| 8 | + public int UserId { get; set; } |
| 9 | + public DateTime CreatedDate { get; set; } |
| 10 | + public DateTime? FulfilledDate { get; set; } |
| 11 | + public decimal TaxRate { get; set; } |
| 12 | + public OrderStatus Status { get; set; } |
| 13 | + |
| 14 | + public User User { get; set; } = null!; |
| 15 | + public ICollection<OrderItem> Items { get; set; } = []; |
| 16 | + |
| 17 | + // ── Feature 1: Projectable properties (compose each other recursively) ────── |
| 18 | + |
| 19 | + /// <summary>Sum of (unit price × quantity) for all items — inlined into SQL.</summary> |
| 20 | + [Projectable] public decimal Subtotal => Items.Sum(item => item.Product.ListPrice * item.Quantity); |
| 21 | + |
| 22 | + /// <summary>Tax amount (Subtotal × TaxRate) — composed from another [Projectable].</summary> |
| 23 | + [Projectable] public decimal Tax => Subtotal * TaxRate; |
| 24 | + |
| 25 | + /// <summary>Total including tax — composed from two [Projectable] properties.</summary> |
| 26 | + [Projectable] public decimal GrandTotal => Subtotal + Tax; |
| 27 | + |
| 28 | + /// <summary>True when the order has been fulfilled — usable in .Where() filters.</summary> |
| 29 | + [Projectable] public bool IsFulfilled => FulfilledDate != null; |
| 30 | + |
| 31 | + // ── Feature 1 (method): Projectable method with a parameter ───────────────── |
16 | 32 |
|
17 | | - public decimal TaxRate { get; set; } |
| 33 | + /// <summary>Grand total after applying a percentage discount — demonstrates a [Projectable] method.</summary> |
| 34 | + [Projectable] |
| 35 | + public decimal GetDiscountedTotal(decimal discountPct) => GrandTotal * (1 - discountPct); |
18 | 36 |
|
19 | | - public User User { get; set; } |
20 | | - public ICollection<OrderItem> Items { get; set; } |
| 37 | + // ── Feature 5: Pattern matching — switch expression ────────────────────────── |
21 | 38 |
|
22 | | - [Projectable] public decimal Subtotal => Items.Sum(item => item.Product.ListPrice * item.Quantity); |
23 | | - [Projectable] public decimal Tax => Subtotal * TaxRate; |
24 | | - [Projectable] public decimal GrandTotal => Subtotal * TaxRate; |
| 39 | + /// <summary> |
| 40 | + /// Priority label derived from GrandTotal using a switch expression. |
| 41 | + /// The generator rewrites this into SQL CASE WHEN expressions. |
| 42 | + /// </summary> |
| 43 | + [Projectable] |
| 44 | + public string PriorityLabel => GrandTotal switch |
| 45 | + { |
| 46 | + >= 100m => "High", |
| 47 | + >= 30m => "Medium", |
| 48 | + _ => "Low", |
| 49 | + }; |
| 50 | + |
| 51 | + // ── Feature 6: Block-bodied member (experimental) ──────────────────────────── |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Shipping category determined via an if/else block body. |
| 55 | + /// AllowBlockBody = true acknowledges the experimental nature (suppresses EFP0001). |
| 56 | + /// The block is converted to a ternary expression — identical SQL to the switch above. |
| 57 | + /// </summary> |
| 58 | + [Projectable(AllowBlockBody = true)] |
| 59 | + public string GetShippingCategory() |
| 60 | + { |
| 61 | + if (GrandTotal >= 100m) |
| 62 | + return "Express"; |
| 63 | + else if (GrandTotal >= 30m) |
| 64 | + return "Standard"; |
| 65 | + else |
| 66 | + return "Economy"; |
25 | 67 | } |
| 68 | + |
| 69 | + // ── Feature 8: Enum method expansion ───────────────────────────────────────── |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Human-readable status label. |
| 73 | + /// ExpandEnumMethods = true makes the generator enumerate every OrderStatus value at |
| 74 | + /// compile time and bake the results in as a SQL CASE expression — the GetDisplayName() |
| 75 | + /// method itself never runs at runtime. |
| 76 | + /// </summary> |
| 77 | + [Projectable(ExpandEnumMethods = true)] |
| 78 | + public string StatusDisplayName => Status.GetDisplayName(); |
| 79 | + |
| 80 | + // ── Feature 9: UseMemberBody ────────────────────────────────────────────────── |
| 81 | + |
| 82 | + // Private EF-compatible expression — the actual body EF Core will use. |
| 83 | + private bool IsHighValueOrderImpl => GrandTotal >= 50m; |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// UseMemberBody delegates the expression source to IsHighValueOrderImpl. |
| 87 | + /// The annotated member's own body is ignored by the generator; the target |
| 88 | + /// member's body is used as the expression tree instead. |
| 89 | + /// </summary> |
| 90 | + [Projectable(UseMemberBody = nameof(IsHighValueOrderImpl))] |
| 91 | + public bool IsHighValueOrder => IsHighValueOrderImpl; |
26 | 92 | } |
0 commit comments