Question

When developping rather large applications using Prism and MEF/Unity I always reach a point where I should choose between using events, a service or maybe both. And I cannot decide what's most usable. Maybe something is wrong with my architecture (as in this decision shouldn't have to be made in the first place) but I don't see what.

Here's a typical example: my application has a main window and a lot of slave windows that are created on demand by modules or user actions. The application decides what a slave window's chrome looks and behaves like, remembers window placement etc while the content itself is created somewhere in the modules. There are also a lot of user actions resulting in hiding/showing/bringing to front of windows. To achieve all this, I currently have a WindowManager service that listens to CreateWindow/SetWindowState/.. events.

This has benefits:

  • classes using this only know about IEventAggregator (which they already use most of the time anyway for other events) and the events consumed by WindowManager, not WindowManager itself
  • classes like ViewModels don't deal with windows directly. Instead they refer to them by their title or id and small event classes that encapsulate just what's needed.
  • no need for a seperate IWindowManager interface just for the purpose of mocking it in a test

And withdrawals:

  • WindowManager could be used perfectly standalone, but now it needs to subscribe for events. Or probably better, some other class has to take care of that.
  • extending it to show a modal dialog is somewhat tricky: if a VM fires an event to show a dialog, it's of utter importance that the Publish call only returns after the dialog was closed
  • WindowManager is available as a service and it's in the CompositionContainer, why not use it as such anyway?

Using the service directly just shifts benefits/withdrawals around and there doesn't seem to be a clear winner.

Question: what would you use as guidance rules to pick one or the other, or would you rather always pick just one, or both? Is there something particularly wrong in my application design that I have to make this decision?

Was it helpful?

Solution

Events and services are used for different things. You don't have to choose between them, you can use them for different purposes. You would typically use event to notify listeners that something has happened. Example: users changes the font size in the application settings. You would send event to all listeners (e.g. viewmodels) so that the views update. Usually event is kind of thing for which you don't get a response (although you could attach e.g. callback function/action that the event listener would call).

What if your viewmodel needs to open new window? Usually the viewmodel shouldn't care how this new window is opened or whether it is modal or not. In this case it would be easy to use a service:

windowManager.ShowDetailsView();

The WindowManager (which you use through IWindowManager interface) is responsible for displaying the details view. Maybe it is a modal view or maybe there is some kind of slide animation. The point is that the viewmodel that uses IWindowManager doesn't care.

In some cases you might need to receive notification if users clicks Ok or Cancel. You can still use the IWindowManager by having method like this:

public void ShowEditView(Action userSavedChanged, Action userCancelled);

Then just call it from the viewmodel

windowManager.ShowEditView(this.SaveChanges, this.CancelChanges);

// in your viewmodel you have the SaveChanges and CancelChanges methods
private void SaveChanges()
{
   // save the changes.
}

Hopefully this all makes some sense. After all it is friday :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top