MVVM + Broker + Castle Interceptor = FUN

Scenario: have a WPF application based on custom MVVM + Broker + Castle, the users told us that some operations took long time to accomplish and they want a wait cursor on the application.

Resolution: Since all long operations are triggered with event handled by a Broker system, the obvious solution is an interceptor

   1: public class WaitCursorInterceptor : IInterceptor

   2: {

   3:     private IWpfSystem _wpfSystem;

   4:  

   5:     public WaitCursorInterceptor(IWpfSystem wpfSystem)

   6:     {

   7:         _wpfSystem = wpfSystem;

   8:     }

   9:  

  10:     public void Intercept(IInvocation invocation)

  11:     {

  12:         using (_wpfSystem.SetWaitCursor())

  13:         {

  14:             invocation.Proceed();

  15:         }

  16:     }

  17: }

This depends on the IWpfSystem interface that has this function to change the cursor of the application

   1: public DisposableAction SetWaitCursor()

   2: {

   3:     Mouse.OverrideCursor = Cursors.Wait;

   4:     return new DisposableAction(() => Mouse.OverrideCursor = null);

   5: }

Now you only need to wire up everything adding the interceptor on the IBroker interface.

   1: ServiceLocator.FluentRegistration(

   2:      Component.For<WaitCursorInterceptor>());

   3:  

   4: ServiceLocator.FluentRegistration(

   5:    Component.For<IWpfSystem>()

   6:      .ImplementedBy<WpfSystem>());

   7:  

   8: ServiceLocator.FluentRegistration(

   9:    Component.For<IBroker>()

  10:     .ImplementedBy<Broker>()

  11:     .Interceptors(new InterceptorReference(typeof(WaitCursorInterceptor))).Anywhere);

Now every call to broker function pass through the interceptor that automatically set the cursor to wait. Then users told us that some views take a long time to open and they want the cursor to appear when a new view waits to appear.

Since all view are created/managed by the navigator, you only need to write this.

   1: ServiceLocator.FluentRegistration(

   2:       Component.For<INavigator>()

   3:         .ImplementedBy<MainNavigator>()

   4:         .Interceptors(new InterceptorReference(typeof(WaitCursorInterceptor))).Anywhere);

Add the same interceptor on the INavigator interface, and the game is done.

alk.

Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

One thought on “MVVM + Broker + Castle Interceptor = FUN”

Comments are closed.