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 ?