jasontaylordev / NorthwindTraders

Northwind Traders is a sample application built using ASP.NET Core and Entity Framework Core.
MIT License
5k stars 1.59k forks source link

What's the point of having the IDateTime interface? #270

Closed mboukhlouf closed 4 years ago

mboukhlouf commented 4 years ago

What's the point of having the IDateTime interface? The implementation MachineDateTime just calls DateTime.Now for the Now property.

AndreiTsvettsikh commented 4 years ago

You need it for Unit tests. Sample: When you create an object you want to fill CreatedAt by the DateTime.UtcNow The current DateTime is a kind of dependency on the environment. How to write tests to CreatedAt? The easiest way - create an IDateTime interface and mock it in tests. And one more benefit - dependency on the IDateTime is explicit. You can see that this class depends on environment.

mboukhlouf commented 4 years ago

You need it for Unit tests. Sample: When you create an object you want to fill CreatedAt by the DateTime.UtcNow The current DateTime is a kind of dependency on the environment. How to write tests to CreatedAt? The easiest way - create an IDateTime interface and mock it in tests. And one more benefit - dependency on the IDateTime is explicit. You can see that this class depends on environment.

Thank you so much.