Model Driven Development with StatePatternModeler

I am proud to announce two Visual Studio 2010 Extensions that are supposed to make development easier and more efficient. Yesterday I also managed to get my German website up to date, so check it out at www.neokc.de. This is also my first blog entry since Windows Live Spaces, which I liked very much, got closed down.

Logging changes with MethodLog

In my current position there is not much time in my life that I can spend on programming and related stuff, so this is all happening a little bit delayed. I published my first ever Visual Studio Extension in early September but could not write about it until now. I named it MethodLog and what it does is quite shortly to describe. Every change that the user makes to a method, is logged as a comment on that method, containing a running number and a current date and time.

methodlog

The image above shows how this looks in Visual Studio. You can download this extension on my German website at www.neokc.de/programme/vsix/methodlog.htm or directly at the Visual Studio Gallery at http://visualstudiogallery.msdn.microsoft.com/en-US/b86ec350-26ad-4323-a297-9ffc38038053.

Modeling states and generating code with StatePatternModeler

The other extension that I am announcing is called StatePatternModeler. The caption of this blog post is referring to this one, since it makes Model Driven Development (MDD) easy in Visual Studio. It does not provide for an entire solution to MDD but does solve the development of stateful applications with models. In my previous blog post I was already describing how this could be accomplished with a custom UML Profile and a T4 template file that I created. If you have not read that article, go ahead and do so now, if you are interested in the raw mechanics.

The StatePatternModeler extension just wraps the UML Profile and the T4 template into a Visual Studio 2010 Extension. It provides a Project Template for the UML State Model and an Item Template for the T4 template that generates the code and a derived behavior class to apply custom logic to the state machine.

spm-s1

These templates make it easy to work with the UML Profile and the code generation and therefore allow an easy way to work with state machines in C#.

spm-s2

The images above show how this looks in Visual Studio. You can download this extension on my German website at www.neokc.de/programme/vsix/statepatternmodeler.htm or directly at the Visual Studio Gallery at http://visualstudiogallery.msdn.microsoft.com/en-US/c7bcfe24-3b1e-4eb8-8956-48d61d58fa56. StatePatternModeler only works with Visual Studio 2010 Ultimate, since it is relying on the UML Modeling Tools for the State Model Project Template.

A type-safe ViewModel base class

The last thing that mattered to me today and that I want to write about is how you can create a type-safe base class for your ViewModel in WPF or Sivlerlight. For the projects that I was doing so far, I did not need to use the MVVMlight framework. Though, I needed a base class for my ViewModels that does not use strings in order to notify property changes. Having strings can be the source of much frustration since they don’t survive refactoring. The approach that I am using allows you to have a ViewModel like below.

   1: using System.Windows.Input;

   2:

   3: namespace SilverlightRun

   4: {

   5:     public class CounterViewModel : ViewModelBase

   6:     {

   7:         private int _Counter;

   8:         public int Counter

   9:         {

  10:             get

  11:             {

  12:                 return _Counter;

  13:             }

  14:             private set

  15:             {

  16:                 _Counter = value;

  17:                 NotifyChange(() => Counter);

  18:             }

  19:         }

  20:

  21:         public ICommand CountCommand { get; private set; }

  22:

  23:         public CounterViewModel()

  24:         {

  25:             this.CountCommand = new ActionCommand(() => this.Counter++);

  26:         }

  27:     }

  28: }

Ignore everything but line 17. There I notify about a change of the Counter property in a type-safe manner. Pretty cool, if you would ask me! The code of the ViewModelBase class is very simple as well, like you can see below.

   1: using System;

   2: using System.ComponentModel;

   3: using System.Linq.Expressions;

   4:

   5: namespace SilverlightRun

   6: {

   7:     public abstract class ViewModelBase : INotifyPropertyChanged

   8:     {

   9:         public event PropertyChangedEventHandler PropertyChanged;

  10:

  11:         protected void NotifyChange(Expression<Func<object>> locator)

  12:         {

  13:             if (this.PropertyChanged != null)

  14:             {

  15:                 string property = this.GetNameByLocator(locator);

  16:                 this.PropertyChanged(this, new PropertyChangedEventArgs(property));

  17:             }

  18:         }

  19:

  20:         private string GetNameByLocator(Expression<Func<object>> locator)

  21:         {

  22:             LambdaExpression lambdaExp = (LambdaExpression)locator;

  23:             UnaryExpression unExp = (UnaryExpression)lambdaExp.Body;

  24:             MemberExpression memExp = (MemberExpression)unExp.Operand;

  25:             return memExp.Member.Name;

  26:         }

  27:     }

  28: }

The trick is to use a Linq expression that you can parse and get the name of the property, like I do in lines 22 to 25. Now you can refactor all you like and still get a compiling output. Actually, refactoring can be done including strings as well with some tools already. Type-safety as well is also not quite true, since the types don’t matter at all. Everything that is inheriting from object can be used to notify a change of it. It is just more type-safe than strings, so meta-type-safe would be a better term to describe it.

That’s it for this time. Please check out my German website www.mnsdc.de and follow me on twitter (@halllo) www.twitter.com/halllo if you have not done so already.

Take care!

Manuel Naujoks

Advertisements

Comments are closed.