Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better way to write MVVM boilerplate code?

I have found myself recently writing a lot of boilerplate MVVM code and wonder if there is a fancy way to get around writing it all? I already use a ViewModelBase class that implements INotifyPropertyChanged but that doesnt solve the problem of having to write all the accessor code etc. Perhaps by writing a custom attribute that does this, or via a templating system?

public MyClass : ViewModelBase
{
    private int someVariable;

    public int SomeVariable
    {
        get
        {
            return this.someVariable;
        }

        set
        {
            this.someVariable = value;
            this.NotifyPropertyChanged("SomeVariable");
        }
    }
}
like image 941
Chris Avatar asked Nov 18 '13 15:11

Chris


2 Answers

I have a snippet that i use to create my view model properties. This particular snippet uses the Expression<Func<T>> notation that other commenters have hinted upon.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>View Model Property</Title>
      <Description>
          Declares a property and member suitable for Viewmodel implementation.
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>propvm</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>propname</ID>
          <ToolTip>Property Name</ToolTip>
          <Default>Name</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>type</ID>
          <ToolTip>Property type.</ToolTip>
          <Default>Type</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>init</ID>
          <ToolTip>Member initialisation</ToolTip>
          <Default>null</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Kind="type decl"><![CDATA[public $type$ $propname$
{
    get { return m_$propname$; }
    set 
    { 
        m_$propname$ = value;
        base.OnPropertyChanged(() => $propname$);
    }
} $type$ m_$propname$ = default($type$);$end$]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Note the call to base.PropertyChanged(). I have a ViewModelBase class to do the heavy lifting of property notification and validation for me.

Usage is this:

  1. Type propvm
  2. Hit TAB twice
  3. Fill in the highlighted field and press tab to flip to the next one!

Walkthrough : Creating a code snippet

like image 143
Gusdor Avatar answered Oct 19 '22 05:10

Gusdor


Aspect oriented programming (AOP) is a way to reduce the amount of such boilerplate code. A framework that is widely known is PostSharp. There also is a free Express edition.
You use attributes (either on the classes directly or as a multicast to all points in code that satisfy a specific set of conditions) to mark the spots where the code should be integrated and PostSharp weaves in the implementations during build. You can find an example for the implementation of INotifyPropertyChanged here.
An AOP based approach (no matter which framework you use) has the advantage that you can change the implementation afterwards and that these changes are reflected in the existing code base. It is also possible to apply the aspects to a big number of already existing classes.

like image 41
Markus Avatar answered Oct 19 '22 03:10

Markus