Click here to Skip to main content
15,891,184 members
Articles / Programming Languages / C#
Tip/Trick

Two Useful MVVM Code Snippets

Rate me:
Please Sign up or sign in to vote.
4.64/5 (7 votes)
9 Feb 2015CPOL2 min read 19.3K   474   12   1
Visual Studio code snippets for implementing MVVM properties and commands

Introduction

Visual Studio allows you to insert blocks of code by typing a keyword and then pressing tab. This is called a code snippet. If you want to learn more about code snippets and how to create them, take a few minutes and check out Microsoft's documentation.

Background

Some code snippets that I commonly find myself adding (or editing) are the code snippet for generating a MVVM property, and the code snippet for generating a MVVM command. I have a certain convention regarding properties and commands that I like to follow (that I wish more people would adopt), and additionally find that if I add intellisense prompts into my code snippets, I usually end up with better documented code.

Note: These code snippets are designed to be used with MVVM light; feel free to update/adjust them as appropriate for your favorite MVVM framework.

Installing the Snippet

Copy the snippet into the following folder (assuming you use VS2013, it may be different for other versions): C:\Users\{your user name}\Documents\Visual Studio 2013\Code Snippets\Visual C#\My Code Snippets

The files you save should have a .snippet extension.

Code Snippets

I'll leave you to download and examine the exact content of each snippet, so let's look at the generated code for each.

Property Code Snippet

To use it, type mvvmp then [tab]

A property named "FirstName" using this code snippet will be generated as such:

C#
#region FirstName Property

/// <summary>
/// Private member backing variable for <see cref="FirstName" />
/// </summary>
private String _FirstName = null;

/// <summary>
/// Gets and sets the first name of the user
/// </summary>
public String FirstName
{
    get
    {
        if (_FirstName == null)
        { _FirstName = String.Empty; }

        return _FirstName;
    }
    set { Set(() => FirstName, ref _FirstName, value); }
}

#endregion

The backing variable is the same name as the property itself (prefixed by an underscore), is SLOC close to the property it backs, and has intellisense documentation pointing to the member it backs, the getter does an initialization check prior to returning its value, there are no magic strings, intellisense documentation has been applied to the property, and the whole thing is wrapped in an appropriately named region so we can cleanly collapse it if we need to.

I should point out that the initializer does not handle cases where we're using primitives such as int, float, double, etc; however, I find that my personal use of those types in MVVM properties is rare enough where I don't need to have code my snippet template to accommodate them. YMMV.

Command Code Snippet

To use it, type mvvmc then [tab]

A command named "AddUser" using this code snippet will be generated as such:

C#
#region AddUser Command

/// <summary>
/// Private member backing variable for <see cref="AddUser" />
/// </summary>
private RelayCommand _AddUser = null;

/// <summary>
/// Gets the command which adds a user to the collection of users
/// </summary>
public RelayCommand AddUser
{
    get
    {
        if (_AddUser == null)
        { _AddUser = new RelayCommand(AddUser_Execute, AddUser_CanExecute); }

        return _AddUser;
    }
}

/// <summary>
/// Implements the execution of <see cref="AddUser" />
/// </summary>
private void AddUser_Execute()
{
}

/// <summary>
/// Determines if <see cref="AddUser" /> is allowed to execute
/// </summary>
private Boolean AddUser_CanExecute()
{
    return true;
}

#endregion

Again, consistent naming conventions, SLOC of member variables and associated methods are close to the command they support, intellisense comments point back to the command, initialization checks are performed, and the whole thing is wrapped in a region.

History

  • 2015-02-09: Original post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Matthew is a software developer currently living and working in San Antonio Texas. When not working on software, Matthew enjoys spending time in his backyard garden.

Comments and Discussions

 
QuestionParam Pin
Member 1046487216-Feb-15 0:48
Member 1046487216-Feb-15 0:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.