Skip to main content

skip to main content

developerWorks  >  Rational  >

Modeling C# applications using Rational Modeling Extension for .NET, Part 1: Modeling using the C# profile and type library

developerWorks
Document options

Document options requiring JavaScript are not displayed


Rate this page

Help us improve this content


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:

  1. Create an empty UML model.
  2. Import the C# primitive types library.
  3. 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
New UML Project wizard screen capture

Step 2. Import the profile and type library

  1. Now, in the Modeling perspective, select the model from the Project Explorer.
  2. Right-click, and choose Import model library from the pop-up menu (Figure 2).

Figure 2. Pop-up menu for importing model libraries
Pop-up menu for importing model libraries screen capture

  1. Choose C# Primitive Types from the Deployed Library list (Figure 3).

Figure 3. Importing the C# primitive types library
Importing the C#  primitive types library screen capture

Step 3. Apply a C# profile

  1. To apply a C# profile, select the model in the Project Explorer.
  2. In the Properties view, select the Profiles tab and click Add profile .
  3. Choose C# profile from the Deployed Profile list. (See Figure 4.)

Figure 4. Applying a C# profile to a model
Applying a C# profile to a model screen capture



Back to top


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
Modeling a C# class screen capture

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 a C# struct screen capture

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)
A C# delegate: bool handleResize(Point start,Point end) screen capture

To model the sample delegate:

  1. First, create a UML class and name it handleResize.
  2. Now apply the <<CSharp Delegate>> stereotype to it.
  3. Add a UML operation, and name it handleResize.
  4. Now select the newly added operation and, from Properties view, go to the Parameters tab and insert a new parameter.
  5. Name this parameter start and set its direction as In ,and then from Browse type dialog, select Point struct as its type.
  6. Add another parameter using the same process, and name it end.
  7. 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
Selecting a type from the imported C# primitive types library screen capture

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:

  1. First create a UML enumeration, and name it Colors.
  2. 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.
  3. Add an enumeration literal of RED, and set its value to 10.
  4. 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
A C# enum: Color with RED, BLUE, and GREEN literals screen capture

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:

  1. Create a UML attribute of X and, from the C# primitive type library, set its type to int as described previously.
  2. 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.
  3. 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.
  4. Set the default value to 0.
  5. 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
struct Point with fields X and Y and constants ORIGIN_X and ORIGIN_Y screen capture

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:

  1. Right-click a UML operation, and then go to the UML Properties menu.
  2. 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.

  1. Create two UML classes, DrawingSurface and GraphicException, to be used as a parameter type and exception type, respectively, for the sample method.
  2. 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.
  3. Add a parameter named surface to the operation, and set its type to DrawingSurface.
  4. In the Project Explorer, right-click the UML operation, and then select the UML Properties menu.
  5. Go to the RaisedException section, and click Insert to insert a new exception type.
  6. 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
C# method display with return type bool screen capture


Figure 12. Adding exception type to method display()
Adding exception type to method display() screen capture



Back to top


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

Get products and technologies

Discuss


About the author

Rajeshwari Rajendra

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


Please take a moment to complete this form to help us better serve you.



 


 


Not
useful
Extremely
useful
 


Share this....

digg Digg this story del.icio.us del.icio.us Slashdot Slashdot it!



Back to top