Hadi Hariri's Blog

Profiling Apps 1 of N: The MVC ActionLink

Tuesday, 9 March 2010 21:30 by Hadi

 

I’m starting a new series of blogs posts on profiling, where we’ll try and cover common bottlenecks and how to identify them in your applications. However, before delving deeper into the subject, let me make a small but important observation:

Your bottleneck is probably not your for loop

Now, replace that for loop with switch statement, an older version of some outdated algorithm that you feel needs optimizing, or that retched collection of classes that would perform better if you were using an array to loop through them, and you’ll end up with the same observation.

Premature Optimization

When dealing with business applications, it is unusual for major performance problems to be pinpointed down to specific portions of code or a concrete implementation of an algorithm. Usually most of the issues are bottlenecks at the data level, network level or purely down to how a business decision is made.

Whether we use an ORM or use SQL directly, incorrectly formulated queries are one of the most predominant causes of bad performance. Not understanding concepts such as Lazy or Eager loading when using an ORM can be disastrous to the performance of an application, and are usually portrayed as “ORM XYZ sucks at performance”. 

Network bandwidth and latency are other issues; when dealing with web applications for instance, having large pages (i.e. ViewState) or rendering Javascript directly without using script files, are a common problem for performance penalties. Making heavy calls to the server when very little information is required (i.e. UpdatePanel used incorrectly) are again main causes for concern.

In many cases, design decisions we make early can affect the performance of our applications, and it is important to identify these concerns and address them correctly. Using an ORM, profiling it, understanding how Ajax really works and not worrying about working with Javascript, or using an asynchronous architecture when dealing with long running business processes are many ways to avoid bad performance in the long run.

On the other hand, what we shouldn’t do is focus on micro-optimizations, on trying to make the most efficient, yet completely undecipherable algorithm to calculate the probability of winning money when buying lottery tickets, when the underlying problem is a bottleneck caused by a bad query. This kind of approach is often referred to as Premature Optimization, and can be disastrous for a project.

When it does boil down to code

Having said all that, there are times when we need better performance after having eliminated all the obvious causes, and need to discover why something is not performing as well as it should be. Of course, these concerns are greater when the nature of our application demands highly optimized code. In these cases, it is crucial to understand how things work in order to solve the problem. As a old-school boy, before we had managed libraries and drag-n-drop, I’m also a firm believer that it is always important to understand how things work under the covers, even if it is to just improve one self's knowledge.

Therefore, in these series of blog posts about performance, I’m going to focus on the latter, examining the details of code and how some things can perform better than others. So given the disclaimer, let’s get down to business.

In order to do performance tuning, you need to use a tool. Setting stopwatches doesn’t work, because as Christian Gross so rightly pointed out during one of his talks, and I semi-quote: ‘if you’re using a stopwatch, you think you know where the bottleneck is. Most of the time, you’re wrong’.  If you are setting using a manual approach of setting calling Start and Stop, trying to time something, you’re assuming you know that the performance problem is located in a particular point, and many times it is not that point. So you end up having to place these kind of diagnostic codes in various places in your code, and soon it becomes a maintenance nightmare. Fortunately, there are tools that can profile your application in a non-invasive manner. When talking about SQL profiling, there’s NHProfiler for instance. When it comes to code performance profiling, the two most known ones are ANT Profiler and dotTrace. I’m going to be using dotTrace. I used it before joining JetBrains and continue to use it now that I’m at JetBrains. I’ll be using version 4.0 which is currently (at the time of writing this post), in Early Access Program and with Beta being released very soon.

To Express or not Express

Those that are familiar with it ASP.NET MVC know it relies heavily on the use of strings in many areas. For instance, when defining ActionLinks, you write

Html.ActionLink(“Home”, “Index”, “Home”)

where the first parameter is the link text, the second the Action and the third parameter is the Controller. The problem with this of course is that if you type either the second or third parameter incorrectly, you won’t know until runtime. Even if you build your views it won’t help.

An alternative is to use Expression based Html Helpers (another option is to use ReSharper of course :)). These are strongly-typed ActionLinks that do not ship out of the box with ASP.NET MVC, but are available as a separate download in the MVC Futures assembly, which can be thought of as a kind of sandbox for Microsoft to play with. Some of the functionalities in this library have eventually made it to the main core, such as RenderPartial, which was in fact there from pretty much the early Previews of MVC 1, and didn’t get all the excitement until it made it into the core in version 2. Other functionality, including the expression based ActionLinks haven’t made it in yet. When using these helpers, the previous link would be:

Html.ActionLink<HomeController>( a => a.Index, “Home”)

In principle this looks good, and begs the question of why it is not in the main core. Well I don’t know the exact reason, but one could potentially be due to it’s performance. Several people have talked about the difference in terms of rendering when using this version as opposed to the standard string based one. You can find one of those posts here. What I thought I’d do, is actually see how much difference in speed there is between one and the other.

The Project

I’m using a very simple project for this profiling. It’s your standard ASP.NET MVC 2 application. On Index page, I’ve added two blocks of code:

  1. <% for (int i = 0; i < 1000; i++)
  2. {%>
  3.   <%= Html.ActionLink("Link " + i, "Index", "Home")%><br />
  4.   <%
  5. } %>
  6.  
  7. <% for (int i = 0; i < 1000; i++)
  8. {%>
  9.   <%= Html.ActionLink<HomeController>(a => a.Index(), "Link " + i)%><br />
  10.   <%
  11. } %>

The first for loop will render out 1000 links using the string based ActionLink version, whereas the second loop will do the same using the expression-based one.

What we want to do now is run this and see how long it takes for each loop to complete.

Setting up dotTrace Profiler

Working with dotTrace is as easy as it gets. There are two ways to profile an application: Standalone or integrated within Visual Studio. In the case of the former, you can start up dotTrace outside of Visual Studio and point to an application to profile. On the other hand if you have it integrated inside Visual Studio, then all you need to do is click on the Profile menu option:

image

Once we do that, we get a dialog box that provides us a series of options, mainly to do with the type of profiling we are going to perform.

image

Since we’re profiling a web application, we can either use the Development Server or Internet Information Server. In our case we’re going to use the former. dotTrace will automatically pick up the server settings as well as fill out the physical path for our application.

Next come the profiling options. The basic settings are Profiling Type and Meter Kind. The first parameter indicates how profiling will take place. It can be:

  • Sampling: dotTrace will do frequent analysis of calls stacks. It’s the least intrusive, has very little impact on performance, but gives approximate timing.
  • Tracing: dotTrace receives notifications from the CLR on entry/exit of methods. More precise timing and call information.
  • Line-by-Line: dotTrace logs times for every statement in methods. Most precise but also has higher impact on performance.

Tracing is normally the recommended option. Meter Kind defines how dotTrace logs the time: CPU instruction or Performance counter (uses the Windows API and samples are hardware independent).

Profiling our application

Once we have everything setup, we can start profiling our app. dotTrace will launch a small panel that allows us to control data sampling.

image

dotTrace does not by default however launch the browser. In order to do so, we need to either click on the WebDev server and Open in Browser or just type the URL directly in the browser.

image

The next step is to perform the operations we want to profile and then click on GetSnapshot.

image

Since in our case, having rendered the Index action performs these operations, once the page has been loaded, we can click on GetSnapshot and have the profiler launched.

image

I’m not going to get into all of the details of dotTrace in this first post because otherwise it would never end;  we’ll cover some of the aspects in future posts. For now, lets focus on our performance test at hand which is the difference between the two types of ActionLinks (string and expression based).

The easiest way to find what we are looking for is to use the…you guessed it, Find feature. Ctrl+F will bring up a dialog box similar to ReSharper’s Type location. We can then type ActionLink to filter the list of functions down to the ones that interest us

image

We can see that there are two versions, as expected. Let’s drill in to the second one first, the string based one. Hitting Enter will find the first location. We can then press F3 until we find the one that interests us. Remember, Site.Master and other references to this call also exist. We’re specifically looking for the loop, the one with 1000 calls

image

We can see that the ActionLink call takes 121ms for 1000 calls. Drilling in, we can see exactly where the time is spent, and 104ms of that is calling GenerateUrl. Now let’s take a look at the Expression based ActionLink

image

For the same 1000 calls it takes 140ms, which is an increase of approximately 17%. Diving in once again, we can see that 6ms of this is in the parsing of the expression tree, GetRouteValuesFromExpression. What this function does is merely analyze the expression to extract the ActionName from the parameter. The ControllerName it already has since it’s the concrete type the generic method is invoked with, returning both values in a RouteValueDictionary. As such it then needs to call GenerateRouteLink as opposed to GenerateLink since the former takes a RouteValueDictionary as a parameter, whereas the latter takes strings indicating the controller and action. They ultimately both call GenerateUrl.

Summary

From the results, the difference between the two calls is not that significant for 1000 links. As the number of links increment, the difference between the two does not change significantly. For instance, rendering 10.000 links, has a difference of 50ms between one version and another.What’s interesting that having run the same profiling on previous releases, the difference in time was nearly double, so there seems to have been improvement in this area. And as we can see, sometimes what might seem a performance problem, isn't necessarily one.
Tags:   , ,
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

MSpec Live Templates

Monday, 1 March 2010 16:04 by hadi

 

If you’re using MSpec (Machine.Specifications), you’ve no doubt run into the verbosity of it. Here’s a typical specification in MSpec

  1. [Subject("Registering as new a usergroup")]
  2. public class when_requesting_registration_page
  3. {
  4.     Establish context = () =>
  5.     {
  6.     };
  7.  
  8.     Because of = () =>
  9.     {
  10.     };
  11.  
  12.     It should_display_blank_registration_form = () =>
  13.     {
  14.     };
  15. }

 

The code is usually divided into three areas, the Establish that defines the context, Because which is defined as the action to take, and finally one or more benefits that are defined as It sections. Having this layout makes it easier to understand specifications and both the console runner as well as ReSharper’s MSpec runner remove underscores and provide a nice formatting.

Live Templates

However when it comes to typing all this in, well all those brackets and symbols become tiring. For that I normally use ReSharper Live Templates. If you’re not familiar with these, think of them as code snippets that allow interaction. Each template is uniquely identified by a shortcut, which is a series of characters. To invoke it, you simply type the characters and hit TAB. It then inserts some code. Up to this point it’s like a normal snippet. The twist however is that you can then TAB through placeholders and define values (I’ll cover  Live Templates and all the possibilities it allows in a future post).

image

What I’ve been using for a while are some Live Templates for MSpec that allow me to define specifications with a little less effort. The first one is for empty specifications, which is useful for defining scenarios for features and then later proceeding to implement them, named espec. The second one is for the specifications and implementation, named spec.

image

To install them, the easiest way is to just open up Live Templates (ReSharper menu) and Import the following file.

.

image

Obviously these are just two simple versions and can be expanded on. For instance, if you use Behaviors in MSpec, you could write an additional one for that. Feel free to use them for your own needs.

Tags:   ,
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Cannot Launch Debugger error in ReSharper

Tuesday, 16 February 2010 11:31 by hadi

 

[Note: This blog post applies to the nightly builds for ReSharper 5 version 1616 and lower]

If you’re getting the “Cannot Launch Debugger” message when trying to debug Unit Tests with the ReSharper runner, you’ll be happy to know it’s fixed. However, if you can’t wait until the next build, you can workaround by taking the following steps:

1. Locate the following files located in %Program Files%\JetBrains\ReSharper\v5.0\Bin\

  • JetBrains.ReSharper.TaskRunner.CLR4.exe.config
  • JetBrains.ReSharper.TaskRunner.CLR4.MSIL.exe.config
  • JetBrains.ReSharper.TaskRunner.CLR4.x64.exe.config

2. Find the entry in each file:

<startup>
  <requiredRuntime version="v4.0.21006" />
</startup>

and change them to:

<startup>
  <requiredRuntime version="v4.0" />
</startup>

Tags:  
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

Localizing your Applications with ReSharper 5

Tuesday, 16 February 2010 11:01 by Hadi

This is the first in a series of screen casts on ReSharper. I’m still getting to grips with Camtasia as well as Flowplayer, not to mention streaming and video formats in general, so be gentle. I'd appreciate any feedback, including but not necessarily limited to Contents, Duration, Resolution, Pacing, Voice and Quality in general.

In this first screen cast I’m going to show you some of the Localization features that come with ReSharper 5. Enjoy!

Tags:  
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (6) | Comment RSSRSS comment feed

Technological shortcomings driving business needs

Monday, 15 February 2010 14:41 by Hadi

 

Recent conversation I had with someone the other day:

Jack: Yes, but we can’t do it like that. The business does not work that way.

Me: Why does it not work that way?

Jack: It’s a fundamental aspect of the business. We can’t change that.

Me: And I accept that, but have you thought about why it is like that?

Jack: Well because it’s not viable.

Me: Why is it not viable ?

Jack: It would cause too much havoc.

Me: Why would it cause too much havoc?

Jack: Well because the software doesn’t allow it.

Reading this script, one would think that it is quite absurd, yet this situation is quite common.

Updating a legacy system

Jack in this case was a developer, that is working on re-designing an existing system. But Jack could be anyone. In fact, many times, Jack is a user of the system.

Users are many times accustomed to having software impose the way they do their job. They have to accommodate their process to that of the way the software they are using functions. What then happens is that they become used to this way of working to the point that they actually change their process, to work with the system, many times attempting to make their own life easier.

The problem is of course that these changes have an impact on the business. At times this influences productivity, at other times revenue, or even how dealing with customers is handled, ultimately jeopardizing the business or its potential.

When the time comes to develop a new system, what was considered an impediment has now become a fundamental pillar of how the business should work. As such, the user no longer thinks of how he/she would ideally want to do his/her job and have the software adapt to that, but lets his/her requirements be driven by technology.

Of course, all this become even more vicious when new users join the scene. They are not aware of the real reasons behind certain workings and assume that that is how it is meant to be, no questions asked.

Focus on the customer, not the software

It is very important for us to analyze the true needs of our users, to make them look beyond existing systems and offer them the possibility of describing how they would want to work if they had a blank sheet of paper and could start from scratch. If we have the opportunity to re-design an existing system, let’s make sure we base it on the users needs, not the existing software.

Upgrading existing systems is not about making them use the latest, greatest and most shiny technology.

Tags:  
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Removing old feed

Monday, 25 January 2010 12:50 by Hadi

Due to mostly uninteresting reasons, I’ve been running two different feeds to this blog:

http://feeds.feedburner.com/hadihariri/rSpO

and:

http://feeds2.feedburner.com/HadiHariri

This second one is the valid on. I’ll shortly be removing the first one (in about a week). There’s over 200 subscribers to it, so please, if you’re one of these, could you update your feed to the new one? Once again that would be this

 

Thanks.

Tags:   ,
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

ASP.NET Support in ReSharper 5

Wednesday, 20 January 2010 17:58 by Hadi

Although I mentioned briefly in the past some of the new features ASP.NET MVC features ReSharper 5 supports, I thought it would be a good idea to sum up the main ones in a single post and go into a little bit more detail on them.

 

ASP.NET

ReSharper ‘s support for ASP.NET is not restricted only to MVC.In fact, most of the new features are for general ASP.NET, be it WebForms (also known as Traditional, Classic, For Historical Purposes Only?) or MVC.

 

Go to File Member

As part of the code navigation features of ReSharper, you have the possibility to locate a file member, be it a method, property, class, etc. very easily by using the the Go To File Member (Alt+\) option. You can search instantly and hit Enter to navigate to the specific one. [For the record, I’m using the Visual Studio Keyboard Scheme. If you’re using IntelliJ (why?) go to the web site and download the PDF mapping files.]

 

image

Notice how it it looks for anything that matches the characters introduced, so for instance typing “Exc” would give us all those with the word “Exception”

image

Well you now have this functionality in ASPX, ASCX, ASAX and Web.config files also as of ReSharper 5!

image 

 

The same goes for the File Structure tool Window (Ctrl+Alt+F).

image 

 

Go To Related Files

Many times, an ASPX has references to other files, such as Cascading Style Sheets, Javascript Files, Master Pages and User Controls. You can now navigate to these files efficiently by using the Go To Related Files (Ctrl+Alt+F7)

image

 

Master Pages

Version 5 also adds support for Master and Content Pages. To begin with, we have added support for navigation. Using Go To Declaration (Ctrl+Left Mouse) on the ContentPlaceHolderID will take you from the Content Form/Page to the corresponding ContentPlaceHolder of the Master Page

image

image

If you are in the Master Page, you can navigate to all its inheritors (Shift+Alt+F12)

image

You can also create content place holders from usage. When defining a ContentPlaceHolderID that does not exist, you have the option to create it in the Master Page (Alt+Enter)

image

Last but not least, in a page that uses a master page, you can generate content place holders by pressing Alt+Ins

image 

which in turn will bring up a dialog box for you to pick and choose what you want

image

 

User Controls

You can now navigate to user controls as well as automatically import the correct references.

image

 

File Generation

There are new code generation options for ASP.NET with version 5. By pressing Alt+Ins in the Solution Explorer, we are presented with a popup menu

image

where you can choose from a selection of items to generate. You can also access this menu from anywhere (not only in the Solution Explorer) by now pressing Ctrl+Alt+Ins. This is actually an awesome new addition to ReSharper 5 (Ctrl+Alt+Ins) and it removes even more friction when create new items!

image

We’re asked for a name (which is the base minimum required to create an item) and If the item selected requires more information, then we’ll be prompted accordingly. For example, when creating a WebForm with Master Page, it’s convenient to specify which Master Page we want to use. In this case, once the page has been created, we’ll be given a chance to provide this information

 

image

I have to say, that I love Ctrl+Alt+Ins in ReSharper 5.

 

Refactoring and Assistance

In terms of Refactoring and Coding Assistance there are a few new features. One of them is the auto-update of ending tags. If you have for instance a div tag, and want to change it to span, as you start typing span, the end tag is automatically updated to reflect the changes you’re making live.

image

If you have a series of elements that you want to surround within a tag, you can easily do this by selecting all the elements and choosing Surround with Template (Ctrl+E,U)

 

image

image

image

 

ASP.NET MVC

ReSharper 5 treats ASP.NET MVC as a first class citizen. As such, it now has knowledge of concepts such as Views, Controllers and Action.

 

Navigate to View

For those of you working with ASP.NET MVC, you might have noticed that Views are now underlined in Actions

image

When you click on the View with Ctrl+Left Mouse, you’ll be presented with a dropdown menu

image

Here you have two options. The first is to navigate to the source for the View method (navigation to external sources is another new feature of ReSharper 5). The second option is to go to the View. Although you can navigate to the View in Visual Studio by right-clicking and selecting Go To View, one advantage the underlining adds is that it serves as hint for non-existent views, as you can see in the action below, which doesn’t have a corresponding View file.

image

View discovery also works with named Views. If a named view exists, the string literal will be underlined

image

On the other hand, if it doesn’t exist, it will be highlighted as an error, once again providing you the benefit of discovering any missing views earlier on.

image

 

Action Links

Those of us who have worked with ASP.NET MVC applications, know the problems with using strings when defining ActionLinks. Not only do you run into issues when refactoring, but misspelling an action or controller causes unnecessary pain. Some of us, including myself, have often resorted to using the expression based Actions available in the MVC Futures library.

ReSharper 5 now provides Intellisense, preventing you from mistyping Actions and Controllers

image

Similar to named Views, if you define a non-existing Action, you will get it highlighted. The difference here is that by pressing Alt+Enter, ReSharper will create the Action for you (create based on usage)

image

image

 

And much like Views, you can now navigate to Actions and Controllers from an ActionLink by choosing Go To Declaration (Ctrl+Left Mouse) on the Action and Controller respectively.

image

 

User Controls

Navigation is also provided for User Controls

image

as well as Intellisense

image

 

Summary

I’ve outlined some of the main features that ReSharper 5 brings to the table in regard to ASP.NET. There are many smaller features that you can discover eventually as you play with it more. If you haven’t yet, make sure you download 5 from JetBrains. Don’t forget to follow ReSharper on Twitter if you want tips, tricks and latest info.

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (2) | Comment RSSRSS comment feed

Writing plug-ins for ReSharper: Part 1 of Undefined

Tuesday, 12 January 2010 14:24 by Hadi

ReSharper does a lot of things, but as they say, you can’t please all the people all of the time. However, one the great things about ReSharper is that it is quite extensible and there are already quite a number plug-ins available. Some of the better known ones are:

- StyleCop for ReSharper by Howard Van Rooijen

- TDD Productivity Plugin by Eric Hexter

- ReSharper Test Runner for MSpec by Alexander Groß 

and of course there is also the repository of plug-ins available from the JetBrains site, where many of these are located.

Now I’m no expert on writing plug-ins for ReSharper, and the authors of the previous ones leave the bar quite high, but seeing that I actually now have the time to play with them, I’ve decided to do so by sharing my experiences with you in a series of blog posts.

The ReSharper API is pretty extensive and can be overwhelming as I’ve been discovering, so I want to try and take it slowly. This might mean that at times, the code is not always complete and might be missing a few checks for example, but will come in due course.

Everything that I’ll cover applies to version 5.0 which is currently in Beta. There have been some changes from previous versions so if you’re working with 4.5 some of the code might not work.

 

The First Plug-in

I was initially intending to write a useless plug-in for the first demo that didn’t do much, but after talking to Orangy (aslo known as Ilya) who commented on a tweet Jeremy Skinner had posted a few days back:

image_thumb51

we decided to use it as the first sample.

I’m going to build up the sample over the next few series of posts, so the initial solution is not the ideal one or complete, but it will help introduce a few core concepts. Ideally this feature should be implemented as what’s known as a QuickFix but we’re first going to do it as a ContextAction.

 

ContextAction

So what exactly is a Context Action? It’s actions that can be applied based on the context (the name is quite descriptive). They shows up as items in a popup menu which is invoked using Alt+Enter (don’t use the mouse for this…it’s very unproductive).

image_thumb4

What we want to do in this first version of the plug-in is to write a new action that makes methods that are public and not declared virtual, virtual, something NHibernate users would appreciate greatly. As mentioned previously, in this first version we’re going to add a context action.

1. Creating a plug-in assembly

ReSharper plug-ins are assemblies that are located in Plugins folder (by default %programFiles%\JetBrains\ReSharper\v5.0\Bin\Plugins). Each plug-in is in it’s own folder and doesn’t require any further registration. Therefore the first step is to create a class library which will host our context action.

2. Creating the Context Action Skeleton

In order to create a context action, we need to implement the IContextAction interface. This interface has one method IsAvailable which indicates to us if that particular action is available given the context, and a property of type IBulbItem[] which contains a series of bulb items. Each IBulbItem in turn has a property Text which is the text that appears next to the item in the context dropdown, and an Execute action, which is what happens when the item is selected. So as we can see, a context action can consist of more than one bulb item. An added benefit to this decoupling is the re-usability of bulb items. By implementing IBulbItem, we can potentially use it in more than once place (obviously if it makes sense).

Many times however, we only want one bulb item per action, and to somehow simplify the process, we can inherit from the BulbItemImpl class. This class defines an abstract Text property that represents the text of the bulb item, and also has an ExecuteTransaction member which is where our code is executed. We’ll see the difference between this method and Execute shortly.

 

  1. public class MakeMethodVirtualContextActions: BulbItemImpl, IContextAction
  2. {
  3.     protected override Action<ITextControl> ExecuteTransaction(ISolution solution, IProgressIndicator progress)
  4.     {
  5.         throw new NotImplementedException();
  6.     }
  7.  
  8.     public override string Text
  9.     {
  10.         get { throw new NotImplementedException(); }
  11.     }
  12.  
  13.     public bool IsAvailable(IUserDataHolder cache)
  14.     {
  15.         throw new NotImplementedException();
  16.     }
  17. }

 

 

Before implementing any of the methods, there’s one thing we need to do. ReSharper requires that context action have a constructor with a parameter that implements a IContextActionDataProvider. This parameter is actually useful to use to provide information about the context as we’ll see shortly. Therefore, we need to add a constructor to the previous code (lines 6-9). Since our action is only for C#, we will use a ICSharpContextActionDataProvider. We then save this provider for later use (line 3). What we’re effectively doing here of course is nothing more than dependency injection. The plumbing (passing in the correct provider) is taken care of for us by ReSharper.

 

  1. public class MakeMethodVirtualContextAction : BulbItemImpl, IContextAction
  2. {
  3.     readonly ICSharpContextActionDataProvider _provider;
  4.  
  5.  
  6.     public MakeMethodVirtualContextAction(ICSharpContextActionDataProvider provider)
  7.     {
  8.         _provider = provider;
  9.     }

  10.     protected override Action<ITextControl> ExecuteTransaction(ISolution solution, IProgressIndicator progress)
  11.     {
  12.         throw new NotImplementedException();
  13.     }
  14.  
  15.     public override string Text
  16.     {
  17.         get { throw new NotImplementedException(); }
  18.     }
  19.  
  20.     public bool IsAvailable(IUserDataHolder cache)
  21.     {
  22.         throw new NotImplementedException();
  23.     }
  24. }

 

3. Defining Availability

The next step is to define when our action is available. In order to do so, we need to implement the IsAvailable method. In our case, we want to convert methods that are public and not virtual, to virtual. We therefore need to identify methods that are public and not virtual. We also need to filter out static methods. In order to get access to the current context we’re in, we can use the provider we injected in via the constructor. The provider has a GetSelectedElement method which corresponds to the element the caret is on. Since we’re interested in a method, we can invoke this function, requesting back a method declaration. If the caret is in the context of a method (i.e. header, body), it will return this information. If it’s not, it will return null.

If we have a valid method declaration, the next step is to find out if it is public. We do that by invoking GetAccessRights [In later series we’ll see that further checks are necessary here]. If it is public, we then need to check whether it is a static method, an override or already virtual. Based on that, we return true or false, indicating whether the action is available in the current context.

 

  1. public bool IsAvailable(IUserDataHolder cache)
  2. {
  3.     var item = _provider.GetSelectedElement<IMethodDeclaration>(false, true);
  4.  
  5.     if (item != null)
  6.     {
  7.         var accessRights = item.GetAccessRights();
  8.  
  9.         if (accessRights == AccessRights.PUBLIC && !item.IsStatic && !item.IsVirtual && !item.IsOverride)
  10.         {
  11.             return true;
  12.         }
  13.     }
  14.     return false;
  15. }

 

4. Performing the action

When the action is available and the user selects it, what we need to do is make the method virtual. This is done in the ExecuteTransaction method. Since we are going to modify the code, we need to make sure that the file is writable. Normally this would be done by invoking a call to a method named EnsureWritable. However, inside the method ExecuteTransaction, this is done for us, so we don’t have to worry about it. This is one of the differences between ExecuteTransaction and IBulbItem.Execute.

So how do we go about modifying code in ReSharper? Well remember that refactoring code is ReSharper’s daily bread. It’s what it does. As such, there’s a ton of infrastructure in place to allow us to do modify code easily. In fact, making a method virtual is as easy as calling a method, as shown below.

  1. protected override Action<ITextControl> ExecuteTransaction(ISolution solution, IProgressIndicator progress)
  2. {
  3.     var method = _provider.GetSelectedElement<IMethodDeclaration>(false, true);
  4.  
  5.     if (method != null)
  6.     {
  7.         method.SetVirtual(true);
  8.     }
  9.  
  10.     return null;
  11. }

 

5. Testing the plug-in

The only thing left to do is test that it works (writing unit tests for plug-ins is something we’ll cover in the future). One option is to copy the assembly to the plug-in folder for ReSharper and restart Visual Studio. However, in order to be able to debug it, what we can do is set the project properties to start an external program, which is non other than devenv.exe, and pass as parameter to it /ReSharper.Plugin <Path_to_Plugin_Assembly>, instructing ReSharper to load a specific plug-in.

How does ReSharper know which classes in the assembly are context actions? The easiest way is to decorate these with the ContextAction attribute

 

  1. [ContextAction(Group="C#", Name = "MakeMethodVirtual", Description = "Adds context action to make methods virtual")]
  2.     public class MakeMethodVirtualContextAction : BulbItemImpl, IContextAction
  3.     {
  4.   

 

This information is also what appears under ReSharper –> C# –> Context Actions

 

image_thumb1

Now all that’s left is to try it out. Write a new class, add a few methods and check to see it’s all working correctly.

 

image_thumb41

Public static method (not available)

 

image_thumb5

Private method (not available)

 

image_thumb7

Public method (option available)

 

 

 

 

 

 

 

What’s next?

In this first part we’ve seen the basics of writing plug-ins for ReSharper. In the next blog post, we’ll extend the example to also include properties and we’ll change it to be a QuickFix as opposed to a context action. This means that we’ll get some nice highlighting by ReSharper telling us something can be changed (similar to when you have something named incorrectly for instance), thus being more *in your face*.

There’s so much more to see than what’s here, and gradually we’ll drill more into each of the different areas, examining parameters, return values, etc. Until then, Happy ReSharping (did I just coin that? Too lame?)

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Dynamic types and ASP.NET MVC

Wednesday, 30 December 2009 13:25 by Hadi

If you’re using ViewModels in your ASP.NET MVC applications, know that if working with .NET 4.0, you can now create dynamic view models. And it’s actually very simple to do.

Create an ExpandoObject to represent your ViewModel:

 

image

Declare your view to be of type ViewPage<dynamic>

 

image

 

 

 

And you’re done. Here’s the output:

 

image

 

If you’re using ReSharper, you will get Intellisense once you’ve declared a property once (both in the Action as well as the View).

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

Changes as of January 2010

Saturday, 26 December 2009 20:07 by Hadi

It’s with a bittersweet feeling that I write this post. As of January, I will no longer be with iMeta Technologies. During my time there, I’ve been involved in some awesome projects, had the privilege of working with some very smart and talented people, and made some great friends. I wish them all the best success in the future.

As of January, I'm starting a new job. I’m going to go back to working from home, something I did for 8 years prior to joining iMeta, so for me it feels like home (pun intended). I have to admit that as much as I liked the office buzz and enjoyed every minute of working with Steve and his eternal ramblings on expression trees, I am happy to not have to face morning and afternoon commutes.

So what’s my new job? Well it comes as no surprise to many to say that I’ve been a fan of a tool called ReSharper for quite some time. I’ve been a member of the JetBrains Academy for a few years and have collaborated with JetBrains at various conference booths. We’ve now decided to formalize the relationship.

I’m very excited to announce that as of January I will be joining JetBrains. My primary role will be to help promote their products on the .NET side, which include ReSharper, dotTrace, as well as cross-platform tools such as TeamCity, YouTrack or WebIDE. As part of my job, I’ll be doing screen casts, blogging, and not only about using the tools, but also focusing on best practices, boosting productivity, etc. As a company that firmly believes in OSS, it also means that I’ll be increasing my participation in OSS projects and actually get to code on things I enjoy!

I’ll be active in the community, continuing to speak at conferences and events, as well as serve as a liaison for JetBrains. I’ll continue to blog on my own site as well as devlicious.

As far as Twitter, we’ll be pushing the Tool-Specific Twitter accounts for ReSharper and dotTrace, so make sure you follow them! You can contact me via my own twitter feed, and of course, don’t forget Ilya, also known as Orangy, ReSharper PM!

With that, all that’s left to say is Happy New Year and we’ll talk in 2010! And don’t start 2010 without ReSharper!

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed