Problem with events published by service

Topics: CAB & Smart Client Software Factory
Apr 20, 2006 at 11:20 AM
originally posted by: _Umka

I have problem with subscription to events published by service.
Service fire events correct I tested it by subscribe to event directly in unit test. Signature events delegates and methods in controller are equal. I inspected RootWorkItem.Commands and found that all Microsoft.Practices.CompositeUI.EventBroker.EventTopic with my published events names inside. Then I inspected events handlers before fire and found thet _invocationCount is zerro.

Why is it happen?


I have written service with several events inside

public class ToolBoxService : IToolBoxService
{

Dictionary<string, ToolBoxGroup> groups = new Dictionary<string, ToolBoxGroup>();

EventPublication(AnommaCommon.ToolBoxConstants.TOOLBOX_ADD_GROUP)
public event EventHandler AddGroupHandler;
EventPublication(AnommaCommon.ToolBoxConstants.TOOLBOX_ADD_ITEM)
public event EventHandler AddItemHandler;
EventPublication(AnommaCommon.ToolBoxConstants.TOOLBOX_REMOVE_GROUP)
public event EventHandler RemoveGroupHandler;
EventPublication(AnommaCommon.ToolBoxConstants.TOOLBOX_REMOVE_ITEM)
public event EventHandler RemoveItemHandler;
EventPublication(AnommaCommon.ToolBoxConstants.TOOLBOX_FIRE_ITEM)
public event EventHandler FireItemHandler;
...........

and also have written controller

public class ToolBoxController : Controller
{

EventSubscription(AnommaCommon.ToolBoxConstants.TOOLBOX_ADD_GROUP)
private void OnAddGroup(object sender, EventHandler e)
{
}

EventSubscription(AnommaCommon.ToolBoxConstants.TOOLBOX_ADD_ITEM)
private void OnAddItem(object sender, EventHandler e)
{
}

EventSubscription(AnommaCommon.ToolBoxConstants.TOOLBOX_REMOVE_GROUP)
private void OnRemoveGroup(object sender, EventHandler e)
{
}

EventSubscription(AnommaCommon.ToolBoxConstants.TOOLBOX_REMOVE_ITEM)
private void OnRemoveItem(object sender, EventHandler e)
{
}
....
Apr 20, 2006 at 11:22 AM
originally posted by: _Umka

Addon:

Service class marked by Service(typeof(AnommaCommon.IToolBoxService))

and I can get it from controller by

ServiceDependency
public IToolBoxService ToolBoxService
{
set { service = value; }
}
Apr 20, 2006 at 12:16 PM
originally posted by: atome

Hello, I've some questions :

Is your controller (or the view which owns it) added to a workItem ?

What are the scopes of your events ?

Why getting a service reference in your controller if you subscribe to its events in the same place ?
Apr 20, 2006 at 12:38 PM
originally posted by: _Umka

My controller added to custom control that added in workItem

ToolBoxView view = this.Items.Get<ToolBoxView>("ToolBox");
...
sideBarWorkspace.Show(view, spInfo);
this.Activate();

public partial class ToolBoxView : UserControl
{
private ToolBoxController controller;

CreateNew
public ToolBoxController Controller
{
set
{
controller = value;
controller.ExplorerBar = this.explorerBar;
}
}
}

Controller should get events from service about addition or removing elements to mange ExplorerBar accordingly these changes. I did not found any other way to get singleton object that can be easy accessible from each workItem/Control/Controller than create service. Service just a storage of group of items. It storage created first and I can guaranty that any workItem can publish groups and items before toolbox control created and shown.

I placed events subscription to WorkItem but nothing happened. :(

I though that my toolbox created and shown before service but it wasn't true - service created first.
Apr 20, 2006 at 12:50 PM
originally posted by: _Umka

correct previous example

ToolBoxView view = this.Items.Get<ToolBoxView>("ToolBox");

if (view == null)
{
view = this.Items.AddNew<ToolBoxView>("ToolBox");
}

if (sideBarWorkspace.SmartParts.Contains(view) == false)
{
sideBarWorkspace.Show(view, new UltraDockSmartPartInfo());
this.Activate();
}
else
{
sideBarWorkspace.Activate(view);
this.Activate();
}
Apr 20, 2006 at 7:36 PM
originally posted by: atome

Hi Umka,

Have you tried to use this prototype when subscribing to the events ?

- public instead of private
- EventArgs instead of EventHandler

e.g. :

EventSubscription(AnommaCommon.ToolBoxConstants.TOOLBOX_ADD_GROUP)public void OnAddGroup(object sender, EventArgs e)
{
}
Apr 20, 2006 at 7:45 PM
originally posted by: _Umka

Yes! You right!

So silly mistake...