async
isn't a part of the signature, so you don't actually need to be concerned with whether the method implementing the interface is async
or not, you only need to be concerned with the types of the properties, the return type, the name of the method, and the accessibility.
The real difference is that your async
methods will need to return a Task
or a Task<T>
, whereas the non-async methods are most likely currently returning void
or some type, T
directly.
If you want to "future proof" your application one option is to ensure that all of your interfaces return Task
or Task<T>
and that for your EF4/EF5 implementations you wrap your results in a completed task even though they're executed synchronously.
Task.FromResult
was added in .NET 4.5, but if you don't have it you can write your own easily enough:
public static Task<T> FromResult<T>(T result)
{
var tcs = new TaskCompletionSource<T>();
tcs.SetResult(result);
return tcs.Task;
}
You can also write a CompletedTask
method that simply returns a task that has already completed: (It caches a single task for efficiency reasons.)
private static Task _completedTask;
public static Task CompletedTask()
{
return _completedTask ?? initCompletedTask();
}
private static Task initCompletedTask()
{
var tcs = new TaskCompletionSource<object>();
tcs.SetResult(null);
_completedTask = tcs.Task;
return _completedTask;
}
Those two methods will simplify the process of having all of your methods returning some type of Task
, although doing this will make your code a bit messier until you're able to use C# 5.0 to be able to await
the result.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…