Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Linq to Sql asynchronously with new async/await

What are best practices to use L2S with new C# 5 async/await keywords comparing to this approach? Couldn't find any on web.

like image 544
UserControl Avatar asked Sep 19 '12 16:09

UserControl


People also ask

Is LINQ to SQL obsolete?

No it is not.

Does async await create new thread?

The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active.

Is LINQ asynchronous?

Note that there are no async versions of some LINQ operators such as Where or OrderBy, because these only build up the LINQ expression tree and don't cause the query to be executed in the database. Only operators which cause query execution have async counterparts.

Does async await make it synchronous?

Top-level code, up to and including the first await expression (if there is one), is run synchronously. In this way, an async function without an await expression will run synchronously. If there is an await expression inside the function body, however, the async function will always complete asynchronously.


2 Answers

EF 5 does not have async/await support, but the open source version is actively looking into possibilities here. EDIT: the Async support in EF is documented at http://msdn.microsoft.com/en-us/data/jj819165.aspx. It doesn't stream the results in as they are hydrated (as you would find with RX) but it does make the database calls asynchronous.

As for LINQ to SQL, outside of wrapping your request in a Task.Factory.Start operation, I wouldn't hold my breath hoping that task based async (required for async/await) will be implemented by Microsoft for Linq to SQL.

You could use the IQToolkit and extend it adding your own async support if absolutely necessary. Also, Mono has implemented LINQ to SQL which you might be able to extend with async support.

like image 69
Jim Wooley Avatar answered Oct 10 '22 19:10

Jim Wooley


Scott Hanselman has an interesting post where he demonstrates how one could produce an async API on top of an existing Linq to SQL query. I haven't time too play around with the idea but I'm guessing that one could create a more generic extension method which would allows the same technique to be expanded to any object of type IQueryable or IEnumerable.

Here is the code directly from his post to use as a reference.

SqlCommand _beginFindCmd = null;

public IAsyncResult BeginFind(int id, AsyncCallback callback, Object asyncState)
{
    var query = from w in _db.Widgets
                where w.Id == id
                select w;
    _beginFindCmd = _db.GetCommand(query) as SqlCommand;
    _db.Connection.Open();
    return _beginFindCmd.BeginExecuteReader(callback, asyncState, System.Data.CommandBehavior.CloseConnection);
}

public Widget EndFind(IAsyncResult result)
{
    var rdr = _beginFindCmd.EndExecuteReader(result);
    var widget = (from w in _db.Translate<Widget>(rdr)
                  select w).SingleOrDefault();
    rdr.Close();
    return widget;
}

With a little bit of work one could make this TPL and likewise even cleaner as a single async method. If I get a chance to do just this I'll post what I come up with.

like image 29
jpierson Avatar answered Oct 10 '22 19:10

jpierson