 | Level: Intermediate Rajeshwari Rajendra (rajeshwari.r@in.ibm.com), Software Engineer, IBM India
16 Oct 2007 Learn how to model Microsoft® .NET C# applications by using
IBM® Rational® Modeling Extension for Microsoft® .NET, which is available in
IBM® Rational's® software modeling tools. This article assumes that you are familiar with basic
UML modeling concepts and using Rational modeling tools.
IBM Rational Modeling Extension for Microsoft .NET
enables .NET application developers to use Rational modeling tools in designing
applications. This extension is available in IBM® Rational® Software
Modeler, IBM® Rational® Systems Developer, and IBM® Rational
Software Architect, and it includes support for modeling C# applications. This
article, the first part in a series, describes modeling C# constructs and concepts
using the C# profile and type library available in this extension. The examples
here use that library to model these elements:
- Types
- Classes
- Structures (struct)
- Delegates
- Enumerations (enum)
- Fields
- Methods
To get started with modeling C# applications, you need to use the C# profile and
type library provided by Rational Modeling Extension for .NET. To do this, you
will follow these basic steps, which the following sections explain in more
detail:
- Create an empty UML model.
- Import the C# primitive types library.
- Apply the C# profile.
The C# primitive types library provides the basic primitive data types that are
available in the C# language, such as long, byte, short, and
so forth. The C# profile consists of various stereotypes that are applicable to
UML classes, attributes, operations, and so on. By using those stereotypes, you
can design C# constructs. Examples of stereotypes available in the profile are
<<CSharpIndexer>> and
<<CSharpDelegate>>.
Note:
General UML concepts, such as generalization, realization, and
visibility, have the same meaning and are not explained in this article. However,
Package visibility in UML maps to internal visibility in C#.
Step 1. Create a new
model
Note:
The examples used here are only to illustrate the concepts and
may not represent a real design for any application.
Start by designing a simple graphics library: To do this, create an empty model
in a new UML project by using by the New UML Project wizard (Figure 1).
Figure 1. New UML Project wizard
Step 2. Import the profile and type library
- Now, in the Modeling perspective, select the model from the Project
Explorer.
- Right-click, and choose Import model library from the pop-up menu
(Figure 2).
Figure 2. Pop-up menu for importing model libraries
- Choose C# Primitive Types from the Deployed Library list
(Figure 3).
Figure 3. Importing the C# primitive types library
Step 3. Apply a C# profile
- To apply a C# profile, select the model in the Project Explorer.
- In the Properties view, select the Profiles tab and click
Add profile .
- Choose C# profile from the Deployed Profile list. (See Figure
4.)
Figure 4. Applying a C# profile to a model
How to model various types
You can use Rational modeling extension to model various types, including
classes, structs, delegates, and enumerations, among others. The following
subsections describe the basic processes.
Modeling classes and interfaces
A C# sharp class is modeled simply as a UML class. However, to use C#-specific
modifiers such as new, protected internal, static, and
unsafe, you need to apply the
<<CSharp class>> stereotype
and set its property values. The example in Figure 5 uses a GraphicObject class
with <<CSharp Class>> applied, just for the sake of
illustration. Because we are not using any of the stereotype properties, we leave
them at default values.
Note:
Use a stereotype from a C# profile only when you set at least
one of the stereotype property values.
Figure 5. Modeling a C# class
Similarly, a C# interface is modeled as a UML interface. Also, you can use
a <<CSharp
Interface>>
stereotype if you need to use specific modifiers, such as
new, protected
internal,
and unsafe.
Modeling structures
A C# structure (struct) is modeled as a UML class with the
<<CSharp
Struct>>
stereotype applied. C#-specific modifiers, such as
new,
protected internal, and
unsafe are available in the
<<CSharp
Struct>>
as Boolean stereotype properties. The example in Figure 6 models a C# point
structure with
<<CSharp
Struct>>
applied.
Figure 6. Modeling a C# struct
Modeling delegates
A C# delegate is modeled as UML class with the
<<CSharp Delegate>> applied
to it. As with the struct type, C#-specific modifiers such
new,
protected internal, and
unsafe are available as Boolean string properties. A
UML class stereotyped as
<<CSharp Delegate>> should
not contain any attributes.
Also, in such a UML class, there should be a single UML operation with the same
name as the delegate class. The signature of this operation should be same as the
signature that would declared for its corresponding C# delegate. For the example
in Figure 7, we modeled a C# delegate called
<<handleResize>> by
applying a
<<CSharp Delegate>>
stereotype to a UML class. This class has a UML operation named
<<handleResize>> that
takes two parameters of the Point type and returns a C# bool type.
Figure 7. A C# delegate: bool handleResize(Point start,Point end)
To model the sample delegate:
- First, create a UML class and name it
handleResize.
- Now apply the
<<CSharp Delegate>>
stereotype to it.
- Add a UML operation, and name it
handleResize.
- Now select the newly added operation and, from Properties view, go to the
Parameters tab and insert a new parameter.
- Name this parameter start and set its
direction as In ,and then from Browse type dialog, select Point struct as its type.
- Add another parameter using the same process, and name it
end.
- Add another parameter, and set its direction as Return. Set its type as bool by
browsing for the types from the imported CSharpPrimitiveTypes library.
Figure 8. Selecting a type from the imported C# primitive types library
Modeling enumerations
A C# enumeration (enum) is modeled as a UML enumeration. A <<CSharp Enum
>> stereotype can be applied whenever you need to
set the new or protected internal modifiers. This stereotype also
allows you to specify an enumeration base type that is different from the implicit
default. To do that, you need to set the stereotype implementation_type property in
<<CSharp Enum >> to one of the listed enumeration
base types, such as int, byte, or short. Optionally, the enumeration literals can
have default integral values that represent the constant of the
corresponding C# enumeration literal.
To model the sample enumeration:
- First create a UML enumeration, and name it
Colors.
- Apply the <<CSharp Enum >> stereotype to it so that you can
specify a different base type, such as short. The stereotype need not be
applied if you do not want to set any of
the stereotype properties.
- Add an enumeration literal of RED, and set its value to 10.
- Similarly add enumeration literals BLUE and GREEN, with values
20 and 30, respectively.
Figure 9. A C# enum: Color with RED, BLUE, and GREEN literals
Modeling fields
A C# field is modeled as a UML property, thus either as a UML attribute or
as an association end. Additionally <<CSharp Field >> stereotype can
be applied whenever you need to set the new, protected internal, unsafe,volatile, and const
modifiers. Other modifiers, such as static and readonly are
available in the general properties section of a UML property.
A C# constant is modeled as a
UML property with a <<CSharp Field >> stereotype
and setting the Boolean stereotype property constant to true.
For this example, model sample fields and constants in the Point
structure:
- Create a UML attribute of X and, from the C#
primitive type library, set its type to int as described previously.
- Similarly, create another UML attribute of Y and set its type to
int. This makes the two UML attributes X and Y, the C# fields.
- To create a C# constant, add a UML ORIGIN_X attribute of type
int, apply the <<CSharp Field >> stereotype, and set the
stereotype property to constant=true.
- Set the
default value to 0.
- Add another ORIGIN_Y constant of type int with
a default value of 0, apply the same stereotype, and set the property
to constant=true. Now, both ORIGIN_X and ORIGIN_Y represent C#
constants. (See Figure 10.)
Figure 10. struct Point with fields X and Y and constants ORIGIN_X and ORIGIN_Y
Modeling methods
A C# method is modeled as a UML operation with an optional <<CSharp
Method>> stereotype. You need to apply the
stereotype only if you want to set the modifiers, such as new, override, extern,
virtual, protected internal, and unsafe. You can set other
modifiers, such as abstract and
static, in the general Properties section of a UML operation. If
you need to set the sealed modifier on the method, select (enable) the
Leaf property of the operation.
A void method is modeled as one
with no return type. Set exceptions raised by a method this way:
- Right-click a UML operation, and then go to the UML Properties menu.
- Select the
Raised Exceptions section, and insert the various exceptions.
For this example, model a simple C# method named displaywith a bool return type
and a GraphicException. The method will take a parameter type of
DrawingSurface.
- Create two UML classes,
DrawingSurface and GraphicException, to be used
as a parameter type and exception type, respectively, for the sample method.
- Create a public UML operation named
display in the
Point class, and, from the C# primitive type
library, set its return type to bool as described previously.
- Add a parameter named
surface to the
operation, and set its type
to DrawingSurface.
- In the Project Explorer, right-click the UML operation, and then select the
UML
Properties menu.
- Go to the RaisedException section, and
click Insert to insert a new exception type.
- Select the
GraphicException class as the exception to add. (See Figure 11 and
Figure 12.)
Figure 11. C# method display() with return type bool and parameter of type DrawingSurface
Figure 12. Adding exception type to method display()
What's next
This article described how to use the C# profile and type library in
Rational Modeling Extension for Microsoft .NET. After completing these exercises, you
should be able to create simple models containing C# classes, structs, delegates,
interfaces, enums , fields, and methods. Part 2 of this series explains modeling
C# indexers, properties, constructors, destructors, and operators.
Resources Learn
-
Modeling C# applications using Rational Modeling Extension for .NET, Part 2
by Rajeshwari Rajendra, is the second article in this two-part series. (IBM®
developerWorks®, November 2007).
- Visit the
IBM Rational Modeling Extension for Microsoft .NET
area on developerWorks for technical documentation, how-to articles, education,
downloads, and product information.
- Visit the
Rational Software Architect
area on developerWorks for technical documentation, how-to articles, education,
downloads, and product information.
- Subscribe to the
developerWorks Rational zone newsletter.
Keep up with developerWorks Rational content. Every other week, you'll
receive updates on the latest technical resources and best practices for the
Rational Software Delivery Platform.
- Browse the
technology bookstore
for books on these and other technical topics.
Get products and technologies
Discuss
About the author  | 
|  | Rajeshwari Rajendra is a software engineer in the IBM India Software Lab, working on the Rational Systems Developer team. Her expertise includes XML query technologies, such as XPath and XQuery, and UML modeling and transformations (specifically, C# and C++). She has a Master's degree (MCA) from Bangalore University, India. |
Rate this page
|  |