I am in the process of learning WPF and CompositeWPF concepts - I havn't yet studied the CompositeWPF event system but reviewed enough of it to have a preliminary judgement that the Unity simple Event Broker is more loosely coupled (and easier to use); we'll see....
I followed the CompositeWPF documentation to create a new solution. The only place that I vary is that my ShellApp does not have a BootStrapper, I moved it into the ShellApp.Infrastrustructure (which will be shared with other solutions).
The App.xaml.cs is modified as follows:
public partial class App : Application
{
/// <summary>
/// Load application
/// </summary>
public App()
{
// Bootstrapper is in the Shell.Infrastructure
// project which can be shared by multiple solutions.
BootStrapper bootStrapper = new BootStrapper(CreateShell, GetModuleEnumerator);
bootStrapper.Run();
}
protected DependencyObject CreateShell(IUnityContainer container)
{
// Launch our shell
Shell shell = container.Resolve<Shell>();
shell.Show();
return shell;
}
protected IModuleEnumerator GetModuleEnumerator()
{
// Return modules to load
return new StaticModuleEnumerator()
.AddModule(typeof(ProcessSolution));
}
}
The revised (shared) BootStrapper looks as follows - note that my global bootstrapper adds SimpleEventBrokerExtension in CreateShell():
public class BootStrapper : UnityBootstrapper
{
public delegate DependencyObject ShellCreationDelegate(IUnityContainer container);
public delegate IModuleEnumerator GetModuleEnumeratorDelegate();
private ShellCreationDelegate _createShell;
private GetModuleEnumeratorDelegate _getModules;
/// <summary>
/// We want to decouple our bootstrapper from the calling
/// application so we can reuse it.
/// </summary>
/// <param name="createShell"></param>
/// <param name="getModules"></param>
public BootStrapper(ShellCreationDelegate createShell,
GetModuleEnumeratorDelegate getModules)
{
_createShell = createShell;
_getModules = getModules;
}
protected override DependencyObject CreateShell()
{
Container.AddNewExtension<SimpleEventBrokerExtension>();
return _createShell(Container);
}
protected override IModuleEnumerator GetModuleEnumerator()
{
return _getModules();
}
}
With these changes I can now implement a routed event handler in my ProcessSolutionView.xaml.cs that will publish the event as follows:
public partial class ProcessSolutionView : UserControl, IProcessSolutionView
{
public ProcessSolutionViewPresenter Presenter { get; set; }
[Publishes("UserControl_Click")]
public event EventHandler OnUserControlClicked;
/// <summary>
/// Handles routed click events
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UserControl_Click(object sender, RoutedEventArgs e)
{
if (OnUserControlClicked != null)
OnUserControlClicked(sender, e);
}
/// <summary>
/// Constructor
/// </summary>
public ProcessSolutionView()
{
InitializeComponent();
}
....
}
Now all that remains is to subscribe to the published event - the ProcessSolutionViewPresenter subscribes below:
/// <summary>
/// Routed events are used to process control clicks -
/// we'll let the RoutedArgFactory handle the processing
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[SubscribesTo("UserControl_Click")]
public void UserControl_Click(object sender, EventArgs e)
{
FactoryEventArgs args = new FactoryEventArgs(e as RoutedEventArgs);
ICommand cmd = RoutedArgFactory.GetConcrete(args);
cmd.Execute();
View.SetStatusBar(args.Message, StatusMessageType.Message);
}
Below you'll find the source code for the demo:

Click image for flash demo
Source Code: ShellApp.zip (1.40 mb)
I'm building a framework, based on the CompositeWPF (as outlined in the documentation) as well as principals learned while working with the Smart Client Software Factory. In the attached demo you'll find that there are actually two applications, both sharing the same Shell.Infrastructure. One is for a Visio solution/project conversion utility so that I can easily reverse enginneer VS2008 solutions/projects (using VS2005 and Visio for Architects software) and the other (NSApp) which will be used to setup and configure a SQL Server Notification Service - a somewhat daunting task. The applications up to this point are simply to plug in and demonstrate how SCSF event processing can be done in the CompositeWPF (reference ShellApp solution).
Tags:
compositewpf,
unity,
eventbroker
Categories:
Unity