Code Design and Visualization with Visual C# 2005

By Kuristosu Dei
Dev Source, May, 2005 by Joe Mayo

One of the exciting new features in Visual C# 2005 is Class Diagrams. Class diagrams give developers IDE support for visualizing the static structure of code. Furthermore, the class diagram capability enables visual design of new code. This article discusses both of these exciting new features, and shows you the details of making them work for you.

Why Visualization?

To understand the need for code visualization, think of how many times you've begun maintenance work on a piece of code that someone else wrote. Similarly, ponder the times you've returned to your own code, after more than a week of not working on it. In most cases, it takes time to acclimate yourself to how the pieces fit together.

The Class Diagram helps in this process, by allowing you to visualize the structure of an application with a graphical representation of your code. It displays types, their members, and associations between them. The result is that you now have an opportunity to get up-to-speed in understanding the makeup and relationships of objects in your application quicker than reading code alone.

The Initial Class Diagram

The process of visualization assumes you have some code that has already been written. It could have come from someone else, or could potentially be code that you wrote yourself. To get started, the code must be a part of a Visual C# project. To pull the code in from somewhere else, open the project you want to work with, right-click on the project in Solution Explorer, and select Add/Existing Item. Listing 1 shows some code I wrote to demonstrate visualization of various types and their relationships.

Listing 1. The purpose of this listing is to demonstrate the visualization process and how each type appears in the Class Diagram.

using System;

namespace Visualization
{
class Program
{
private MyDerivedClass m_derived = new MyDerivedClass();

public MyDerivedClass MyDerivedClass
{
get { throw new NotSupportedException(); }
set { }
}

static void Main(string[] args)
{
}
}

public interface IMyInterface
{
void MyRequiredMethod();
int MyRequiredProperty { get; set; }
}

public class MyBaseClass
{
public void MyRequiredMethod() { }

private int[] m_myNums = { 1, 2, 3 };

protected int this[int index]
{
get { return m_myNums[index]; }
set { m_myNums[index] = value; }
}

protected internal void MyBaseMethod() { }
}

public sealed class MyDerivedClass : MyBaseClass, IMyInterface
{
private int m_myInt;

public int MyRequiredProperty
{
get { return m_myInt; }
set { m_myInt = value; }
}

public void MyDerivedMethod()
{
}

public event MyDelegate MyEvent;
}
public class MyClass : IMyInterface
{
private int m_myInt;

public int MyRequiredProperty
{
get { return m_myInt; }
set { m_myInt = value; }
}

public void MyRequiredMethod()
{
throw new Exception("The method or operation is not implemented.");
}

public event MyDelegate MyEvent;
}

public struct MyStruct : IMyInterface
{
private int m_myInt;

public int MyRequiredProperty
{
get { return m_myInt; }
set { m_myInt = value; }
}

public void MyRequiredMethod()
{
throw new Exception("The method or operation is not implemented.");
}

public event MyDelegate MyEvent;
}

public delegate void MyDelegate();
}

To visualize the code in Listing 1, right-click on its containing project in Solution Explorer and select View Class Diagram. This produces the image shown in Figure 1.

Figure 1. Class Diagram of Code from Listing 1 produced from Visual C# 2005.

Types and Shapes

As shown in Figure 1, each type has a distinctive shape and gradient color. Classes, such as MyClass and Program, have a rectangle with rounded corners and a blue color. Interfaces, such as IMyInterface, are green rounded rectangles, and delegates, such as MyDelegate, are violet rounded rectangles. Structs, such as MyStruct, are straight rectangles in blue.

Inheritance relationships are displayed with a closed empty arrow, as demonstrated where MyDerivedClass inherits MyBaseClass. Also notice the interface inheritance relationship between MyDerivedClass and IMyInterface, which is represented with a lollipop. Both MyStruct and MyClass inherit IMyInterface, but I suspect there is a bug in my version of the Beta 2 software that doesn't display the label by the lollipop.

There is a difference between the MyDerivedClass and other classes. Its border is thicker because MyDerivedClass is sealed.

Inside Types

Initially, each type in a diagram has a chevron that points downward. When you click the chevron, it points upward, and the type expands to show its members. By default, these members are categorized by Fields, Properties, Methods, and Events. Indexers are included in the Properties category. Figure 2 shows the expanded MyDerivedClass diagram, which includes each category.
 

0 comments so far.

Something to say?