I want to use recent asynchronous C # 5.0 methods, but in some specific way.
Consider the following code example:
public abstract class SomeBaseProvider : ISomeProvider
{
public abstract Task<string> Process(SomeParameters parameters);
}
public class SomeConcreteProvider1 : SomeBaseProvider
{
public override Task<string> Process(SomeParameters parameters) {
string result = "string which I don't need any async code to get";
return Task.Run(() => result);
}
}
public class SomeConcreteProvider2 : SomeBaseProvider
{
public async override Task<string> Process(SomeParameters parameters) {
var data = await new WebClient().DownloadDataTaskAsync(urlToRequest);
string result =
return result;
}
}
Now, as I will use asynchronous methods (you can ignore the fact that the consumer is actually an ASP.NET MVC4 app in my case ... it could be anything):
public class SomeAsyncController : AsyncController
{
public async Task<ActionResult> SomethingAsync(string providerId)
{
var provider = SomeService.GetProvider(providerId);
string result = await provider.Process(new SomeParameters(...));
return Content(result);
}
}
As you can see, I have 2 implementations, each of them will work differently: one I want to run in async and not block the flow (SomeConcreteProvider2), and the other I want to be able to start synchronization and not create any object Task, etc. (which I cannot code in the code above, that is, I am creating a new task here!).
, async <T> ?. - ... , - , (.. ), - /I/O .. , , SomeConcreteProvider1 (html), . SomeConcreteProvider2 -, - - , - Async, ( , ).
, : ( ?), , , , , Task.Run(... ) SomeConcreteProvider1.Process?
1: ( , ), , - (, isAsyncImplementation), , , , ( ) : DI - , : D