Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

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

Rajeshwari Rajendra (rajeshwari.r@in.ibm.com), Software Engineer, IBM India
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.

Summary:  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.

Date:  16 Oct 2007
Level:  Intermediate
Also available in:   Chinese

Activity:  4675 views
Comments:  

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


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


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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational
ArticleID=260848
ArticleTitle=Modeling C# applications using Rational Modeling Extension for .NET, Part 1: Modeling using the C# profile and type library
publish-date=10162007
author1-email=rajeshwari.r@in.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers