Click here to Skip to main content
15,891,423 members
Articles / All Topics
Tip/Trick

Templates Add Life

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
30 Nov 2011Public Domain4 min read 14.3K   1   5
DevExpress eXpressApp Framework (XAF), a really powerful application framework for ASP.NET and Windows Forms applications.

Introduction

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, like this:

C#
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:

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. 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. 

Guessed-Fields

Now for the magic! In the next image, the template editor presents a grid showing each one of my fields, the source of that fields 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.

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 cliche: Voila! 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.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Founder Erisia Web Development
South Africa South Africa
I am a software developer in Johannesburg, South Africa. I specialise in C# and ASP.NET MVC, with SQL Server, with special fondness for MVC and jQuery. I have been in this business for about eighteen years, and am currently trying to master Angular 4 and .NET Core, and somehow find a way to strengthen my creative faculties.
- Follow me on Twitter at @bradykelly

Comments and Discussions

 
GeneralMy vote of 5 Pin
Nirav Prabtani27-Apr-17 21:26
professionalNirav Prabtani27-Apr-17 21:26 
GeneralMy vote of 5 Pin
pt140125-Jan-14 10:04
pt140125-Jan-14 10:04 
GeneralMy vote of 1 Pin
Nirav Prabtani25-Jan-14 1:12
professionalNirav Prabtani25-Jan-14 1:12 
GeneralRe: My vote of 1 Pin
Brady Kelly25-Jan-14 3:29
Brady Kelly25-Jan-14 3:29 
GeneralRe: My vote of 1 Pin
Marco Bertschi25-Jan-14 4:40
protectorMarco Bertschi25-Jan-14 4:40 

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.