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:
#region FirstName Property
private String _FirstName = null;
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:
#region AddUser Command
private RelayCommand _AddUser = null;
public RelayCommand AddUser
{
get
{
if (_AddUser == null)
{ _AddUser = new RelayCommand(AddUser_Execute, AddUser_CanExecute); }
return _AddUser;
}
}
private void AddUser_Execute()
{
}
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