ISqlBuilderLoader.LoadAsync(object[] parameters, CancellationToken cancellationToken) Method
.NET Standard 2.x
Asynchronously retrieves data by the built SQL statement.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public Task<ILoadable<TModel>> LoadAsync(object[] parameters, CancellationToken cancellationToken)
Parameters
parameters
System.Object[]
One or more values that you want to use as retrieval arguments in the SQL SELECT statement.
cancellationToken
CancellationToken
A cancellation token that can be used by other objects or threads to receive notice of cancellation.
Returns
Task<ILoadable<TModel>>
Returns a task that represents the asynchronous operation.
Examples
The following code example demonstrates how to use the LoadAsync
method. It uses a CancellationTokenSource type parameter to make this operation cancelable.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Appeon.ApiDoc.Models.School;
using SnapObjects.Data;
namespace Appeon.ApiDoc.ISqlBuilderLoaderExamples
{
public class LoadAsyncExample
{
private readonly SchoolContext _context;
private ISqlModelMapper mapper;
public LoadAsyncExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
mapper = _context.SqlModelMapper;
}
public async Task<int> Example2(CancellationTokenSource cts)
{
var builder = mapper.GetQueryBuilder<DepartmentByName>();
int count = 0;
try
{
// If a task has been cancelled, the call to the
// ISqlBuilderLoader.LoadAsync method throws an
// OperationCanceledException.
count = (await builder.LoadAsync(new object[]{ 1 },
cts.Token)).ToList().Count;
Console.WriteLine("Count: {0}", count);
}
catch (OperationCanceledException e)
{
// The operation was cancelled before completion.
Console.WriteLine(e.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
cts.Dispose();
}
return count;
/*The code produces the following output:
A task was canceled.
*/
}
}
}
Applies to
.NET Standard
2.x