1

There are three parties in my design:

  • MSFT orderbook
  • NASDAQ index (need to be recalculated when MSFT orderbook changes)
  • MSFT strategy (need to be recalculated when MSFT orderbook or NASDAQ index changes)

So I attach both NASDAQ index and MSFT strategy to MSFT orderbook like that: msftOrderBook.OrderbookUpdated += orderbookUpdated;

I also attach MSFT strategy to listen for NASDAQ index changes the same way.

I have following problems:

  • Order is important. As I want NASDAQ index to be recalculated before recalculating strategy I should attach NASDAQ index to orderBook before attaching strategy. This is potential bug as I don't have easy mechanism to "guarantee" that I attach in right order...
  • When MSFT orderbook is updated, it notifies NASDAQ index and both NASDAQ index and MSFT orderbook notifies MSFT strategy about update. So MSFT strategy receives double update but I don't need that. I want only one update because it's expected that when MSFT orderbook changes NASDAQ index will be changed too.

Questions:

  • Is it good to rely on the order of EventHandler calls? and if it is not good what would be general solution to my problem?
  • Regarding second point I just want to ignore in MSFT strategy MSFT orderBook update because strategy knows that every time orderBook is updated NASDAQ index updated too and so it would be enough to react only on NASDAQ index update. But probably you can suggest something completely different and better..
David Hall
  • 32,624
  • 10
  • 90
  • 127
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • Events in .net are synchronous (http://stackoverflow.com/questions/7106454/are-c-sharp-events-synchronous) so you can rely on the order. This is the order the events are raised though - it has nothing to do with the order in which you attach the handler. Though you can rely on the order, I'd suggest your consumer should ensure the order of things rather than relying on the provider. Business logic imo should not rely on implementation logic of external parties - even the .net framework! If the third party logic changes you could be in a world of hurt. – David Hall Jul 01 '12 at 08:58
  • @DavidHall so what should I do when I 1. use events to notify consumers about some change. 2. I need consumers to be notified in certain order? Should I introduce different events for that which called one after each other? – Oleg Vazhnev Jul 01 '12 at 09:13
  • I took the question as being you are the consumer or the events - if you are raising the events then the order of this is entirely under your control. For things which absolutely must be in order though I'd consider an architectural approach such as introducing a FIFO queue. – David Hall Jul 01 '12 at 09:16
  • I am however, just offering ideas here! Which is why no answer - there are people on SO who will have far better things to offer than I do so if I were you I would wait to see what they have to say :) – David Hall Jul 01 '12 at 09:17

1 Answers1

1

It seems clear:

  • MSFT orderbook
  • NASDAQ index (need to be recalculated when MSFT orderbook changes)
  • MSFT strategy (need to be recalculated when MSFT orderbook or NASDAQ index changes)

together with

  • As I want NASDAQ index to be recalculated before recalculating strategy

Means the MSFT-strategy depends on the NASDAQ-index and only indirectly on MSFT-orderbook.
So this asks for a Changed event on NASDAQ-index that the strategy can subscribe to.

I would favor an approach w/o events though but it's not clear if that's feasible.


response to comment:

class BusinessLayer
{
    public void Update(Type data)
    {
        OrderBook.Update(data);
        NasdaqIndex.Update(OrderBook);
        NasdaqStrategy.Update(NasdaqIndex);
    }
}
H H
  • 263,252
  • 30
  • 330
  • 514