CommandHandler called 8 times

Topics: CAB & Smart Client Software Factory
Nov 15, 2006 at 6:58 AM
originally posted by: pcfountain

I have a CommandHandler that is being called 8 times in a row, when I select the menu item associated with that Command. I have seen this behavior before with events - the event gets fired 8 times because I have 8 CAB modules and they all get a copy of the event. If you are subscibing to an event in the Infrastructure.Module, you will receive all 8 copies of the command/event, UNLESS you set the PublicationScope to WorkItem. However, I cannot find a way to set the PublicationScope for a command. Is this possible?
Nov 15, 2006 at 11:07 AM
originally posted by: HeinA

I have a simular problem.

Same View with 2 different WorkItems open in 2 different WindowWorkspaces. The View has a ToolStrip UIExtension site & when you add an invoker to the button & click it, the command handler gets called once for each workitem???
Nov 16, 2006 at 11:47 AM
originally posted by: techtickler

If PublicationScope set to WorkItem, only the local WorkItem can get the event. Not sure your scenoria fit to this scope.
An eventhandler gets fired multiple times is normally because there are multiple object which has the eventhandler and subscribe to the same event. So, to be accurate, it is not the same eventhandler to be called multiple times, it is just each object's eventHandler called one time when listening to an event. This is the way Event Broker works. All subscriber will be called when an event raised.
Just my two cents. Try to avoid using EventBroker when you don't want to see multiple calls happened. What I mean is that the purpose of using EventBroker is to decouple the modules. If the module or WorkItems are closely coupled, try not to use the event to indirectively interact each other. Use direct invoke for the case.

Just my two cents. Happy coding.
Mar 28, 2007 at 6:33 PM
I have a similar problem but related to an event. I have an audit window so to loosely couple it from my domain, I've made it respond to an event "AuditLog" which gets published by an observer object attached to my domain entities. Problem is that it gets called once for each pass when the system invokes it.

So for example the sequence is:
1. User clicks button in view
2. Presenter creates a domain service entity and attaches an injected observer class (which is a service dependency)
3. service calls a method called audit() which invokes the observer
4. Observer publishes an event
5. Event is subscribed to by audit window which displays a message

The problem is that when I fire it the first time, 1 message is displayed. When I invoke it a second time, it's called twice and so adds two messages to the window (total = 3), when I invoke it a 3rd time 6 messages are displayed. And that's when one message is written to the log but imagine how big this gets when I write 3 or 4 unique messages.

I know it's something to do with the publication as the subscriber is being called once for each publisher, but it's like the publish event keeps getting added each time. I need to have the publication scope set to global in order for the audit module to recieve the event. Any ideas on what I can do to not have the same message sent multiple times? I can put together a spike project if someone has time to look at it.