Skip to content

Instantly share code, notes, and snippets.

@caleb-vear
Last active August 29, 2015 14:25
Show Gist options
  • Save caleb-vear/ec814433317e28359913 to your computer and use it in GitHub Desktop.
Save caleb-vear/ec814433317e28359913 to your computer and use it in GitHub Desktop.
An example of an extension method for an interface I control.
// We are keeping the interface super simple to implement.
// It turns out that there are a number of situations where I want to
// send a message after a delay of a given period. Rather than force
// every implementation to provide that I add via the PublishAfterDelay
// extension method.
public interface ISimulatorBus
{
void Publish(object message);
}
public static class SimulatorBusExtensions
{
public static Task PublishAfterDelay(this ISimulatorBus bus, object message, TimeSpan timeUntilSend)
{
return PublishAfterDelay(bus, message, timeUntilSend, CancellationToken.None);
}
public static async Task PublishAfterDelay(
this ISimulatorBus bus,
object message,
TimeSpan timeUntilSend,
CancellationToken cancellationToken)
{
await Task.Delay(timeUntilSend, cancellationToken);
bus.Publish(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment