Last active
August 29, 2015 14:25
-
-
Save caleb-vear/ec814433317e28359913 to your computer and use it in GitHub Desktop.
An example of an extension method for an interface I control.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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