Intro to ReSharper’s Live Templates

Every so often I have the pleasure of doing some work with the DevExpress eXpressApp Framework (XAF), a really powerful application framework for ASP.NET and Windows Forms applications. Tedious LOB code is just not my heavy metal (well, not for your average B), and XAF’s model designers and components almost save me from writing any of that kind of code, save for the actual business objects and a few custom UI validations.

It is with the business objects I have problems though. These use the proprietary DevExpress ORM library, so I can’t plug in EF Code First, with its ever so sweet POCO business objects, and I am forced to use backing field based properties, having to raise a change notification in every setter. At it’s simplest, a XAF domain class will look like this:

    public class Person : BaseObject
    {
        public Person(Session session)
            : base(session)
        {

        }

        private string _surname;
        public string Surname
        {
            get { return _surname; }
            set { SetPropertyValue("Surname", ref _surname, value); }
        }
    }

I’m sure the reader will agree that manually typing up more than just the above example quickly becomes very tedious; is also a huge waste of truly productive development time, and unnecessarily introduces risk, what with typos and all, especially with that oh-so-nasty string literal property name. While I am most sure that DevExpress’s CodeRush IDE assistant has something that magically helps coders produce these extremely fast and accurately, I am a died in the wool ReSharper man, and my honey doesn’t automatically handle proprietary structures. Now ReSharper has a powerful feature called Live Templates, of which I have only used a small sample of the built-in templates, for things like foreach loops, where ReSharper inserts all the boilerplate text and automatically focuses an edit field only where required, i.e. on variable declarations, like so:

List of property names in a dropdown that ReSharper automatically chooses for loop the variable in a For loop Live Temple.

List of property names in a dropdown that ReSharper automatically chooses for loop the variable in a For loop Live Temple.

Here the template guessed I wanted a loop over Problems, but also gave me a dropdown of other available collections – I just rather captured the more impressive choice of variable names – and at worst, the word Problems is the first thing I would have had to type after invoking the template. Next, I press TAB to travel over to var where, besides typing something new and interesting, the template gives me a dropdown choice of var or PropertyBag, and then last, but not least, for the loop variable name I get given the choice shown in the image, or, of course whatever I want to laboriously type for myself. Now I’ve known for a long time that I can somehow create such templates for myself, but I tried once and it didn’t work, and pressure was on at the time, plus I can code pretty fast anyway, so I left it there. Yesterday I took time out to investigate this monster, and discovered ReSharper makes it shockingly easy to create such templates. Please note that I am not plugging here but praising, besides pointing out an amazing feature to the chosen few, i.e. readers of this blog.

By now only the slowest readers won’t have guessed which template I set out to create, but that wasn’t the only factor that prompted me to check this out when I did. I discovered, much to my small happiness, that ReSharper has local help. I’m using version 6, and I don’t know when last I used ReSharper help, but back then it was only online, and I am using a GPRS connection right now, so any time online is strictly rationed to Very Important ThingsTM. With this local help, I found how to begin and complete a cool little template, and I outline this below.

To start I select the property declaration you see right at the top of this post, then from the ReSharper menu, select the command Tools->Create Live Template from Selection. This brings up a dialogue showing the template fields that ReSharper has inferred, and gives me an opportunity to remove unwanted fields. Unfortunately ReSharper guesses no fields for my example property, so if you’ll allow me to cross examples, doing the same thing with my foreach loop in the closer image yields one field, $IENUMERABLE$, in place of the literal text Problems. After I accept the assumed fields, or lack thereof, in my property, I get taken into the template editor, where I edit the template, replacing literal text with field names, which as you see are indicated by surrounding the field name with the $ token.

Guessed-Fields

Now for the magic! To the right of the text editing pane the template editor presents a grid showing each one of my fields, the source of that field’s value, and, where a field occurs more than once, an option for the editable instance of that field. This determines the instance you edit; which determines the value of the other instances.  The images below show the inferred field in the loop and my template after I have edited it and inserted fields where I want it to replace literal text.

Template-Editor_All

I can only show a small sample of the built-in macros, but there are many. I choose a macro for each field and save the template, and on to my final cliché  Voilà  By simply typing the shortcut I defined for my template, dxp, as seen above right – followed by TAB – I get my whole property declaration expanded and I only have to type the name once, for both private and public parts. This is because the template field doesn’t include the underscore for the private name – you have to type one underscore once – to it can juts make that properties first letter upper case, as the chosen, built-in, macro does.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>