
Class Designer
• Often times, developers and testers need to “visualize” the types that make up the whole or parts of a
given project.
- Developers can visually create the five standard .NET types (class, structure, delegate, enumeration and
interface) with their members (fields, properties, methods, events, etc.).
- Testers and new team developers can immediately visualize and understand the software so they can
start working on it sooner.
- By seeing the types, they can visualize or create the cohesive (highly focused) types.
- This can also help them understand the relationships between the types, striving for low coupling
(fewer connections/dependencies).
• Some developers prefer to create types visually rather than by typing them in. Which tools should be
considered?
- One of the most popular ways to visualize types is by using the Unified Modeling Language (UML).
- Microsoft Visio supports all the different types of UML diagrams, however, is not yet UML 2.0
compliant.
- Visio was released with the team editions of Visual Studio 2005.
- Third party tools (example: Enterprise Architect from Sparx Systems) can also be considered.
- Visual Studio 2005 Standard (and above) now includes a new Class Designer.
• You can create one or Class Diagrams (*.cd) with the Class Designer.
- Class Diagrams can be created by clicking on the Class Diagram icon at the top of the Solution Explorer.
- This will automatically create a class diagram for the class file that is highlighted. In the picture above,
notice that OrderItem.cs is selected.
- After we click the “View Class Diagram” icon, ClassDiagram1.cd was created with the OrderItem class
being added automatically.
• ClassDiagram1.cd is really just an XML file. Here is the contents of the one we just created:
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1">
<Font Name="Segoe UI" Size="9" />
<Struct Name="OrderSystem.OrderItem" Collapsed="true">
<Position X="0.5" Y="0.5" Width="1.5" />
<TypeIdentifier>
<FileName>OrderItem.cs</FileName>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAASAIAAAAAA=</HashCode>
</TypeIdentifier>
</Struct>
</ClassDiagram>
- Notice how it tracks the view (collapsed) of our type, it’s X and Y coordinates and the file’s name,
where it resides.
- Mind you, our diagram was extremely simple so this XML file is quite short also.
- The initial (default) view of our shape(s) in the diagram is collapsed. We can expand the types to see
the list of their members by clicking on the chevron button on the types.
- This class diagram is showing the OrderItem structure. It has four internal(C#) / Friend(VB) fields,
denoted as such by the envelope icon.
- Changes that are made to the source code are immediately reflected in the diagram. Likewise, changed to
the diagram are immediately reflected in the code. There is no “round-tripping” step; it is live!
• We can add new types to our diagram by using the Toolbox.
- Pre-existing types can be added by dragging them from the Solution Explorer (their files) or the Class
Viewer (their types).
• The Class Designer is not a full blown UML editor, supporting all UML features.
- In the Toolbox, notice we can also set up Inheritance (Specialization) between the types. Association
can be used as an over-simplified form of Composition.
- Many of the staples of UML design are missing from the Class Designer (such as multiplicity). It is
assumed that more UML support will be added in future versions.
- If you need more specialized diagrams, consider the tools we mentioned at the beginning of the chapter.
• The Association prototype can be used to support a form of Composition.
- In the following diagram, we’ve set up an Association between the Customer and Order types.
- Notice the direction of the arrow. Also notice that the Order object is now considered a part of the
Customer class as a public property.
• The empty skeletal property, Order, was automatically generated in the Customer class.
public Order Order
{
get
{
throw new System.NotImplementedException();
}
set
{
}
}
- It is assumed that you will write code that returns an instance on an Order type. Alternatively, you
could convert this property into an indexer.
• What if you don’t want to have all your types on ONE diagram?
- By default, Visual Studio will continue adding all the types to the same diagram.
- For example, if you click on a different file and then click on the View Class Diagram button again, the
type will be added to the same class diagram.
- Solution: right-click the project in your Solution Explorer and choose Add New Item. Select the last
item in the huge list – Class Diagram!
• We can modify the members of a given type by using the Class Details window.
- Not only can we add members (Methods, Properties, Fields and Events) to our types, with can also
add parameters with their scope modifiers, including a brief summary. Take advantage of the shortcuts down
the left side.
- Note the Properties window. We can set properties on the types, their members and their parameters.
In this case, we’re looking at the properties of the Order class.
Code Analysis
• Often times, developers must follow a set list of coding standards developed internally at the company.
- This is typically created in the form of a Word document, which is emailed to the developers.
- From here it is often pinned to a cube wall, filed in a drawer, flipped over to draw out software designs,
used to clean up spilt coffee, recycled or shredded.
• The main point is, in most cases - it will not be followed!
- The coding standards will not be enforced without someone constantly checking and correcting the
source code as its being written. This would be ridiculous.
- No one wants to work with anyone constantly checking to see if their coding is up to snuff. It’s
judgmental and, because of the human element involved in enforcing the rules, standards will be missed or
abused.
- Code Analysis in Visual Studio 2005 can do this checking for you without other humans involved.
• Code Analysis will tell you, the developer, how your code can be improved to follow programming
guidelines.
- These guidelines can be preset to follow the guidelines your developers share at your company.
- The included guidelines also suggest better ways of writing code. Better in ways of security, clarity,
performance, platform portability and globalization.
• The Code Analysis feature in Visual Studio 2005 is based on the free code analysis tool, FxCop, which
you can download from: http://www.gotdotnet.com/team/fxcop/
- If you are not using the Team Developer edition or Team Suite edition of Visual Studio 2005, you can
use this utility instead to analyze your code.
• By default, Code Analysis in Team Developer is not turned on.
- If you’d like to run Code Analysis just once, right-click the project and click “Run Code Analysis”.
- If you want it to consistently perform Code Analysis, turn it on by:
Right-click the project in the Solution Explorer window and choose Properties.
Go to the Code Analysis tab and check “Enable Code Analysis”
- Notice that all problems found will be reported as Warnings. Visual Studio’s reaction to the build is
configurable, as we’ll see later.
• After turning on Code Analysis, we can rebuild the solution.
- At first glance it appears that were zero errors. Yee-ha!!
- Wait - look at the warnings. In the following example, there are 17 warnings! @#%&* How dare this
piece of junk insult my coding ability?!?
• After getting past the consideration of punching the screen or sending nasty-grams to the Microsoft
Code Analysis team, let’s look at the details of the errors.
• When I double-click the first warning, it brings me right to the problem:
private List<OrderItem> Items;
- As the warning suggests, I should initialize this field with some value, even if it’s null. The next four
warnings are for the same issue. Okay – makes sense. What’s next?
• Warning #6 tells me I need to declare the minimum security that my application requires for it to run.
- Not knowing how to do that, I select that warning and click F1 to read about it in MSDN help.
- I find out that I need to add an assembly level attribute to AssemblyInfo.cs that will request minimum
permissions, like this:
[assembly: SecurityPermission(
SecurityAction.RequestMinimum, Execution = true)]
- That’s fixed – what next?
• Warning #7 tells me that I have to sign the assembly. I’ll do that when it is done, or beforehand if I need
to test it in the GAC.
• Warning #8 tells me to mark the assembly as “CLS Compliant”. As long as I’m not adding my own
custom intrinsic types, adding this assembly-level attribute should not be a problem.
• Warnings #9 and #10 are warning me that I never instantiate the Customer class and I haven’t added any
code to the Order property that was added when we created the Association. That’s okay – I’m not done yet.
• Warnings #12-15 want me to rename my all uses of “ID” to “Id”.
- Notice that these warnings are grouped under Microsoft.Warning and are labeled CA1706 and CA1709.
- Hmph! I like having two capital letters for ID so maybe we should change this Name Rule.
• To turn off a rule, go to the Project Properties window – Code Analysis tab and expand “Naming
Rules”.
- Here, we can check/uncheck the rules we want Code Analysis to use. We have total control!
- QUESTION: Should we uncheck CA1706 and CA1709 so that we can keep using ID? No… there’s a
better solution.
- We want to continue this method of code analysis for all other declarations.
• You can suppress warnings that you don’t want to see in the Warnings window.
- When you rebuild the project, those warnings will not reappear.
• By suppressing the warnings, you’re really adding attributes to your source code. Deleting the
attributes brings back the warnings.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:
IdentifiersShouldBeCasedCorrectly", MessageId = "0#"), System.Diagnostics.CodeAnalysis.
SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", MessageId = "0#")]
public OrderItem GetOrderItem(int OrderItemID)
{
if (Items.Count > 0)
if (OrderItemID <= Items.Count)
return Items[OrderItemID];
return new OrderItem();
}
• The last two warnings inform me that I should probably override the Equals method and overload the
“==” and “!=” operators.
- This is only important if I’m going to be comparing instances of the objects with each other.
• If you decide to turn on Code Analysis for a large project you’ve previously created, you may get the
following build message:
CA0503: Additional warnings cannot be displayed.
By default, a maximum of 200 warnings are displayed in the Error List. You can increase this amount by
modifying the following registry value:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.
0\Setup\EDev\CodeAnalysisErrorListViolationLimit
- Basically, you can change the default number of warnings to be displayed from 200 to anything you
want. Unfortunately, changing the registry requires administrative privileges.
• Code Analysis is one of the ways for a developer to privately verify the quality of their code before
they add it to Source Control Management.
VSTS for Software Developers
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials