Tuesday, April 15, 2014

Visual Studio 2013: Displaying C# Partial Classes as a Hierarchy in Solution Explorer

Goal


This post demonstrates how to make C# partial classes display as a hierarchy within a Visual Studio 2013 project. To demonstrate consider the partial class DataTier which is composed of three source files: DataTier.cs, DataTier.Query.cs and DataTier.CreateUpdateDelete.cs. The aforementioned files appear in Visual Studio's Solution Explorer (View | Solution Explorer) as follows:


The files appear in non-hierarchical fashion because they were each created by right clicking on the project and selecting Add | Class from the context menu. Visual Studio by default simply alphabetizes the file when it displays them in Solution Explorer. This post demonstrates how to display the files as follows, in a hierarchical fashion:


Notice in the previous screenshot that DataTier.cs is the root file displayed and  DataTier.Query.cs and DataTier.CreateUpdateDelete.cs are displayed underneath.

Background

A C# class prefixed by the partial keyword allows the class definition to span multiple source files. Using multiple source files per-class is desirable to:
  1. allow multiple developers to work on a class but allows each developer to be siloed which simplifies code sharing.  
  2. generated code, where the class to be defined such that there is a source file for manually created code (code created by a developer) and a source file for automatically generated such as the code created by the designers for Windows Forms, WPF and ASP.NET.
A case where partial classes can be used is in developing a data base API where the following files are defined:
  • DataTier.cs: contains constants shared by methods that query and methods that create, update and delete.
  • DataTier.Query.cs: contains the methods associated with querying data.
  • DataTier.CreateUpdateDelete.cs: contains the methods associated with creating new objects, updating existing objects and deleting existing objects.

Solution

As was mentioned previously, creating three source files by right clicking on the project in Solution Explorer and selecting Add | Class from the context menu, results in the files being displayed in non-hierarchical fashion. When Visual Studio creates Windows Forms, ASP.NET Forms and WPF Forms, it displays said files in a hierarchical fashion. To display the three partial source files associated with the DataTier class as a hierarchy, is a matter of reverse engineering a project that contains a hierarchical display of files. Visual Studio's ASP.NET Web Forms Application project template creates a project with parent/child files displayed within Solution Explorer. 

An ASP.NET Web Forms Application project is created in Visual Studio 2013 by selecting File | New | Project | Templates | Visual C# | Web | ASP.NET Web Application, specifically the Web Forms template:


To the right of the previous screenshot notice that for the class, About, there are two files under About.aspx: About,aspx.designer.cs and About.aspx.cs. The previous screenshot shows to the left that these files are just the source files associated with the About partial class.

Within the project file, WebApplication.cspoj, the files associated with the About web form are represented as follows:


Each child source file contains DependentUpon element that specified About.aspx.cs and About.aspx.designer.cs should be displayed under About.cs.

The source files associated with the DataTier partial class are found in the project file, ConsoleApplication1.csproj. The DataTier partial class files within  ConsoleApplication1.csproj are represented as follows:


By creating a DependentUponchild element for DataTier.Query.cs and DataTier.CreateUpdateDelete.cs, these files can be displayed under DataTier.cs within Solution Explorer:


The previous changes have to be made by hand using Notepad, Notepad++ or any text editor. The result when the following changes are made and the project is opened by Visual Studio is the files are displayed as a hierarchy:


This approach works with Visual Studio 2005, Visual Studio 2008, Visual Studio 2012 and Visual Studio 2013. In the future Microsoft may get around to adding the feature directly to Visual Studio. Users creating partial classes is a fairly rare event so moving forward partial classes may just need to be manipulated by hand to get the desired display within Solution Explorer. 






1 comment: