-1

In our program we have so called WallclockDateTime. Because we can shift in time, this WallclockDateTime is not related to the current time. But now I need this WallclockDateTime everywhere in my classes.

First I decided to pass this WallclockDateTime by reference from constructor to constructor. But that takes a lot of maintenance. And now I have some class Foo which is created in masses in a Collection<Foo>, and passing this same WallClockTime over and over again through the constructor seemed a bit useless to me. So I thought: Can't this be done more efficient?

First I tried to make the central WallclockDateTime static. EDIT: But I'm not allowed to use OnPropertyChanged() inside a static property and it is from outside my solution. If I make _wallclockDateTime public and static I can get to it everywhere! But it seems dirty to me. So what is a good solution for this? Passing from constructor to constructor? Or make the field public and static? Or some other clever solution?

    private DateTime _wallclockDateTime;
    public DateTime WallclockDateTime
    {
        get
        {
            return _wallclockDateTime;
        }
        set
        {
            if (_wallclockDateTime != value)
            {
                _wallclockDateTime = value;
                OnPropertyChanged(nameof(WallclockDateTime));
                OnPropertyChanged(nameof(CurrentSliderStateLabel));
                OnPropertyChanged(nameof(WallclockDateTimeTicks));
            }
        }
    }

// This class is used in a Collection<Foo>
public class Foo
{
    private static DateTime _wallClockTime;

    public Foo(ref DateTime wallClockTime)
    {
        _wallClockTime = wallClockTime;
    }

    public void Bar()
    {
        // Do something with the _wallClockTime
    }
}
ffonz
  • 1,304
  • 1
  • 11
  • 29
  • could you not just define _wallClockTime in a base class? – Sean T Nov 28 '18 at 14:41
  • @Rango I do know static. I need ONE instance of a WallClockDateTime, but I need to see it everywhere. – ffonz Nov 28 '18 at 14:43
  • @ffonz So you want a `public static` field... You *could* use a Singleton, but that's basically as dirty as a `public static` field – Camilo Terevinto Nov 28 '18 at 14:44
  • @CamiloTerevinto If I don't use `ref` I get all different times, since DateTime is a primitive type and not an object. – ffonz Nov 28 '18 at 14:45
  • @ffonz: you can make the property `public static`, the backing field should not be public. I'd wrap the setter in a `lock`-statement – Tim Schmelter Nov 28 '18 at 14:46
  • @Rango But I'm not allowed to use `OnNotifyPropertyChanged()` inside a static property. – ffonz Nov 28 '18 at 14:48
  • 2
    You may want to read this: https://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern – Camilo Terevinto Nov 28 '18 at 14:49
  • @CamiloTerevinto my thoughts exactly. Singleton seems to be the most reasonable choice here. – Zohar Peled Nov 28 '18 at 14:54
  • 1
    @ffonz: So you can't make the property-changed event `static` because it's not part of your solution? Otherwise you can use: `public static event PropertyChangedEventHandler WallclockDateTimeChanged; private static void OnWallclockDateTimeChanged(string name = null) { WallclockDateTimeChanged?.Invoke(null, new PropertyChangedEventArgs(name)); }` – Tim Schmelter Nov 28 '18 at 14:55

1 Answers1

1

The usual solution is to use Dependency Injection. You create a service provider and a service that supports all of your date needs.

Microsoft has a dependency injection system, Microsoft.Extensions.DependencyInjection.

You can either have the service put into the constructors of other services, or else just access it directly.

There is a view that says that dependency injection is just a fancy way to have global variables. On the other hand, when used with interface definitions for each service in a large system it probably does improve the ability to test components.

One other comment, in most cases DateTimeOffset is better than DateTime, because it eliminates the ambiguity of what time zone the date/time is relative to.

sjb-sjb
  • 1,112
  • 6
  • 14