Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

await/async Microsoft Practices Enterprise Library Data

I have an older application that I wrote where I used Microsoft.Practices.EnterpriseLibrary.Data to get data from the DB. I have recently upgraded to .NET 4.5 and wanted to advantage of await/async.

I do not see any methods ending in "Async" as per the naming standard, even in the most recent version of the package. Is it possible to use await/async with this ADO .NET library without manually making it asynchronous?

like image 932
ScubaSteve Avatar asked Apr 11 '14 22:04

ScubaSteve


People also ask

Is it OK to not await async?

If you forget to use await while calling an async function, the function starts executing. This means that await is not required for executing the function. The async function will return a promise, which you can use later.

Is await async the same as sync?

The differences between asynchronous and synchronous include: Async is multi-thread, which means operations or programs can run in parallel. Sync is single-thread, so only one operation or program will run at a time. Async is non-blocking, which means it will send multiple requests to a server.

Do I need to await an async function C#?

The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete. await can only be used inside an async method.

Does async await improve performance?

The main benefits of asynchronous programming using async / await include the following: Increase the performance and responsiveness of your application, particularly when you have long-running operations that do not require to block the execution.


1 Answers

I'm using an older version of the EL that offers Begin* / End* methods, but not async versions. Some simple extension methods simplify life:

public static async Task<IDataReader> ExecuteReaderAsync(this SqlDatabase database, DbCommand command)
{
    return await Task<IDataReader>.Factory.FromAsync(database.BeginExecuteReader, database.EndExecuteReader, command, null);
}

public static async Task<object> ExecuteScalarAsync(this SqlDatabase database, DbCommand command)
{
    return await Task<object>.Factory.FromAsync(database.BeginExecuteScalar, database.EndExecuteScalar, command, null);
}

public static async Task<XmlReader> ExecuteXmlReaderAsync(this SqlDatabase database, DbCommand command)
{
    return await Task<XmlReader>.Factory.FromAsync(database.BeginExecuteXmlReader, database.EndExecuteXmlReader, command, null);
}

public static async Task<int> ExecuteNonQueryAsync(this SqlDatabase database, DbCommand command)
{
    return await Task<int>.Factory.FromAsync(database.BeginExecuteNonQuery, database.EndExecuteNonQuery, command, null);
}
like image 85
Eric Patrick Avatar answered Sep 28 '22 06:09

Eric Patrick