TimeProvider and the End of Untestable DateTime.Now
Read OriginalThis article explains how .NET 8's built-in TimeProvider abstract class solves the long-standing problem of testing code that relies on DateTime.Now. It describes the common pattern of wrapping system clock calls with custom interfaces like IDateTimeProvider or IClock, and how each team implemented it differently. With TimeProvider, developers get a standardized solution in the base class library, featuring methods like GetUtcNow(), GetLocalNow(), and timestamp-based elapsed time measurement. The article also mentions how Duende IdentityServer v8 replaced its own IClock wrapper with TimeProvider, making it a practical case study for migrating legacy code to this new approach.
Comments
No comments yet
Be the first to share your thoughts!
Browser Extension
Get instant access to AllDevBlogs from your browser
Top of the Week
No top articles yet