DateTimeProviders 1.0.0
dotnet add package DateTimeProviders --version 1.0.0
NuGet\Install-Package DateTimeProviders -Version 1.0.0
<PackageReference Include="DateTimeProviders" Version="1.0.0" />
paket add DateTimeProviders --version 1.0.0
#r "nuget: DateTimeProviders, 1.0.0"
// Install DateTimeProviders as a Cake Addin #addin nuget:?package=DateTimeProviders&version=1.0.0 // Install DateTimeProviders as a Cake Tool #tool nuget:?package=DateTimeProviders&version=1.0.0
DateTimeProviders
DateTimeProviders aims to solve the problems around testing code that has dependencies on the DateTime/DateTimeOffset structs, and also enforcing consistent use of Now and UtcNow.
It does this in two ways...
Write Testable Code
DateTimeProviders wraps most of the standard DateTime & DateTimeOffset functionality in new abstractions that have interfaces. By injecting these interfaces as dependencies into your code, you can now mock your use of these dependencies as you see fit.Maintain Consistency
Working on a project where it is important that the developers stick to using UtcNow instead of Now?DateTimeProviders allows you to override the standard Now/UtcNow functionality so that you can do whatever you need. For example you could make the two methods equivalent to each other, log a warning that there is an undesirable method call, or throw an exception.
Basic Usage
- Register DateTimeProvider and DateTimeOffsetProvider as a Singleton using your DI/IoC container
- (Optional) If you need to enforce consistency you can set your DI/IoC containers factory method to use the non-default constructor and pass in your own implementations.
- In your application code, do not call DateTime directly. Instead inject one of the interfaces
IDateTimeProvider
orIDateTimeOffsetProvider
, and call the properties/methods on the instance given to you by your DI/IoC container. - In your unit tests or any other time you need a mock, mock out the IDateTimeProvider/IDateTimeOffsetProvider interfaces and inject your mocks into your subject-under-test.
Enforcing Consistency
If you need to enforce consistent usage of Now vs UtcNow, both of the abstractions support replacing the default implementations by passing your own Func<T>
implementations, where T is either a DateTime or a DateTimeOffset.
Each abstraction provides a default parameterless constructor and a constructor that accepts arguments that allow you to override one or both of Now/UtcNow properties with your own implementation.
If you only want to override one property, you can pass a null
argument to the other and the default implementation will continue to be used.
The signatures of the constructors are
public DateTimeProvider(Func<DateTime> nowFunc = null, Func<DateTime> utcNowFunc = null)
public DateTimeOffsetProvider(Func<DateTimeOffset> nowFunc = null, Func<DateTimeOffset> utcNowFunc = null)
Examples
Overriding Now so that it returns the same value as UtcNow
`var dateTimeProvider = new DateTimeOffsetProvider(()=>DateTime.UtcNow, null);`
Overriding Now so that it throws an exeption
`new DateTimeOffsetProvider(() => throw new NotSupportedException("You should call UtcNow"), null);`
Version History
- 1.0 - Initial Release.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 4,463 | 1/16/2021 |