I wrote a blog post called Functional Construction with LINQ to XML. I wrote a book entitled LINQ Unleashed: for C# and have written several blogs, dozens of articles, and the book Professional DevExpress ASP.NET Controls, which has several LINQ queries in it. Having done all of that and having used LINQ in applications and presentations, unfortunately there are just too many intricate details to remember everything. Fortunately, one thing that helps me remember details is the Internet, my own blogs, articles, and books. Another thing that helps me remember is CodeRush. CodeRush is a useful extension of my memory.
In the last blog I wrote about Functional Construction. Functional construction is roughly constructing something from function calls. The blog employed LINQ to XML and the System.Xml.Linq namespace. The classes in that namespace construct individual elements of an XML document. All of this happens with an embedded LINQ query. The upshot is that all of this requires a meticulous attention to detail when constructing LINQ to XML queries and those queries construct XML documents. When I need to rely on meticulous details one of the things I rely on is CodeRush. If you use a meticulous feature occasionally—like LINQ to XML and Functional Construction—then create a CodeRush template to “remember” it for you.
The example that follows will walk you through, step-by-step, in creating a CodeRush template for the LINQ query from Functional Construction with LINQ to XML. (Mark Miller could probably give you a better solution, but he is a pretty busy guy and I don’t mind muddling through.)
If you are reading this blog then I hope you have heard of CodeRush and are using it. If not, in my own words CodeRush is a meta-programming tool or perhaps a code accelerator. CodeRush has templates that contain code stubs with replaceable elements. You type the short command name for the template and the IDE spits out the code for you. With CodeRush and its underlying DXCore engine the parts you need to change like an actual property name for the property template have visual cues and interact with the IDE to facilitate your changing them. The net result is that you get all of the code you need with a whole lot less typing and time. Everyone has heard about the mythological programmers that are twenty times more productive than every other programmer. Well, with CodeRush we can all be that guy, we can all be part of the myth.
Now, Mark and the DevExpress developers that work on CodeRush have thought of a lot of templates, and have done a great job with the common templates. However, what they have accomplished in addition to that is to make CodeRush extensible. This means if you define a template that you need then you can add the template right in Visual Studio. Here are the steps I used to define the template in Listing 1:
- In Visual Studio select the DevExpress menu; click on the Options menu
- In the navigator expand the Editor node and click on the Templates item
- A list of predefined templates will be displayed
- Click the New Category—it has the red starburst on a folder
- When the New Category dialog is displayed enter LINQ and click OK
- Select the LINQ category and click the New Template button (just to the right of the New Category button)
- Enter a name (Functional Construction) for the new template
- Define the template as shown in Listing 1
- Plain text is just typed in the way you want it generated and the special elements like <<Caret>> are commands and they are added from the toolbar or Command drop down just below the Expansion window
- Use window check select where the expansion will be activated. (Expand the and check InMethod and InMethodBlock, and expand Line and check On Empty Line)
- Finally at the bottom enter in the Dependent Namespaces box enter the LINQ to XML namespaces System.Linq and System.Xml.Linq
- Click OK when you are done editing the expansion
The explanatory comments for the expansion are provided after the code listing.
Listing 1: The CodeRush template that generates the code for a LINQ to XML functional construction of an XML document.
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "true"),
new XElement("Root",
from «Caret»«Link(Range)»«BlockAnchor» in
orderby «Link(Range)»
select new XElement("",
new XAttribute("CustomerID", «Link(Range)»),
new XElement("CompanyName", «Link(Range)»)
)));«Target»
The «Caret» command indicates where the caret will be after the expansion is initiated. When you follow the «Caret» with «BlockAnchor» this indicates the block of the expansion that will be selected. The «Link» command links some text to all other appearances of the text in the Link. In the example, «Link(Range)» will link all occurrences of Range, so when you change the text Range all other instances of Range will be automagically changed when the text is changed. Finally, «Target» is where the cursor stops when the user has modified the elements of the expansion.
Figure 1: Add a new Category for your custom template.
Figure 2: The Options dialog roughly as it will appear after the expansion is defined and the configuration settings have been made.
Listing 3: The expansion right after it has been added ; you can see that the first instance of Range is selected and the cyan color indicates that all occurrences of Range are linked.
Now, CodeRush isn’t going to be very snappy with expansions named Functional Construction; that’s too many keystrokes. You create a new expansion—steps 6 and 7 above—and give it a shorter name; I used fc, for the new expansion. In the expansion window enter the name—Functional Construction—of the first template/expansion. The proper input for referring to the other expansion is «:Functional Construction».
Mark and company have gone to a lot of trouble to create some great CodeRush videos. You can check out these videos on the our website at http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/training.xml.