Skip to content

Commit 5b5397c

Browse files
authored
Simplified Expression Structure (#7973)
* Simplified Expression Structure * Fixed snapshots * Fixed snapshots * Fixed snapshots
1 parent 184d60f commit 5b5397c

File tree

29 files changed

+2403
-45
lines changed

29 files changed

+2403
-45
lines changed

src/GreenDonut/src/GreenDonut.Data.EntityFramework/Expressions/ExpressionHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,9 @@ private static Expression CreateParameter(object? value, Type type)
378378
return converter(value);
379379
}
380380

381-
private static Expression CreateAndConvertParameter<T>(object value)
381+
private static Expression CreateAndConvertParameter<T>(T value)
382382
{
383-
Expression<Func<T>> lambda = () => (T)value;
383+
Expression<Func<T>> lambda = () => value;
384384
return lambda.Body;
385385
}
386386

src/GreenDonut/test/GreenDonut.Data.EntityFramework.Tests/PagingHelperIntegrationTests.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public async Task Paging_Empty_PagingArgs()
2929
var result = await context.Brands.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(pagingArgs);
3030

3131
// Assert
32-
await Snapshot.Create()
32+
await CreateSnapshot()
3333
.AddQueries(queries)
3434
.Add(
3535
new
@@ -61,7 +61,7 @@ public async Task Paging_First_5()
6161
var result = await context.Brands.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(pagingArgs);
6262

6363
// Assert
64-
await Snapshot.Create()
64+
await CreateSnapshot()
6565
.AddQueries(queries)
6666
.Add(
6767
new
@@ -97,7 +97,7 @@ public async Task Paging_First_5_After_Id_13()
9797
var result = await context.Brands.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(pagingArgs);
9898

9999
// Assert
100-
await Snapshot.Create()
100+
await CreateSnapshot()
101101
.AddQueries(queries)
102102
.Add(
103103
new
@@ -129,7 +129,7 @@ public async Task Paging_Last_5()
129129
var result = await context.Brands.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(pagingArgs);
130130

131131
// Assert
132-
await Snapshot.Create()
132+
await CreateSnapshot()
133133
.AddQueries(queries)
134134
.Add(
135135
new
@@ -165,7 +165,7 @@ public async Task Paging_First_5_Before_Id_96()
165165
var result = await context.Brands.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(pagingArgs);
166166

167167
// Assert
168-
await Snapshot.Create()
168+
await CreateSnapshot()
169169
.AddQueries(queries)
170170
.Add(
171171
new
@@ -186,7 +186,7 @@ public async Task BatchPaging_First_5()
186186
{
187187
// Arrange
188188
#if NET8_0
189-
var snapshot = Snapshot.Create();
189+
var snapshot = CreateSnapshot();
190190
#else
191191
var snapshot = Snapshot.Create("NET9_0");
192192
#endif
@@ -229,7 +229,7 @@ public async Task BatchPaging_Last_5()
229229
{
230230
// Arrange
231231
#if NET8_0
232-
var snapshot = Snapshot.Create();
232+
var snapshot = CreateSnapshot();
233233
#else
234234
var snapshot = Snapshot.Create("NET9_0");
235235
#endif
@@ -388,6 +388,15 @@ protected override async Task<IReadOnlyDictionary<int, Page<Product>>> LoadBatch
388388
.ToBatchPageAsync(t => t.BrandId, pagingArgs, cancellationToken);
389389
}
390390
}
391+
392+
private static Snapshot CreateSnapshot()
393+
{
394+
#if NET9_0_OR_GREATER
395+
return Snapshot.Create();
396+
#else
397+
return Snapshot.Create("NET8_0");
398+
#endif
399+
}
391400
}
392401

393402
file static class Extensions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# BatchPaging_First_5
2+
3+
## 1
4+
5+
```json
6+
{
7+
"First": "UHJvZHVjdCAwLTA6MQ==",
8+
"Last": "UHJvZHVjdCAwLTE6Mg==",
9+
"Items": [
10+
{
11+
"Id": 1,
12+
"Name": "Product 0-0",
13+
"Description": null,
14+
"Price": 0.0,
15+
"ImageFileName": null,
16+
"TypeId": 1,
17+
"Type": null,
18+
"BrandId": 1,
19+
"Brand": null,
20+
"AvailableStock": 0,
21+
"RestockThreshold": 0,
22+
"MaxStockThreshold": 0,
23+
"OnReorder": false
24+
},
25+
{
26+
"Id": 2,
27+
"Name": "Product 0-1",
28+
"Description": null,
29+
"Price": 0.0,
30+
"ImageFileName": null,
31+
"TypeId": 1,
32+
"Type": null,
33+
"BrandId": 1,
34+
"Brand": null,
35+
"AvailableStock": 0,
36+
"RestockThreshold": 0,
37+
"MaxStockThreshold": 0,
38+
"OnReorder": false
39+
}
40+
]
41+
}
42+
```
43+
44+
## 2
45+
46+
```json
47+
{
48+
"First": "UHJvZHVjdCAxLTA6MTAx",
49+
"Last": "UHJvZHVjdCAxLTE6MTAy",
50+
"Items": [
51+
{
52+
"Id": 101,
53+
"Name": "Product 1-0",
54+
"Description": null,
55+
"Price": 0.0,
56+
"ImageFileName": null,
57+
"TypeId": 1,
58+
"Type": null,
59+
"BrandId": 2,
60+
"Brand": null,
61+
"AvailableStock": 0,
62+
"RestockThreshold": 0,
63+
"MaxStockThreshold": 0,
64+
"OnReorder": false
65+
},
66+
{
67+
"Id": 102,
68+
"Name": "Product 1-1",
69+
"Description": null,
70+
"Price": 0.0,
71+
"ImageFileName": null,
72+
"TypeId": 1,
73+
"Type": null,
74+
"BrandId": 2,
75+
"Brand": null,
76+
"AvailableStock": 0,
77+
"RestockThreshold": 0,
78+
"MaxStockThreshold": 0,
79+
"OnReorder": false
80+
}
81+
]
82+
}
83+
```
84+
85+
## 3
86+
87+
```json
88+
{
89+
"First": "UHJvZHVjdCAyLTA6MjAx",
90+
"Last": "UHJvZHVjdCAyLTE6MjAy",
91+
"Items": [
92+
{
93+
"Id": 201,
94+
"Name": "Product 2-0",
95+
"Description": null,
96+
"Price": 0.0,
97+
"ImageFileName": null,
98+
"TypeId": 1,
99+
"Type": null,
100+
"BrandId": 3,
101+
"Brand": null,
102+
"AvailableStock": 0,
103+
"RestockThreshold": 0,
104+
"MaxStockThreshold": 0,
105+
"OnReorder": false
106+
},
107+
{
108+
"Id": 202,
109+
"Name": "Product 2-1",
110+
"Description": null,
111+
"Price": 0.0,
112+
"ImageFileName": null,
113+
"TypeId": 1,
114+
"Type": null,
115+
"BrandId": 3,
116+
"Brand": null,
117+
"AvailableStock": 0,
118+
"RestockThreshold": 0,
119+
"MaxStockThreshold": 0,
120+
"OnReorder": false
121+
}
122+
]
123+
}
124+
```
125+
126+
## SQL 0
127+
128+
```sql
129+
SELECT t."BrandId", t0."Id", t0."AvailableStock", t0."BrandId", t0."Description", t0."ImageFileName", t0."MaxStockThreshold", t0."Name", t0."OnReorder", t0."Price", t0."RestockThreshold", t0."TypeId"
130+
FROM (
131+
SELECT p."BrandId"
132+
FROM "Products" AS p
133+
WHERE p."BrandId" IN (1, 2, 3)
134+
GROUP BY p."BrandId"
135+
) AS t
136+
LEFT JOIN (
137+
SELECT t1."Id", t1."AvailableStock", t1."BrandId", t1."Description", t1."ImageFileName", t1."MaxStockThreshold", t1."Name", t1."OnReorder", t1."Price", t1."RestockThreshold", t1."TypeId"
138+
FROM (
139+
SELECT p0."Id", p0."AvailableStock", p0."BrandId", p0."Description", p0."ImageFileName", p0."MaxStockThreshold", p0."Name", p0."OnReorder", p0."Price", p0."RestockThreshold", p0."TypeId", ROW_NUMBER() OVER(PARTITION BY p0."BrandId" ORDER BY p0."Name", p0."Id") AS row
140+
FROM "Products" AS p0
141+
WHERE p0."BrandId" = 1 OR p0."BrandId" = 2 OR p0."BrandId" = 3
142+
) AS t1
143+
WHERE t1.row <= 3
144+
) AS t0 ON t."BrandId" = t0."BrandId"
145+
ORDER BY t."BrandId", t0."BrandId", t0."Name", t0."Id"
146+
```
147+
148+
## Expression 0
149+
150+
```text
151+
[Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression].Where(t => (((t.BrandId == 1) OrElse (t.BrandId == 2)) OrElse (t.BrandId == 3))).GroupBy(k => k.BrandId).Select(g => new Group`2() {Key = g.Key, Items = g.OrderBy(p => p.Name).ThenBy(p => p.Id).Take(3).ToList()})
152+
```
153+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# BatchPaging_Last_5
2+
3+
## 1
4+
5+
```json
6+
{
7+
"First": "MTAw",
8+
"Last": "OTk=",
9+
"Items": [
10+
{
11+
"Id": 100,
12+
"Name": "Product 0-99",
13+
"Description": null,
14+
"Price": 0.0,
15+
"ImageFileName": null,
16+
"TypeId": 1,
17+
"Type": null,
18+
"BrandId": 1,
19+
"Brand": null,
20+
"AvailableStock": 0,
21+
"RestockThreshold": 0,
22+
"MaxStockThreshold": 0,
23+
"OnReorder": false
24+
},
25+
{
26+
"Id": 99,
27+
"Name": "Product 0-98",
28+
"Description": null,
29+
"Price": 0.0,
30+
"ImageFileName": null,
31+
"TypeId": 1,
32+
"Type": null,
33+
"BrandId": 1,
34+
"Brand": null,
35+
"AvailableStock": 0,
36+
"RestockThreshold": 0,
37+
"MaxStockThreshold": 0,
38+
"OnReorder": false
39+
}
40+
]
41+
}
42+
```
43+
44+
## 2
45+
46+
```json
47+
{
48+
"First": "MjAw",
49+
"Last": "MTk5",
50+
"Items": [
51+
{
52+
"Id": 200,
53+
"Name": "Product 1-99",
54+
"Description": null,
55+
"Price": 0.0,
56+
"ImageFileName": null,
57+
"TypeId": 1,
58+
"Type": null,
59+
"BrandId": 2,
60+
"Brand": null,
61+
"AvailableStock": 0,
62+
"RestockThreshold": 0,
63+
"MaxStockThreshold": 0,
64+
"OnReorder": false
65+
},
66+
{
67+
"Id": 199,
68+
"Name": "Product 1-98",
69+
"Description": null,
70+
"Price": 0.0,
71+
"ImageFileName": null,
72+
"TypeId": 1,
73+
"Type": null,
74+
"BrandId": 2,
75+
"Brand": null,
76+
"AvailableStock": 0,
77+
"RestockThreshold": 0,
78+
"MaxStockThreshold": 0,
79+
"OnReorder": false
80+
}
81+
]
82+
}
83+
```
84+
85+
## 3
86+
87+
```json
88+
{
89+
"First": "MzAw",
90+
"Last": "Mjk5",
91+
"Items": [
92+
{
93+
"Id": 300,
94+
"Name": "Product 2-99",
95+
"Description": null,
96+
"Price": 0.0,
97+
"ImageFileName": null,
98+
"TypeId": 1,
99+
"Type": null,
100+
"BrandId": 3,
101+
"Brand": null,
102+
"AvailableStock": 0,
103+
"RestockThreshold": 0,
104+
"MaxStockThreshold": 0,
105+
"OnReorder": false
106+
},
107+
{
108+
"Id": 299,
109+
"Name": "Product 2-98",
110+
"Description": null,
111+
"Price": 0.0,
112+
"ImageFileName": null,
113+
"TypeId": 1,
114+
"Type": null,
115+
"BrandId": 3,
116+
"Brand": null,
117+
"AvailableStock": 0,
118+
"RestockThreshold": 0,
119+
"MaxStockThreshold": 0,
120+
"OnReorder": false
121+
}
122+
]
123+
}
124+
```
125+
126+
## SQL 0
127+
128+
```sql
129+
SELECT t."BrandId", t0."Id", t0."AvailableStock", t0."BrandId", t0."Description", t0."ImageFileName", t0."MaxStockThreshold", t0."Name", t0."OnReorder", t0."Price", t0."RestockThreshold", t0."TypeId"
130+
FROM (
131+
SELECT p."BrandId"
132+
FROM "Products" AS p
133+
WHERE p."BrandId" IN (1, 2, 3)
134+
GROUP BY p."BrandId"
135+
) AS t
136+
LEFT JOIN (
137+
SELECT t1."Id", t1."AvailableStock", t1."BrandId", t1."Description", t1."ImageFileName", t1."MaxStockThreshold", t1."Name", t1."OnReorder", t1."Price", t1."RestockThreshold", t1."TypeId"
138+
FROM (
139+
SELECT p0."Id", p0."AvailableStock", p0."BrandId", p0."Description", p0."ImageFileName", p0."MaxStockThreshold", p0."Name", p0."OnReorder", p0."Price", p0."RestockThreshold", p0."TypeId", ROW_NUMBER() OVER(PARTITION BY p0."BrandId" ORDER BY p0."Id" DESC) AS row
140+
FROM "Products" AS p0
141+
WHERE p0."BrandId" = 1 OR p0."BrandId" = 2 OR p0."BrandId" = 3
142+
) AS t1
143+
WHERE t1.row <= 3
144+
) AS t0 ON t."BrandId" = t0."BrandId"
145+
ORDER BY t."BrandId", t0."BrandId", t0."Id" DESC
146+
```
147+
148+
## Expression 0
149+
150+
```text
151+
[Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression].Where(t => (((t.BrandId == 1) OrElse (t.BrandId == 2)) OrElse (t.BrandId == 3))).GroupBy(k => k.BrandId).Select(g => new Group`2() {Key = g.Key, Items = g.OrderByDescending(p => p.Id).Take(3).ToList()})
152+
```
153+

0 commit comments

Comments
 (0)