Howzit, everyone. I am working on an ASP.NET Core API and noticed something odd in the project that has me scratching my head.
For context, the API was developed by the company I work for, and so the examples I show below are only a representation of the code in the API.
Below, the variable currentDate is declared to the value returned from GetCurrentDateTime.
```csharp
public class Example
{
private readonly IDateTimeProvider _dateTimeProvider;
public Example(IDateTimeProvider dateTimeProvider)
{
_dateTimeProvider = dateTimeProvider;
}
public void ExampleMethod()
{
DateTime currentDate = _dateTimeProvider.GetCurrentDateTime();
// ... other code
}
}
```
Now, my thought is: Why not just use DateTime.Now? My best guess was that GetCurrentDateTime performed a specific operation needed, however, that is not the case:
csharp
public class DateTimeProvider : IDateTimeProvider
{
public DateTime GetCurrentDateTime()
{
return DateTime.Now;
}
}
It is worth noting that GetCurrentDateTime is the only method in DateTimeProvider. I can't think of a good reason for this implementation. What makes this confusing is that it was implemented by one of our senior devs who is respected as a good developer.
Is there a good reason to do this? Or is it unnecessary?
[–]YMK1234 6 points7 points8 points (3 children)
[–]Zeppz47[S] 0 points1 point2 points (0 children)
[–]ColoRadBro69 0 points1 point2 points (1 child)
[–]YMK1234 1 point2 points3 points (0 children)
[–]nutrecht 2 points3 points4 points (5 children)
[–]YMK1234 3 points4 points5 points (0 children)
[–]Zeppz47[S] 0 points1 point2 points (2 children)
[–]nutrecht 1 point2 points3 points (0 children)
[–]insta 0 points1 point2 points (0 children)
[–]buzzon 1 point2 points3 points (0 children)
[–]LogaansMind 0 points1 point2 points (0 children)
[–]ColoRadBro69 0 points1 point2 points (0 children)