Question

I'm looking for a "best practice" (if there is even a best) for cleanly shutting an WPF application down that uses MEF and PRISM4. Ultimately, I'm looking for some sort of "Close Service" would manage all of event/commanding from the Shell to any viewmodel (or other subscribers) that want to verify it's OK to close the application. Is the event aggregator the cleanest way? Other opinions/options?

Ideally, I'd have a button on my ToolbarView in my ToolbarRegion (1 of 2 regions in my Shell). This button would invoke a command on my ToolbarViewModel (referencing a command on my ToolbarControler) which in turn would do 2 things (I think?)...First, notify all subscribers that it's closing time and allow any of them to cancel the close and...secondly, if none cancel, somehow notify the shell to close. My app's shutdown mode is set to "ShutdownMode.OnMainWindowClose" so if the Shell closes, I should be all set.

Can someone help me out with this?

Was it helpful?

Solution

I'm developping a large application using the same stuff: MEF and PrismV4

I handle shutdown it a bit differently:
in the Shell, there is a "Tools" Region, shutdown for that is handled right in the Shell, on the close event.
Then for everything injected in the other region, which is a tabcontrol, I cast the content as IDisposable, and close every tab one by one. (Actually, it's not a tabcontrol, it's an avalondock component, but it's the same thing really).
Of course, you'll have to implement IDisposable on every class that has references etc to dispose, but it's hard to think of a "clean way of shutting down" without meantioning that interface right? =)

Now, about the EventAggregator: you may very well run into trouble, because there is no coupling: you can fire a weak event through it, but you can't wait for objects to do their work after that.
And then, you couldn't make a mechanism to cancel the shutdown.

In case you want your various views to be able to cancel the shutdown, i suggest you create an interface with a single method in it:

public interface IShutdownAware
{
   bool CanShutdown();
}

Then right before you call dispose, call CanShutdown(); if they all return true, proceed with disposing, otherwise, cancel the shutdown process.

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