Skip to content

Instantly share code, notes, and snippets.

@jamesrcounts
Created July 25, 2014 16:12
Show Gist options
  • Select an option

  • Save jamesrcounts/e68ae4b3aae2ba7b0bb3 to your computer and use it in GitHub Desktop.

Select an option

Save jamesrcounts/e68ae4b3aae2ba7b0bb3 to your computer and use it in GitHub Desktop.
A more convenient way to write a saver when you want async
// New interface...
using System.Threading.Tasks;
public interface IAsyncSaver<T>
{
Task<T> Save(T item);
}
// Old way...
ISaver<Task<FileInfo>> saver = new AsyncFileSaver();
FileInfo foo = new FileInfo("foo");
// here we create a task for no reason other than to conform to
// the interface.
FileInfo bar = await saver.Save(Task.FromResult(foo));
// New way...
IAsyncSaver<FileInfo> saver = new AsyncFileSaver();
FileInfo foo = new FileInfo("foo");
FileInfo bar = await saver.Save(foo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment