Catel 2.0: Auditing in MVVM

by Geert 21. June 2011 16:16

There are lots of lightweight MVVM frameworks out there, which work great for the basics. However, if you are writing larger enterprise applications, notifying the UI of changed properties isn't enough. For example, did you think about Command Authentication? Or what about sensor emulation for Windows Phone 7 (that Microsoft don’t provide)?

Catel is a very advanced MVVM framework that provides much more than just a base class that sends some property changed events to the view. It takes care of communication, data handling, validation, nested user controls and much more. Catel is currently heading to a 2.0 release and this blog will leak some new features during the sprint to the new release.

Another very important feature of larger (and I think smaller as well) applications is auditing. Now you think: what the #$%@, why audit MVVM? Well, there are plenty of reasons for this:

  • Measure performance
  • Logging (what user did what on specific moments)
  • Gather statistics (which views (view models) are used most)
  • See what features of your software are being used by checking if anyone is actually invoking specific commands
  • Measure performance (how long does it take to update specific properties or why is a specific view-model so slow?)

The latest feature of Catel is auditing. With auditing, you can create and register custom auditors that can handled changes and events of view models. This way, you can gather a lot of statistics or any information that you want to gather about the user experience. Below is a list of events that can be handled:

  • OnViewModelCreating
  • OnViewModelCreated
  • OnPropertyChanging
  • OnPropertyChanged
  • OnCommandExecuting
  • OnViewModelSaving
  • OnViewModelSaved
  • OnViewModelCanceling
  • OnViewModelCanceled
  • OnViewModelClosing
  • OnViewModelClosed

The developer has all the freedom to handle one or more methods in an auditor. Of course multiple auditors are possible as well. Let’s take a look how easy it is to add auditing to an MVVM application.

Creating an auditor

Creating a new auditor is very simple. Create a new class, derive from AuditorBase and override the methods you are interested in. The class example tracks the event to a fake analytics framework.

   1: /// <summary>
   2: /// Logs all commands to a custom analytics service.
   3: /// </summary>
   4: public class CommandAuditor : AuditorBase
   5: {
   6:     private Analytics _analytics = new Analytics();
   7:  
   8:     /// <summary>
   9:     /// Called when a command of a view model has just been executed.
  10:     /// </summary>
  11:     /// <param name="viewModel">The view model.</param>
  12:     /// <param name="commandName">Name of the command, which is the name of the command property.</param>
  13:     /// <param name="command">The command that has been executed.</param>
  14:     /// <param name="commandParameter">The command parameter.</param>
  15:     public override void OnCommandExecuted(IViewModel viewModel, string commandName, ICatelCommand command, object commandParameter)
  16:     {
  17:         _analytics.TrackEvent(viewModel.GetType(), "commandName");
  18:     }
  19: }

Registering an auditor

Registering a new auditor is extremely easy as you can see in the code below:

   1: AuditingManager.RegisterAuditor(new CommandAuditor());

Is it really that easy? Yes, it is…

Tags: , ,

C# | Catel | MVVM

Pingbacks and trackbacks (1)+

Comments are closed

About the Author

Geert van Horrik is an independent freelance software developer since January 1st, 2007. Since then he was been working on several projects from C++ to C# (WPF, Silverlight, ASP.NET, etc). Currently he loves to write his software using WPF (or Silverlight if WPF isn't an option).

Lately, Geert is spending a lot of time on Catel, a free open-source MVVM Framework for WPF and Silverlight. Actually, it's more than "just" an MVVM Framework, it's a complete application library!