display the Class Details window.
fiGure 10-5
fiGure 10-6
182 .
chaPter 10 modeling WiTh The clASS deSigner
exPortinG diaGraMs
Quite often, the process of designing which classes will be part of the system architecture is a part
of a much larger design or review process. Therefore, it is a common requirement to export the class
diagram for inclusion in reports.
You can export a class diagram either by right-clicking the context menu from any space on
the Class Designer or via the Class Diagram menu. Either way, selecting the Export Diagram as
Image menu item opens a dialog prompting you to select an image format and filename for saving
the diagram.
You can also copy and paste an image directly into Microsoft Word or a drawing program such as
Visio. To do this, you must first select one or more classes on the diagram.
Lastly, you can also print Class Diagrams directly from Visual Studio through the normal
File . Print menu option.
code Generation and refactorinG
One of the core goals of Visual Studio 2010 and the .NET Framework is to reduce the amount
of code that developers have to write. This goal is achieved in two ways: either reduce the total
amount of code that has to be written or reduce the amount that actually has to be written
manually. The first approach is supported through a very rich set of base classes included in
the .NET Framework. The second approach, reduce the amount of code that is written
manually, is supported by the code generation and refactoring tools included with the
Class Designer.
drag-and-drop code Generation
Almost every action performed on the class diagram results in a change in the underlying source
code, and essentially provides some level of code generation. We’ve already covered a number
of these changes, such as adding a property or method to a class in the Class Details window.
However, some more advanced code generation actions can be performed by manipulating the
class diagram.
As explained earlier in the chapter, you can use the Inheritance connector to establish an
inheritance relationship between a parent class and an inheriting class. When you do this, the code
file of the derived class is updated to reflect this change. However, when the parent class is abstract,
as in the case of the Product class in Figure 10-7, the Class Designer can perform some additional
analysis and code generation. If the parent class is an abstract class and contains any abstract
members, those members are automatically implemented in the inheriting classes. This is shown
in Figure 10-7 (right) where the abstract properties Description, Price, and SKU have been added
to the Book class. The method GetInventory() was not implemented because it was not marked
as abstract.
Code Generation and refactoring .
183
fiGure 10-7
The Inheritance connector can be used in one more way that results in automatic code generation.
In Figure 10-8 (left) an interface, ICrudActions, has been added to the diagram. When the
Inheritance connector is dragged from a class to the interface, all the members of the interface are
implemented on the class, as shown in Figure 10-8 (right).
fiGure 10-8
The following code shows the code that is automatically generated when the ICrudActions interface
is added to the Book class.
c#
#region ICrudActions Members
public Guid UniqueId
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
184 .
chaPter 10 modeling WiTh The clASS deSigner
public void Create()
{
throw new NotImplementedException();
}
public void Update()
{
throw new NotImplementedException();
}
public void Read()
{
throw new NotImplementedException();
}
public void Delete()
{
throw new NotImplementedException();
}
#endregion
Visual basic
#Region ICrudActions Members
Public Property UniqueId As Guid
Get
throw new NotImplementedException()
End Get
Set
throw new NotImplementedException()
End Set
End Property
Public Sub Create()
throw new NotImplementedException()
End Sub
Public Sub Update()
throw new NotImplementedException()
End Sub
Public Sub Read()
throw new NotImplementedException()
End Sub
Public Sub Delete()
throw new NotImplementedException()
End Sub
#End Region
intellisense code Generation
The rest of the code generation functions in the Class Designer are available under the somewhat
unintuitively named IntelliSense submenu. Because these code generation functions apply only to
Code Generation and refactoring .
185
classes, this menu is visible only when a class or abstract class has been selected on the diagram.
The two code generation functions included on this menu are Implement Abstract Class and
Override Members.
The Implement Abstract Class function ensures that all abstract members from the base class
are implemented in the inheriting class. To access
this function, right-click the inheriting class,
choose IntelliSense, and then choose Implement
Abstract Class.
Somewhat related is the Override Members
function, which is used to select public properties
or methods from a base class that you would like
to override. To access this function, right-click
the inheriting class, choose IntelliSense, and then
choose Override Members. The dialog box shown
in Figure 10-9 is displayed, populated with the base
classes and any properties or methods that have not
already been overridden.
refactoring with the class designer
In Chapter 8 you saw how Visual Studio 2010 provides support for refactoring code from the code
editor window. The Class Designer also exposes a number of these refactoring functions when
working with entities on a class diagram.
The refactoring functions in the Class Designer are available by right-clicking an entity, or any of its
members, and choosing an action from the Refactor submenu. The following refactoring functions
are available:
.
Rename Types and Type Members: Allows you to rename a type or a member of a type on
the class diagram or in the Properties window. Renaming a type or type member changes it
in all code locations where the old name appeared. You can even ensure that the change is
propagated to any comments or static strings.
.
Encapsulate Field: Enables you to quickly create a new property from an existing field, and
then seamlessly update your code with references to the new property.
.
Reorder or Remove Parameters (C# only): Enables you to change the order of method
parameters in types, or to remove a parameter from a method.
.
Extract Interface (C# only): You can extract the members of a type into a new interface.
This function allows you to select only a subset of the members that you want to extract
into the new interface.
fiGure 10-9
You can also use the standard Windows Cut, Copy, and Paste actions to copy
and move members between types.
186 .
chaPter 10 modeling WiTh The clASS deSigner
ModelinG Power toys for Visual studio
Although the Class Designer is a very useful tool for designing and visualizing a class hierarchy,
it can be cumbersome and unwieldy when trying to work with very large diagrams. To ease this
burden you can either break up the diagram into multiple class diagrams, or install the Modeling
Power Toys for Visual Studio 2010.
Modeling Power Toys is a free add-in to Visual Studio that extends the functionality of the Class
Designer in several ways. It includes enhancements that enable you to work more effectively with
large diagrams including panning and zooming, improved scrolling, and diagram search. It also
provides functions that address some of the limitations of the Class Designer such as the ability to
create nested types and new derived classes and display XML comments.
The add-in, including source code, is available from http://modeling.codeplex.com/.
The download includes an MSI file for easy installation.
Visualization enhancements
The Modeling Power Toys for Visual Studio 2010
provides some very useful enhancements for visualizing
and working with large class diagrams. The diagram
search feature is one of the more useful; it allows you
to search the entities on a diagram for a specific search
term. The search dialog, shown in Figure 10-10, is
invoked via the standard Find menu item or Ctrl+F
shortcut.
Another useful tool for large diagrams is the panning tool, which provides an easy way to see an
overview of the entire diagram and navigate to different areas without changing the zoom level. You
can invoke this tool by clicking a new icon that appears in the bottom right of the window, which
displays the panning window, as shown in Figure 10-11.
fiGure 10-10
fiGure 10-11
Modeling Power Toys for Visual studio .
187
The Modeling Power Toys also allows quite fine control over what is displayed on the diagram via
the filtering options. These are available via the Class Diagram menu, and include:
.
Hide Inheritance Lines: Hides all inheritance lines in the selection.
.
Show All Inheritance Lines: Shows all hidden inheritance lines on the diagram.
.
Show All Public Associations: Shows all possible public associations on the diagram.
.
Show All Associations: Shows all possible associations on the diagram.
.
Show Associations As Members: Shows all association lines as members.
.
Hide Private: Hides all private members.
.
Hide Private and Internal: Hides all private and/or internal members.
.
Show Only Public: Hides all members except for public; all hidden public members are
shown.
.
Show Only Public and Protected: Hides all members except for public and protected; hidden
public and/or protected members are shown.
.
Show All Members: Shows all hidden members.
functionality enhancements
Modeling Power Toys includes a number of enhancements that address some of the functional
limitations of the Class Designer. Though the Class Designer can display nested types, you cannot
create them using the design surface.
This constraint is addressed by the Modeling Power Toys by enabling you to add nested types
including classes, enumerations, structures, or delegates. You can also easily add several new
member types, such as read-only properties and indexers.
There are also some improvements around
working with interfaces. Often it is difficult to
understand what members of a class have been
used to implement an interface. The Modeling
Power Toys simplifies this by adding a Select
Members menu item to the interface lollipop
label on a type. For example, in Figure 10-12,
the Select Members command is being invoked
on the IStatus interface.
In addition to those mentioned here, many
other minor enhancements and functionality
improvements are provided by the Modeling
Power Toys that add up to make it a very
useful extension.
fiGure 10-12
188 .
chaPter 10 modeling WiTh The clASS deSigner
suMMary
This chapter focused on the Class Designer, one of the best tools built into Visual Studio 2010
for generating and understanding code. The design surface and supporting toolbars and
windows provide a rich user interface with which complex class hierarchies and associations
can be modeled and designed.
PART III
digging deeper
. chaPter 11: Unit Testing
. chaPter 12: Documentation with XML Comments
. chaPter 13: Code Consistency Tools
. chaPter 14: Code Generation with T4
. chaPter 15: Project and Item Templates
chaPter 16:. Language-Specific Features
11 11
Unit Testing
what’s in this chaPter?
.
Generating a test harness from existing code
.
Making assertions about the behavior of your code
.
Executing custom code during test life-cycle events
.
Creating data-driven tests
.
Testing private members and code contracts
.
Managing lists of tests
Application testing is one of the most important parts of writing software. Research into
the costs of software maintenance have revealed that a software defect can cost up to 25
times more to fix if it makes it to a production environment than if it had been caught during
development. At the same time, a lot of testing involves repetitive, dull, and error-prone work
that must be undertaken every time you make a change to your code base. The easiest way to
counter this is to produce repeatable automated tests that can be executed by a computer on
demand. This chapter looks at a specific type of automated testing that focuses on individual
components, or units, of a system. Having a suite of automated unit tests gives you the power
to verify that your individual components all work as specified even after making radical
changes to them.
Visual Studio 2010 has a built-in framework for authoring, executing, and reporting on test
cases. Originally included only in the Team System Edition of Visual Studio, many of the
testing tools are now available in the Professional Edition. This means a much wider audience
can now more easily obtain the benefits of automated, robust testing. This chapter focuses on
creating, configuring, running, and managing a suite of unit tests as well as adding support to
drive the tests from a set of data.
192 .
chaPter 11 uniT TeSTing
your first test case
Writing test cases is not a task that is easily automated, because the test cases have to mirror the
functionality of the software being developed. However, at several steps in the process code stubs
can be generated by a tool. To illustrate this, start with a fairly straightforward snippet of code to
learn to write test cases that fully exercise the code. Setting the scene is a Subscription class with