Troubles with EventBroker

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

I'm having trouble with the EventBroker. I'm 100% positive it's user error :0)

I have 3 classes;
- TestManager
- TestService - basically wraps TestManager
- TestingViewPresenter

Here are the classes:
public class TestManager
{
public event EventHandler SomethingHappened;

public void DoSomething()
{
if(SomethingHappened != null)
{
SomethingHappened(this, EventArgs.Empty);
}
}
}


Service
public class TestService
{
private TestManager _testManager = null;


public TestService()
{
_testManager = new TestManager();
_testManager.SomethingHappened += TestEvent;
}

public void DoSomething()
{
_testManager.DoSomething();
}

EventPublication("TestEvent", PublicationScope.Global)
public event EventHandler TestEvent;
}

public class TestingViewPresenter : PresenterBase<ITestingView>
{
private TestService _testService = null;

InjectionConstructor
public TestingViewPresenter
(
ServiceDependencyTestService testService
)
{
_testService = testService;
}

public void TestButtonClicked()
{
_testService.DoSomething();
}

EventSubscription("TestEvent")
public void OnTestEvent(object sender, EventArgs e)
{
MessageBoxSvc.Show("Test Event Fired");
}
// end of classes

Oh, and in the ModuleController class I add TestService to the WorkItem.

So, the problem is when a user clicks the button, the event calls _testService.DoSomething(); which calls _testManager.DoSomething() which in turns tries to fire the SomethingHappened event. SomethingHappened is always null.

I'm not sure what I'm missing here, but I don't know what else I need to do to get this to work. There are some similar examples in the Appraiser's Workbench, but they seem to work. :0)

From what I understand, the ObjectBuilder (or EventBroker, not sure which one) add all EventSubscription marked methods to the matching EventPublication event, so if an event is null, that means that no handlers have been added by the builder? Is that correct?

Maybe I need to add something additional to my TestService class to make the ObjectBuilder handle it better?

Thanks for any help!
Apr 24, 2006 at 5:33 PM
originally posted by: RolandLi

I've found your problem.

You put the "_testManager.SomethingHappened += TestEvent;" in the constructor , but the TestEvent is still Null here,you in fact added nothing to the SomethingHappened event.

You need the instance of the TestEvent , but it only get instanced after the constructor is invoked.
(The ObjectBuilder calls the constructor first ,and deals with the EventPublication after that)

You should get this line of code "_testManager.SomethingHappened += TestEvent;" out of the constructor and put it into another method which will be invoked after the construction.
Apr 25, 2006 at 11:48 AM
originally posted by: sklett

Hi Roland,

Thanks for the post, you did indeed find the problem. I moved the event wiring into the IBuilderAware.OnBuiltUp method and I'm up and running. Thank you for your time!

-Steve