Andrew Connell [MVP SharePoint]
1579 Posts |  42 Articles |  4864 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
SharePoint Quick Links
Article Categories
Archives
Post Categories


Add to Technorati Favorites

In Part 1 of this series on these little tools I created for SharePoint developers I gave a bit of an overview of what they are all about and where you can get additional information. Part 2 discussed CodeRush (CR) templates and showed how they are used. Part 3 showed some MOSS Publishing-specific templates in the collection. Part 4 dig into the guts of how the templates work... specifically around the subject of StringProviders. In this final bit, I want to show how easy it was to build a custom refactoring.

One of the things that I included in AC's VS CodeRush/Refactor Tools for SharePoint Devs is a custom refactoring called Create Web Part Definition (*.webpart). The way it works is if the cursor is on a type that implements System.Web.UI.WebControls.WebParts.WebPart, this refactoring is available. If activated, it will create a *.webpart file, add it to the project, and fill it up with the contents needed. In addition, it adds the 5-part name of the Web Part type ([namespace].[typename], 4-part name). See for yourself (or view the screencast demo linked on the main page for my tools, linked at the bottom of this post):

webpartRefactor
There is a much better, and bigger, screencast available on the main page, linked below.

So, how was this built? Well, it was pretty easy... a lot easier than I thought. First, create a new CR plugin. Then, drag the RefactoringProvider object from the Toolbox onto the design surface (it's likely not present in your toolbox... you'll need to add it... look for this: DevExpress.Refactor.Core.RefactoringProvider). With the provider added, set a few properties as shown here:

refactor - 1

Now, with the properties set, there are two events that need to be implemented. First, you need to implement the event that checks to see if the refactoring is available (if it shows in the refactoring dropdown) in the current context using the CheckAvailability event. All this guy does is check to see if the cursor is on a type that implements the desired WebPart type:

refactor - 2

With the availability set, now we need to add the code that will do the work of the refactoring. The event that triggers the refactoring is Apply. What this code does is get a reference to the current active project, walk through all types in the project and check each to see if it inherits the WebPart class. If it finds one (which it should since the refactoring is available), it will create a new file named [webpart_type_name].webpart and write out the contents. Then, it adds the file to the project. Here's the code:

refactor - 3

Not too hard huh? The best part: you are no longer restricted to the refactorings provided by Visual Studio... you can create your own using DevExpress' Refactor! Pro.

One thing to note: DevExpress has all but stated that they are likely to change the way the RefactoringProvider object works under the covers. This is something they've never / rarely done, but it would be necessary to make life much easier and provide more power to custom refactoring developers. While this won't break any custom built code, it will break the assemblies built from it. So, all refactorings will need to be rebuilt when the next major release of CR/R comes out. I'll make sure to rebuild my tools when that takes place.

To get more information on the tools including documentation, links to detailed screencasts / demonstrations, and a history, check out the main page:

» AC's VS CodeRush/Refactor Tools for SharePoint Developers

Let me know what you think! Oh... and I didn't name this post "part 1 of 5" for some random reason. Over the next four days, I'll add another post related to these productivity enhancements.  :)

posted on Wednesday, August 22, 2007 3:31 PM

Feedback

# re: Creating custom Refactorings for use in Refactor! Pro: The "Create Web Part Definition" Refactoring (Part 5 of 5) 4/16/2009 11:15 PM Agustin
Gravatar "Now, with the properties set, there are two events that need to be implemented. First, you need to implement the event that checks to see if the refactoring is available (if it shows in the refactoring dropdown) in the current context using the CheckAvailability event."

That's something I don't understand. I mean, why an event? Why do they use events for that? I don't think that's a good design. An event just for one object. Why not implement an interface. An event is something useful for broadcast messages, maybe with a lot of observers. But here, you know there's always just one observer. Am I wrong ?

I'm asking this because I'm researching about how this "providers" work. I really like the idea. I was even checking the source code of refactor pro through reflector. But even checking their own refactorings. I see they instantiate a "RefactoringProvider" object and then they subscribe the events. But there's just one object suscribed (or none). However, why use events, and not an interface with a"CheckAvailability", "apply", etc, methods

Hope to make sense with all these :)

Cheers,
Agustín

Post Feedback

Title:
Name:
Email:
(email will not be displayed)
Url:
Comments: 
Please add 2 and 4 and type the answer here:    
All Comments Are Filtered & Moderated
Unfortunately comment spammers are just too effecient and are constantly dirtying up blogs with irrelevant and unwanted comments trying to improve their standing on search engines. All comments on this blog are moderated. I do not censor comments, but I don't approve comments with vulger language or those soliciting products. Most of the time comments are approved within a few hours of being submitted with the only exception when I'm traveling.

Why are you asking for my email address?
The only reason I'm asking for your email address, which isn't required to submit a comment, is to provide a gravatar if you've created an account for yourself and associated your email address with a small image. If you have a gravatar created for the email address you submit, it will appear next to your comment. Otherwise nothing will appear.

What is a gravatar?
A gravatar is a "globally recognized avatar." You can get more information about gravatars, as well as create your own for free, at www.gravatar.com. You can also view my gravatar here.


Copyright © 2003 - 2010 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License
Site design by Heather Solomon.

 
 
MOSS WCM Training
Looking for MOSS 2007 WCM developer training? Look no further! I teach my 5-day hands-on and online WCM classes for developers I offer through my company: Critical Path Training.

Get more information on the WCM courses!