0

As expected I get compiler warnings for the scenario. However the methods involved are called ....Async because they are implementations of a common data IO interface whose concrete classes sometimes have the ability to call their data stores Async (specifically SQL and Mongo).

I would normally put a "#pragma warning" to prevent the warnings, but is there a technically better way of going about the problem. Here the issue is that the Interface implies (via naming convention) that the methods are Async and I'd like the consumer to Await my method calls for those circumstances where the concrete class can indeed make Async calls - so I dont want to split the interface into Async and non-Async calls.

Am I Ok to Pragma out the warnings, or is there some deeper reason for the warning that I'm not understanding.

The scenario is like this;

public interface IGetData 
{
    Task<IEnumerable<Person>> GetPeopleAsync();
}

public class GetDataViaSQLServer : IGetData
{
    public async Task<IEnumerable<Person>> GetPeopleAsync()
    {
        return await SqlCommand.ExecuteAsync("spoc_getpeople"); // Pseudo code to illustrate the async call
    }
}

public class GetDataViaMongo : IGetData
{
    public async Task<IEnumerable<Person>> GetPeopleAsync()
    {
        IEnumerable<Person> people = mongoCollection<Person>(); // Psuedocode illustrating lack of mongo async capability
        return people;
    }
}

The second class will give a compiler warning that it is declared async but has no await dependencies. The internal implementation of the two concrete classes is hidden from the consumer but the naming convention implies they should await the call return value;

IGetData dataFetcher = ioc.Resolve<IGetData>();
IEnumerable<Person> people = await dataFetcher.GetPeopleAsync();

I dont really want them to see any warnings when they compile - but (as an example) the Mongo implementation doesn support Async but the SQL Server one does. Is there a better solution than using #pragma to stop the warnings ?

5
  • 3
    You should post your code, it will make it easier to see what you mean. Commented Sep 29, 2014 at 13:51
  • 1
    I am confused by the question. If the method contains no awaits then why is it to advantage to mark it async? Do not ignore the warning. Remove the async keyword. Commented Sep 29, 2014 at 14:07
  • @EricLippert unless I'm mistaken it prevents me from needing to construct the Task by hand, leading to cleaner code. Commented Sep 29, 2014 at 14:09
  • 1
    If the method contains no awaits but you want it to return a task then just construct the task from the value. Its a single method call. Commented Sep 29, 2014 at 14:11
  • @EricLippert Ok - that seems a good enough reply. Its the same as DanBryant below. Commented Sep 29, 2014 at 14:14

1 Answer 1

2

I have a static TaskHelpers class containing this:

public static readonly Task CompletedTask = Task.FromResult<object>(null);

To create a synchronous implementation, I then just do:

    public Task MyMethodAsync()
    {
        //... Do stuff ...
        return TaskHelpers.CompletedTask;
    }

I use the helper class just to cache the completed task. Thankfully 'await' uses a fast path for already-completed tasks, so the performance impact is not bad.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.