forty-two Random thoughts filtered through a babelfish.

Caliburn.Micro ViewModel File Template for ReSharper

This is post is part of my Learning Caliburn.Micro series. Here are the other posts in the rest of the series.


Over the last few weeks I have been writing some in-depth articles for my Learning Caliburn.Micro series. Today is a brief side-discussion where I introduce a ReSharper File Template to help with the task of generating view-models.

Background

So why do you need this template? Simple – to write those twenty or so lines of code you constantly have to do yourself. I will warn you – this template is highly opinionated. It assumes that you when you create you view-models, you have the ability to pass the IEventAggregator as a parameter. I use Autofac as the way all my view-models are created, so it deals with this dependency injection for me. If you use MEF, then you will need to add the appropriate ImportingConstructor attribute. If you use ReSharper this is easy enough. Just edit the template after you import it.

If you don’t want the IEventAggregator, or you want to another type, it is also that simple. Just modify the template accordingly. I have a few of these templates, each specialized for my use cases.

Usage

Now the fun part. How you use it. You need to import the following template:

If you don’t know how to do this, then I suggest you check out the Templates Galore: File Templates post from JetBrains.

Once it is imported, make any changes you want, or just copy it and change the copies.

Then use the ReSharper New from Template command, and select your template (or if you didn’t add it to your short list, use the More… option).

I chose a highly expressive name, and here is the result:

I used the ReSharper macro functionality to provide a quick drop down of potential base classes. Just pick one and you are done.

Summary

As I work on the Learning Caliburn.Micro series, I hope to post some more quick helpers to complement the deeper functionality articles. This template is available on github, but not in my normal repository. Instead, it is posted as a gist, a git snippet, and is available in this post by clicking on the filename in the gist above.