Skip to content

Commit f91c3b2

Browse files
authored
Store query options in a query provider instead of queryable (#1412)
- Deprecate SetOptions in favor of WithOptions Fixes #1372
1 parent e408d06 commit f91c3b2

26 files changed

+424
-462
lines changed

src/NHibernate.Test/Async/Linq/DynamicQueryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task CanCacheDynamicLinqAsync()
3535
{
3636
//dynamic orderby clause
3737
var users = db.Users
38-
.SetOptions(o => o.SetCacheable(true))
38+
.WithOptions(o => o.SetCacheable(true))
3939
.Fetch(x => x.Role)
4040
.OrderBy("RegisteredAt");
4141

src/NHibernate.Test/Async/Linq/QueryCacheableTests.cs

Lines changed: 77 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
namespace NHibernate.Test.Linq
1717
{
1818
using System.Threading.Tasks;
19-
using System.Threading;
2019
[TestFixture]
2120
public class QueryCacheableTestsAsync : LinqTestCase
2221
{
@@ -31,16 +30,16 @@ protected override void Configure(Configuration cfg)
3130
public async Task QueryIsCacheableAsync()
3231
{
3332
Sfi.Statistics.Clear();
34-
await (Sfi.QueryCache.ClearAsync(CancellationToken.None));
33+
await (Sfi.EvictQueriesAsync());
3534

3635
var x = await ((from c in db.Customers
3736
select c)
38-
.SetOptions(o => o.SetCacheable(true))
37+
.WithOptions(o => o.SetCacheable(true))
3938
.ToListAsync());
4039

4140
var x2 = await ((from c in db.Customers
4241
select c)
43-
.SetOptions(o => o.SetCacheable(true))
42+
.WithOptions(o => o.SetCacheable(true))
4443
.ToListAsync());
4544

4645
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
@@ -52,11 +51,11 @@ public async Task QueryIsCacheableAsync()
5251
public async Task QueryIsCacheable2Async()
5352
{
5453
Sfi.Statistics.Clear();
55-
await (Sfi.QueryCache.ClearAsync(CancellationToken.None));
54+
await (Sfi.EvictQueriesAsync());
5655

5756
var x = await ((from c in db.Customers
5857
select c)
59-
.SetOptions(o => o.SetCacheable(true))
58+
.WithOptions(o => o.SetCacheable(true))
6059
.ToListAsync());
6160

6261
var x2 = await ((from c in db.Customers
@@ -71,9 +70,9 @@ public async Task QueryIsCacheable2Async()
7170
public async Task QueryIsCacheable3Async()
7271
{
7372
Sfi.Statistics.Clear();
74-
await (Sfi.QueryCache.ClearAsync(CancellationToken.None));
73+
await (Sfi.EvictQueriesAsync());
7574

76-
var x = await ((from c in db.Customers.SetOptions(o => o.SetCacheable(true))
75+
var x = await ((from c in db.Customers.WithOptions(o => o.SetCacheable(true))
7776
select c).ToListAsync());
7877

7978
var x2 = await ((from c in db.Customers
@@ -88,21 +87,23 @@ public async Task QueryIsCacheable3Async()
8887
public async Task QueryIsCacheableWithRegionAsync()
8988
{
9089
Sfi.Statistics.Clear();
91-
await (Sfi.QueryCache.ClearAsync(CancellationToken.None));
90+
await (Sfi.EvictQueriesAsync());
91+
await (Sfi.EvictQueriesAsync("test"));
92+
await (Sfi.EvictQueriesAsync("other"));
9293

9394
var x = await ((from c in db.Customers
9495
select c)
95-
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
96+
.WithOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
9697
.ToListAsync());
9798

9899
var x2 = await ((from c in db.Customers
99100
select c)
100-
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
101+
.WithOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
101102
.ToListAsync());
102103

103104
var x3 = await ((from c in db.Customers
104105
select c)
105-
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("other"))
106+
.WithOptions(o => o.SetCacheable(true).SetCacheRegion("other"))
106107
.ToListAsync());
107108

108109
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
@@ -114,10 +115,10 @@ public async Task QueryIsCacheableWithRegionAsync()
114115
public async Task CacheableBeforeOtherClausesAsync()
115116
{
116117
Sfi.Statistics.Clear();
117-
await (Sfi.QueryCache.ClearAsync(CancellationToken.None));
118+
await (Sfi.EvictQueriesAsync());
118119

119120
await (db.Customers
120-
.SetOptions(o => o.SetCacheable(true))
121+
.WithOptions(o => o.SetCacheable(true))
121122
.Where(c => c.ContactName != c.CompanyName).Take(1).ToListAsync());
122123
await (db.Customers.Where(c => c.ContactName != c.CompanyName).Take(1).ToListAsync());
123124

@@ -130,18 +131,20 @@ public async Task CacheableBeforeOtherClausesAsync()
130131
public async Task CacheableRegionBeforeOtherClausesAsync()
131132
{
132133
Sfi.Statistics.Clear();
133-
await (Sfi.QueryCache.ClearAsync(CancellationToken.None));
134+
await (Sfi.EvictQueriesAsync());
135+
await (Sfi.EvictQueriesAsync("test"));
136+
await (Sfi.EvictQueriesAsync("other"));
134137

135138
await (db.Customers
136-
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
139+
.WithOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
137140
.Where(c => c.ContactName != c.CompanyName).Take(1)
138141
.ToListAsync());
139142
await (db.Customers
140-
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
143+
.WithOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
141144
.Where(c => c.ContactName != c.CompanyName).Take(1)
142145
.ToListAsync());
143146
await (db.Customers
144-
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("other"))
147+
.WithOptions(o => o.SetCacheable(true).SetCacheRegion("other"))
145148
.Where(c => c.ContactName != c.CompanyName).Take(1)
146149
.ToListAsync());
147150

@@ -154,15 +157,17 @@ public async Task CacheableRegionBeforeOtherClausesAsync()
154157
public async Task CacheableRegionSwitchedAsync()
155158
{
156159
Sfi.Statistics.Clear();
157-
await (Sfi.QueryCache.ClearAsync(CancellationToken.None));
160+
await (Sfi.EvictQueriesAsync());
161+
await (Sfi.EvictQueriesAsync("test"));
158162

159163
await (db.Customers
160164
.Where(c => c.ContactName != c.CompanyName).Take(1)
161-
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
165+
.WithOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
162166
.ToListAsync());
167+
163168
await (db.Customers
164169
.Where(c => c.ContactName != c.CompanyName).Take(1)
165-
.SetOptions(o => o.SetCacheRegion("test").SetCacheable(true))
170+
.WithOptions(o => o.SetCacheRegion("test").SetCacheable(true))
166171
.ToListAsync());
167172

168173
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
@@ -174,13 +179,13 @@ public async Task CacheableRegionSwitchedAsync()
174179
public async Task GroupByQueryIsCacheableAsync()
175180
{
176181
Sfi.Statistics.Clear();
177-
await (Sfi.QueryCache.ClearAsync(CancellationToken.None));
182+
await (Sfi.EvictQueriesAsync());
178183

179184
var c = await (db
180185
.Customers
181186
.GroupBy(x => x.Address.Country)
182187
.Select(x => x.Key)
183-
.SetOptions(o => o.SetCacheable(true))
188+
.WithOptions(o => o.SetCacheable(true))
184189
.ToListAsync());
185190

186191
c = await (db
@@ -193,7 +198,7 @@ public async Task GroupByQueryIsCacheableAsync()
193198
.Customers
194199
.GroupBy(x => x.Address.Country)
195200
.Select(x => x.Key)
196-
.SetOptions(o => o.SetCacheable(true))
201+
.WithOptions(o => o.SetCacheable(true))
197202
.ToListAsync());
198203

199204
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
@@ -205,11 +210,11 @@ public async Task GroupByQueryIsCacheableAsync()
205210
public async Task GroupByQueryIsCacheable2Async()
206211
{
207212
Sfi.Statistics.Clear();
208-
await (Sfi.QueryCache.ClearAsync(CancellationToken.None));
213+
await (Sfi.EvictQueriesAsync());
209214

210215
var c = await (db
211216
.Customers
212-
.SetOptions(o => o.SetCacheable(true))
217+
.WithOptions(o => o.SetCacheable(true))
213218
.GroupBy(x => x.Address.Country)
214219
.Select(x => x.Key)
215220
.ToListAsync());
@@ -222,7 +227,7 @@ public async Task GroupByQueryIsCacheable2Async()
222227

223228
c = await (db
224229
.Customers
225-
.SetOptions(o => o.SetCacheable(true))
230+
.WithOptions(o => o.SetCacheable(true))
226231
.GroupBy(x => x.Address.Country)
227232
.Select(x => x.Key)
228233
.ToListAsync());
@@ -231,5 +236,50 @@ public async Task GroupByQueryIsCacheable2Async()
231236
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
232237
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
233238
}
239+
240+
[Test]
241+
public async Task CanBeCombinedWithFetchAsync()
242+
{
243+
//NH-2587
244+
//NH-3982 (GH-1372)
245+
246+
Sfi.Statistics.Clear();
247+
await (Sfi.EvictQueriesAsync());
248+
249+
await (db.Customers
250+
.WithOptions(o => o.SetCacheable(true))
251+
.ToListAsync());
252+
253+
await (db.Orders
254+
.WithOptions(o => o.SetCacheable(true))
255+
.ToListAsync());
256+
257+
await (db.Customers
258+
.WithOptions(o => o.SetCacheable(true))
259+
.Fetch(x => x.Orders)
260+
.ToListAsync());
261+
262+
await (db.Orders
263+
.WithOptions(o => o.SetCacheable(true))
264+
.Fetch(x => x.OrderLines)
265+
.ToListAsync());
266+
267+
var customer = await (db.Customers
268+
.WithOptions(o => o.SetCacheable(true))
269+
.Fetch(x => x.Address)
270+
.Where(x => x.CustomerId == "VINET")
271+
.SingleOrDefaultAsync());
272+
273+
customer = await (db.Customers
274+
.WithOptions(o => o.SetCacheable(true))
275+
.Fetch(x => x.Address)
276+
.Where(x => x.CustomerId == "VINET")
277+
.SingleOrDefaultAsync());
278+
279+
Assert.That(NHibernateUtil.IsInitialized(customer.Address), Is.True, "Expected the fetched Address to be initialized");
280+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(5), "Unexpected execution count");
281+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(5), "Unexpected cache put count");
282+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
283+
}
234284
}
235285
}

src/NHibernate.Test/Async/Linq/QueryTimeoutTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task CanSetTimeoutOnLinqQueriesAsync()
4444
var result = await ((from e in db.Customers
4545
where e.CompanyName == "Corp"
4646
select e)
47-
.SetOptions(o => o.SetTimeout(17))
47+
.WithOptions(o => o.SetTimeout(17))
4848
.ToListAsync());
4949

5050
Assert.That(TimeoutCatchingNonBatchingBatcher.LastCommandTimeout, Is.EqualTo(17));
@@ -58,7 +58,7 @@ public async Task CanSetTimeoutOnLinqPagingQueryAsync()
5858
where e.CompanyName == "Corp"
5959
select e)
6060
.Skip(5).Take(5)
61-
.SetOptions(o => o.SetTimeout(17))
61+
.WithOptions(o => o.SetTimeout(17))
6262
.ToListAsync());
6363

6464
Assert.That(TimeoutCatchingNonBatchingBatcher.LastCommandTimeout, Is.EqualTo(17));
@@ -71,7 +71,7 @@ public async Task CanSetTimeoutBeforeSkipOnLinqOrderedPageQueryAsync()
7171
var result = await ((from e in db.Customers
7272
orderby e.CompanyName
7373
select e)
74-
.SetOptions(o => o.SetTimeout(17))
74+
.WithOptions(o => o.SetTimeout(17))
7575
.Skip(5).Take(5)
7676
.ToListAsync());
7777

@@ -84,15 +84,15 @@ public async Task CanSetTimeoutOnLinqGroupPageQueryAsync()
8484
{
8585
var subQuery = db.Customers
8686
.Where(e2 => e2.CompanyName.Contains("a")).Select(e2 => e2.CustomerId)
87-
.SetOptions(o => o.SetTimeout(18)); // This Timeout() should not cause trouble, and be ignored.
87+
.WithOptions(o => o.SetTimeout(18)); // This Timeout() should not cause trouble, and be ignored.
8888

8989
var result = await ((from e in db.Customers
9090
where subQuery.Contains(e.CustomerId)
9191
group e by e.CompanyName
9292
into g
9393
select new { g.Key, Count = g.Count() })
9494
.Skip(5).Take(5)
95-
.SetOptions(o => o.SetTimeout(17))
95+
.WithOptions(o => o.SetTimeout(17))
9696
.ToListAsync());
9797

9898
Assert.That(TimeoutCatchingNonBatchingBatcher.LastCommandTimeout, Is.EqualTo(17));

0 commit comments

Comments
 (0)