-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnippets.cs
More file actions
200 lines (160 loc) · 4.6 KB
/
Copy pathSnippets.cs
File metadata and controls
200 lines (160 loc) · 4.6 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// ReSharper disable All
#pragma warning disable IDE0022
#pragma warning disable IDE0059
namespace Snippets;
public class EnableExample :
DbContext
{
#region EnableQueryComplexity
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder.UseQueryComplexity();
#endregion
}
public class LogAtExample :
DbContext
{
#region LogAt
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder.UseQueryComplexity(
logAt: QueryComplexityLimits.LogDefaults with
{
MaxTake = 500,
RejectUnbounded = false
});
#endregion
}
public class ThrowAtExample :
DbContext
{
#region ThrowAt
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder.UseQueryComplexity(
throwAt: new(
MaxNodes: 4096,
MaxDepth: 64,
MaxOperators: 64,
MaxNavigationDepth: 4,
MaxIncludes: 16,
MaxIncludeDepth: 4,
MaxTake: 1000,
MaxInValues: 1000,
RejectUnbounded: true));
#endregion
}
public class UnboundedOffExample :
DbContext
{
#region RejectUnboundedOff
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder.UseQueryComplexity(
logAt: QueryComplexityLimits.LogDefaults with
{
// Every table is small, so a query with no Take is fine
RejectUnbounded = false
});
#endregion
}
public class AllExceptExample :
DbContext
{
#region RejectUnboundedAllExcept
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder.UseQueryComplexity(
logAt: QueryComplexityLimits.LogDefaults with
{
// Few rows, so returning all of them is fine
RejectUnbounded = UnboundedEntities.AllExcept(
typeof(User),
typeof(AccessGroup))
});
#endregion
}
public class OnlyExample :
DbContext
{
#region RejectUnboundedOnly
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder.UseQueryComplexity(
logAt: QueryComplexityLimits.LogDefaults with
{
// Many rows, so every query for them needs a Take
RejectUnbounded = UnboundedEntities.Only(
typeof(Commitment))
});
#endregion
}
public class CostLimitExample :
DbContext
{
#region SqlServerCostLimit
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder
.UseSqlServer("connection-string")
.UseQueryComplexity(sqlServerCostLimit: 300);
#endregion
}
public class ConfigureWarningsExample :
DbContext
{
#region EscalateWithConfigureWarnings
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder
.UseQueryComplexity()
.ConfigureWarnings(
_ => _.Throw(QueryComplexityEventId.LimitExceeded));
#endregion
}
public class KeepLoggingExample :
DbContext
{
#region KeepLoggingWhenWarningsThrow
protected override void OnConfiguring(DbContextOptionsBuilder builder) =>
builder
.UseQueryComplexity()
.ConfigureWarnings(
_ => _
.Default(WarningBehavior.Throw)
.Log(QueryComplexityEventId.LimitExceeded));
#endregion
}
public class SnippetExamples
{
static async Task Ignore()
{
AppDbContext context = null!;
#region IgnoreQueryComplexity
// Skips every check for this query
var employees = await context.Employees
.IgnoreQueryComplexity()
.ToListAsync();
#endregion
}
static async Task Override()
{
AppDbContext context = null!;
#region WithQueryComplexity
// Replaces levels for this query only
var employees = await context.Employees
.WithQueryComplexity(
new()
{
MaxTake = 5000
})
.Take(5000)
.ToListAsync();
#endregion
}
}
public class SnippetEmployee
{
public int Id { get; set; }
public string Name { get; set; } = "";
}
public class User;
public class AccessGroup;
public class Commitment;
public class AppDbContext :
DbContext
{
public DbSet<SnippetEmployee> Employees => Set<SnippetEmployee>();
}