Skip to content

Commit 061cf32

Browse files
committed
QueryEndpoint.SingleAsync also has option for passing single key value
1 parent e81cb9f commit 061cf32

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

Orm/Xtensive.Orm.Tests/Storage/Prefetch/FetchByKeyWithCachingTest.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void SingleByExistingIdTest()
143143
}
144144

145145
[Test]
146-
public async Task SingleByExistingIdAsyncTest()
146+
public async Task SingleByExistingIdAsyncTest1()
147147
{
148148
await RunWithinSessionAsync(async (s) => {
149149
var existingId = (int) existingKeys[customerType][0].Value.GetValue(0, out var _);
@@ -164,6 +164,28 @@ await RunWithinSessionAsync(async (s) => {
164164
});
165165
}
166166

167+
[Test]
168+
public async Task SingleByExistingIdAsyncTest2()
169+
{
170+
await RunWithinSessionAsync(async (s) => {
171+
var existingId = (int) existingKeys[customerType][0].Value.GetValue(0, out var _);
172+
var detector = new QueryExecutionDetector();
173+
using (detector.Attach(s)) {
174+
// Entity is not in cache yet
175+
var existingEntity = await s.Query.SingleAsync<Customer>(existingId);
176+
}
177+
Assert.That(detector.DbCommandsDetected, Is.True);
178+
detector.Reset();
179+
180+
using (detector.Attach(s)) {
181+
// now it is in cache
182+
var existingEntity = await s.Query.SingleAsync<Customer>(existingId);
183+
}
184+
Assert.That(detector.DbCommandsDetected, Is.False);
185+
detector.Reset();
186+
});
187+
}
188+
167189
[Test]
168190
public void SingleByInexistentIdTest()
169191
{
@@ -186,7 +208,7 @@ public void SingleByInexistentIdTest()
186208
}
187209

188210
[Test]
189-
public async Task SingleByInexistentIdAsyncTest()
211+
public async Task SingleByInexistentIdAsyncTest1()
190212
{
191213
await RunWithinSessionAsync(async (s) => {
192214
var inexistentId = 9999;
@@ -207,6 +229,28 @@ await RunWithinSessionAsync(async (s) => {
207229
});
208230
}
209231

232+
[Test]
233+
public async Task SingleByInexistentIdAsyncTest2()
234+
{
235+
await RunWithinSessionAsync(async (s) => {
236+
var inexistentId = 9999;
237+
238+
var detector = new QueryExecutionDetector();
239+
using (detector.Attach(s)) {
240+
_ = Assert.ThrowsAsync<KeyNotFoundException>(async () => await s.Query.SingleAsync<Customer>(inexistentId));
241+
}
242+
Assert.That(detector.DbCommandsDetected, Is.True);
243+
detector.Reset();
244+
245+
using (detector.Attach(s)) {
246+
_ = Assert.ThrowsAsync<KeyNotFoundException>(async () => await s.Query.SingleAsync<Customer>(inexistentId));
247+
}
248+
Assert.That(detector.DbCommandsDetected, Is.False);
249+
detector.Reset();
250+
await Task.CompletedTask;
251+
});
252+
}
253+
210254
[Test]
211255
public void SingleOrDefaultByExistingKeyTest()
212256
{

Orm/Xtensive.Orm/Orm/Query.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,23 @@ public static Task<T> SingleAsync<T>(object[] keyValues, CancellationToken token
316316
return Session.Demand().Query.SingleAsync<T>(keyValues, token);
317317
}
318318

319+
/// <summary>
320+
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="keyValue"/>
321+
/// in the current <see cref="Session"/>.
322+
/// </summary>
323+
/// <typeparam name="T">Type of the entity.</typeparam>
324+
/// <param name="keyValue">Key value.</param>
325+
/// <param name="token">The token to cancel this operation.</param>
326+
/// <returns>
327+
/// The <see cref="Entity"/> specified <paramref name="keyValue"/> identify.
328+
/// </returns>
329+
/// <exception cref="KeyNotFoundException">Entity with the specified key is not found.</exception>
330+
public static Task<T> SingleAsync<T>(object keyValue, CancellationToken token)
331+
where T : class, IEntity
332+
{
333+
return Session.Demand().Query.SingleAsync<T>(keyValue, token);
334+
}
335+
319336
/// <summary>
320337
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="key"/>
321338
/// in the current <see cref="Session"/>.

Orm/Xtensive.Orm/Orm/QueryEndpoint.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,23 @@ public async Task<T> SingleAsync<T>(object[] keyValues, CancellationToken token
428428
return (T) (object) (await SingleAsync(GetKeyByValues<T>(keyValues), token).ConfigureAwait(false));
429429
}
430430

431+
/// <summary>
432+
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="keyValue"/>
433+
/// in the current <see cref="session"/>.
434+
/// </summary>
435+
/// <typeparam name="T">Type of the entity.</typeparam>
436+
/// <param name="keyValue">Key value.</param>
437+
/// <param name="token">The token to cancel this operation.</param>
438+
/// <returns>
439+
/// The <see cref="Entity"/> specified <paramref name="keyValue"/> identify.
440+
/// </returns>
441+
/// <exception cref="KeyNotFoundException">Entity with the specified key is not found.</exception>
442+
public async Task<T> SingleAsync<T>(object keyValue, CancellationToken token = default)
443+
where T : class, IEntity
444+
{
445+
return (T) (object) (await SingleAsync(GetKeyByValue<T>(keyValue), token).ConfigureAwait(false));
446+
}
447+
431448
/// <summary>
432449
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="key"/>
433450
/// in the current <see cref="session"/>.

0 commit comments

Comments
 (0)