tags:

views:

205

answers:

4

Suppose I have a Customer object that can enter multiple Stores and shop simultaneously. When a customer enters a Store, the Store will begin to handle the Customer's triggered events. To purchase an item, the Customer currently triggers a PurchaseItem event with the particular item specified in the EventArgs.

Currently, since the Customer can be in multiple stores at the same time, whenever he triggers the PurchaseItem event, all of the Stores he is in are notified. Is there any way using events that when the Customer triggers the PurchaseItem event, only the Store that houses that item is notified?

+1  A: 

This sounds like an extremely odd way of modelling things. Purchasing should be an action between the store and the customer - a direct method call, probably from Customer to Store. That could fire an event, but I wouldn't use an event to do the initial purchase. Maybe it makes more sense in your real context... but it's hard to say without knowing more details.

Jon Skeet
+1  A: 

What if you make the item aware of the shop that owns it? Then you could make a call to item.Purchase(this) with this as the customer and the item would fire the shop event.

Diadistis
A: 

I think Commands are the preferred way to handle broadly targeted events. This saves you the trouble of correctly wiring everything. Your customer could issue a purchase Command and stores could consume it.

dmo
A: 

give your events a 'channel' identifier that corresponds to the appropriate store, and code stores to ignore events not on their 'channel'

but jon skeet is right (jon skeet is always right, or at least 99.999% rightness) this is a poor design - perhaps with more details we can help you fix it

Steven A. Lowe