Over the last few weeks I have been writing some in-depth articles for my Learning Caliburn.Micro series. Today is a brief interlude where I introduce a ReSharper File Template to help with the task of generating view-models.
If this is the first time you are hearing about my Learning Caliburn.Micro series, here is the list of previous posts in case you need to catch up.
- Introducing Caliburn.Micro – Lightweight MVVM & WPF
- Caliburn.Micro – Hello World
- Caliburn.Micro the MEFtacluar
- How To Do Logging with Caliburn.Micro
- Bootstrapping Caliburn.Micro with Autofac
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 ReSharper’s 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 at CaliburnMicroViewModel.cs.

Twitter Trackbacks…
…
Nice job making exposing these pieces of functionality and making them accessible and usable in their own right (when I first saw Caliburn it just looked so…different!).
Looking forward to seeing one on the Conductor pattern, in particular.
Not to mention some of the other mysteries that do ultimately turn out to be 42 :–)
Other topics I would love to see with Caliburn have to do with extending PropertyChangedBase or Screen for things like:
1) forwarding basic object overrides to the Model (primarily Equality & GetHashCode, assuming you have some domain superclass / entity)
2) IsDirty tracking, perhaps implementing System.ComponentModel.IEditableObject
3) validation, making use of some sort of model validator (I like NHibernateValidator)
Cheers!