2

I have a Json object. I want to update its lastUpdate property whenever any of the other properties are set (changed).

How can I fix this without calling the lastUpdate property from the calling function?

public class TickerData
{
    public string symbol { get; set; }
    public string exchange { get; set; }
    public SecurityType securityType { get; set; }
    public string  Currency { get; set; } // convert to enum later

    public strategy  entryStrategy { get; set; }
    public OHLCV ohlcv { get; set; }
    public HistoricalDataEventArgs[] historicalData { get; set; }
    public DateTime date { get; set; }
    public decimal open { get; set; }
    public decimal high { get; set; }
    public decimal low { get; set; }

    public decimal close { get; set; }
    public DateTime lastUpdate { get; set; }
}
Gustavo Mori
  • 8,319
  • 3
  • 38
  • 52
junkone
  • 1,427
  • 1
  • 20
  • 45

4 Answers4

4

One option is to:

  1. Add a tickerChanged event to your class
  2. Give your properties backing fields from/to which you read/write
  3. Invoke tickerChanged() every time a backing field other than lastUpdate is set
  4. Add a single event handler to tickerChanged which updates the lastUpdate property

    public class TickerData
    {
        private event Action tickerChanged;
    
        private string _symbol;
        public string symbol
        {
            get
            {
                return _symbol;
            }
            set
            {
                _symbol = value;
                tickerChanged();
            }
        }
    
        private string _exchange;
        public string exchange
        {
            get
            {
                return _exchange;
            }
            set
            {
                _exchange= value;
                tickerChanged();
            }
        }
    
        // ... continue the pattern ...
    
        public DateTime lastUpdate { get; private set; }
    
        public TickerData()
        {
            // register an event handler that
            // updates the lastUpdate property
            tickerChanged += () => lastUpdate = DateTime.Now;
        }
    }
    
allonhadaya
  • 1,297
  • 7
  • 19
  • Out of interest would you care to comment on why the abstraction of an event to be triggered rather than just calling a method directly to update the lastupdate field or doign it in fact actually directly? Not saying its bad, just that its worth an explanation... – Chris May 29 '14 at 20:37
  • 1
    @Chris, sure thing: I consider the event abstraction more elegant here because it allows the property setters to be concerned with the semantics of setting rather than side-effect propagation. Instead of being responsible for changing the `lastUpdate` property, they're responsible for notifying anyone who cares, "I have changed." – allonhadaya May 29 '14 at 20:47
  • Thanks. That makes a lot of sense. I thought it looked nicer but couldn't put my finger on why which you totally have. +1! ;-) – Chris May 30 '14 at 09:01
1

You can call the Setter of lastUpdate from every other setter of TickerData

quantdev
  • 23,517
  • 5
  • 55
  • 88
1

It sounds like you could more easily accomplish this with change tracking. I'm not sure how pure you're trying to keep your object, but you could implement INotifyPropertyChanged maybe even with the new Caller Information Attributes in .NET 4.5 - essentially to monitor for changes to properties and update lastUpdate appropriately. Read more here.

Community
  • 1
  • 1
kman
  • 2,184
  • 3
  • 23
  • 42
0

I know a solution to this, but it's really ugly...

You have to replace all the auto-properties with normal properties backed with fields and set the lastUpdate in the set block. Just make sure to leave lastUpdate as it is.

private string symbol;
public string Symbol
{
    get { return symbol; }
    set
    {
         if(symbol == value) return; //this is optional

         symbol = value;
         lastUpdate = DateTime.Now;
    }
}
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44