Last active
January 7, 2016 15:58
-
-
Save ssajous/e1e4e794e88629165314 to your computer and use it in GitHub Desktop.
Time Provider Ambient Context for mockable date times
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
public abstract class TimeProvider | |
{ | |
private static TimeProvider current; | |
static TimeProvider() | |
{ | |
TimeProvider.current = | |
new DefaultTimeProvider(); | |
} | |
public static TimeProvider Current | |
{ | |
get { return TimeProvider.current; } | |
set | |
{ | |
if (value == null) | |
{ | |
throw new ArgumentNullException("value"); | |
} | |
TimeProvider.current = value; | |
} | |
} | |
public abstract DateTime UtcNow { get; } | |
public static void ResetToDefault() | |
{ | |
TimeProvider.current = | |
new DefaultTimeProvider(); | |
} | |
} | |
public class DefaultTimeProvider : TimeProvider | |
{ | |
public override DateTime UtcNow | |
{ | |
get { return DateTime.UtcNow; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment