Write a well designed async / non-async API

If you want the most maintainable option, only provide an async API, which is implemented without making any blocking calls or using any thread pool threads.

If you really want to have both async and synchronous APIs, then you’ll encounter a maintainability problem. You really need to implement it twice: once async and once synchronous. Both of those methods will look nearly identical so the initial implementation is easy, but you will end up with two separate nearly-identical methods so maintenance is problematic.

In particular, there’s a no good and simple way to just make an async or synchronous “wrapper”. Stephen Toub has the best info on the subject:

  1. Should I expose asynchronous wrappers for synchronous methods?
  2. Should I expose synchronous wrappers for asynchronous methods?

(the short answer to both questions is “no”)

However, there are some hacks you can use if you want to avoid the duplicated implementation; the best one is usually the boolean argument hack.

Leave a Comment