How to maintain C# shared library codebase which can but not have to use async/await logic depending on destination platform.
I'm building .NET Standard library which will fulfill some business tasks related with asynchronous logic (http calls). Now I'm looking for solution to have one codebase and share it between:
- Xamarin.Forms (Android, maybe UWP)
- .NET Framework (WinForms)
- .NET CompactFramework (WinForms).
Problem resides in last environment which does not support async/await at all. Right now I'm recompiling library code under VS2008 project with files added as links. But result code is not easy to maintain because its based on preprocessor. By using #if directive I can enable and disable async/await code parts and prepare code for compilation.
Is there any other solution to simplify such multi platform code?
using System;
using System.Collections.Generic;
using System.Text;
#if ASYNC
using System.Threading.Tasks;
#endif
namespace SharedLibTest
{
public interface IDbEventSource
{
#if ASYNC
Task<DbEventResult> ExecuteSource(EventContext ctx);
#else
DbEventResult ExecuteSource(EventContext ctx);
#endif
}
interface IEventStep
{
#if ASYNC
Task Execute(EventContext ctx);
#else
void Execute(EventContext ctx);
#endif
}
class EventStepDb : IEventStep
{
private readonly IDbEventSource _dbsrc;
public EventStepDb(IDbEventSource dbsrc)
{
this._dbsrc = dbsrc;
}
#if ASYNC
public async Task Execute(EventContext ctx)
#else
public void Execute(EventContext ctx)
#endif
{
#if ASYNC
DbEventResult res = await _dbsrc.ExecuteSource(ctx);
#else
DbEventResult res = _dbsrc.ExecuteSource(ctx);
#endif
ctx.State = res.Answer;
}
}
}