Tuesday, August 7, 2012

Grouping Partial Classes in Visual Studio Solution Explorer

Much has already been said on how useful .Net's partial classes can be. But not much on how to organize them well in Visual Studio's Solution Explorer. Given the below web application project:

Figure 1: New Partial Class File Added

The aspx page WebForm1.aspx has the default code-behind file and the designer file. For example, let's add a partial class to WebForm1 to handle validations for the page. By default, this new class file is not grouped with WebForm1.aspx like the others. Wouldn't it make sense to group this new partial class with the existing group?

The extension VSCommands 2010 has a handy command to join the new class into the existing group. It is as simple as selecting the parent class (in this case, the aspx file) and the new partial class (the aspx.validations.cs file), then clicking on Group Items.

Figure 2: Using VSCommands to Group Items

From here, a popup window will appear verifying which one should be the parent of the selected files, as shown below for Visual Studio 2010:

Figure 3: VSCommands Root Item Selector

We'll keep the aspx file as the parent. With this, we now have the following structure in the Solution Explorer:

Figure 4: New Partial Class Added to Group

The new partial class aspx.validations.cs is now a member of the WebForm1 class group! This also works for Windows Forms projects, and Visual Basic projects as well.

It should be noted that this makes sense for partial classes, but we are not limited to partial classes when grouping them in Solution Explorer! We can join any other class into the group if we wanted. Say, if we have a dedicated helper class for WebForm1 and it is not a partial class, we can still add it to the group. And we can select more than two files at the same time to initiate the item grouping. There will just be more files listed in VSCommands' root item selector in figure 3. It is even possible to attach a class to another sub-item in an existing group, creating deeper levels in the solution tree.

We actually don't need VSCommands to take advantage of this. The grouping is configured in the project file, as shown in the below snippet:

Figure 6: Project File Configurations
In figure 5, the new partial class aspx.validations.cs is given a new node value denoting which existing object it should be attached to. The default classes already has this node connected to WebForm1.aspx, so it is grouped by default. VSCommands just makes things easier and safer to do.

1 comment:

  1. Hey , Kevin

    Your blog posted on '' Studio Solution Explorer '' which , I seen your blog . I have learned more tips by this your blog many ways . Awesome consultant of Studio Solution Explorer program software . Really , I saluted your blog . Some 100 % free Hard drive data is finish of ads which are intentionally quite obstructive and frustrating. The developer earns a bit of cash from the ads, and states that if you register the program you can create the ads go away. Signing up often expenses just a few money, and if you like the program it may be well value the price.

    Thanks for sharing .

    ReplyDelete