Understanding Your Application with Model View

C#Builder has a Model View, which is a visual layout of the objects in a project. This visual layout is presented in the form of a Unified Modeling Language (UML) diagram, which is a ubiquitous standard for modeling and design throughout the world today. The precise diagram type is a UML static structure diagram. At a glance, you can get the gist of how a program works by looking at the Model View.

To understand the benefit of the Model View, consider the case where you have to look at a new piece of code for the first time or come back to your own code after a period of weeks. Sometimes it is a daunting task, especially if you don't have documentation. Because the Model View creates a high-level view of the program by laying out all the objects in a diagram, along with lines that show relationships between the objects, you will be able to see how the program is constructed. This high-level view will let you understand how the program works so that you can quickly begin the task you need to perform.

Accessing Models through the Model View Window

By default, C#Builder is installed with the Model View window located in the same tab group as the Project Manager and Data Explorer. The Model View window is where you locate packages and types to display in the UML diagram. There is a tree view within the Model View window that organizes models by project, namespace, and type in a hierarchical fashion. Figure 5.16 shows the Model View window and identifies various elements that you should be aware of.

Figure 5.16. The Model View window.

graphics/05fig16.jpg

Referring to the callouts in Figure 5.16, we're interested in the ModelView project. C#Builder creates projects with the same project name and namespace name, which is why the same name appears at different levels. Namespaces in C# correspond to packages in the diagram.

Type Details outline each type in the project. Both Package Details and Type Details have branches for navigating to their respective diagrams.

Viewing UML Diagrams

The easiest way to work with the information in this section is by getting the code that comes with this book and loading the project. When the project is loaded, double-click on the Package Diagram icon in the Model View window, shown in Figure 5.16, which will display the Package Diagram in Figure 5.17.

Figure 5.17. The Model View window with a Package Diagram.

graphics/05fig17.jpg

The diagram objects in Figure 5.17 show UML packages, which correspond to C# namespaces. In the code, there are namespaces named ModelView and AnotherNamespace. Also, notice that ModelView contains a Package item in its definition. I created that by coding a nested namespace, as follows:

 namespace ModelView.SubNamespace 

NAMESPACES AND PACKAGES

A UML package in Model View is represented by a C# namespace, which is a logical entity that may traverse multiple files. This means that the packages you see in Model View will not necessarily correspond to physical files, unless the program is written in such a way that you have explicitly limited a namespace to a single file. Because each new file added to a project is assigned a default namespace, you would have to explicitly modify files to achieve this. I've personally found it easier to work with the default behavior. Because there will be situations when you'll want to use Model View to look at code that was written by someone else, it may be helpful to recognize that the packages being viewed in the UML diagram are purely logical entities.

This nested namespace puts the WinForm class in WinForm.cs into the SubNamespace namespace.

Viewing a Package Diagram is interesting from a high-level perspective, but the Type Diagram shows how the code works and illustrates the interaction between objects. Selecting the MyClass element in the Model View window and choosing Show Element in Diagram will display the diagram in Figure 5.18.

Figure 5.18. The Model View window with Type Diagram and relationships.

graphics/05fig18.gif

Looking at the MyClass object in the Type Diagram shown in Figure 5.18, you'll see categories named Attributes, Operations, Properties, and Classes. The names of these categories are common UML descriptions for the contents of objects. Table 5.2 maps the UML object member category names to C# type members that fall into those categories.

Table 5.2. Mapping of UML Object Member Categories to C# Type Members

UML OBJECT MEMBER CATEGORY NAME

C# TYPE MEMBERS

Attributes

Fields

Operations

Constructors, destructors, and methods

Properties

Events and properties

Classes

Nested classes

In addition to the type members shown in Figure 5.18, there are lines in the diagram that connect and demonstrate the relationships between the objects. Applicable relationships include Generalization (Implementation Inheritance), Realization (Interface Inheritance), and Association (Composition Reference). Even though the help files discuss Dependency (Return Value or Parameter Reference), this version of C#Builder does not support UML Dependency relationships in Model View, and Borland has stated that it is an error in the documentation. The following sections detail how these relationships are coded.

Implementing Generalization Relationships

A generalization relationship in Model View is the same as C# inheritance. In the file named MyClass.cs, you'll see the following class declaration where the MyClass class inherits BaseClass:

 public class MyClass : BaseClass // rest of line elided for clarity 

This will create a generalization relationship between MyClass and BaseClass, which is the solid line with a closed arrow that connects the MyClass object to the BaseClass object in Figure 5.18.

Implementing Realization Relationships

A realization relationship in Model View is the same as C# interface inheritance. The full declaration for MyClass, from the MyClass.cs file, is shown in the following line:

 public class MyClass : BaseClass, MyInterface 

This creates a realization relationship between MyClass and MyInterface. Now MyClass has two relationships: generalization with BaseClass and realization with MyInterface. The Realization relationship appears as a closed arrow with a dashed line connecting the MyClass object to the MyInterface object in Figure 5.18. On the computer screen the Realization relationship appears as a blue/green color, which can't be seen in the black-and-white picture in Figure 5.18 of this book.

Implementing Association Relationships

In a Model View association, one C# class contains a reference to another class. The following field in the MyClass class, in the MyClass.cs file, creates a reference from MyClass to AssociatedClass:

 AssociatedClass associatedRef = new AssociatedClass(); 

This creates an association relationship between MyClass and AssociatedClass. The association is illustrated by the line drawn from the MyClass object to the AssociatedClass object in Figure 5.18.

Implementing Dependency Relationships

A dependency is the return type or a parameter of a C# method that are custom types. The following method in the MyClass class references the DependencyClass class as its parameter:

 public void DependencyMethod(DependencyClass dependency) {} 

This will create a dependency relationship between MyClass and DependencyClass. As stated earlier, the dependency relationship will not display in Figure 5.18 because it hasn't been implemented in this version of C#Builder. If dependencies were supported in Model View, there would be an open arrow with a dashed line from the MyClass object to the DependencyClass object in Figure 5.18.

When you are coding and a type diagram is first forming, shapes appear all over the place, wherever they fit. This gets a little messy, but fortunately there is a way to clean up the screen when you're done adding types. Selecting Do Full Layout from the context menu of the diagram screen will rearrange all the shapes on the screen so that they make sense and are easier to view.

The Overview window provides a view of all objects and allows you to navigate quickly throughout the diagram. Figure 5.19 shows the Overview window, which you can open by clicking the button on the lower-right corner of the diagram.

Figure 5.19. The Overview window.

graphics/05fig19.jpg

As shown in Figure 5.19, the Overview window contains a smaller rectangle that is a lens into the entire diagram. If you hold the mouse down on this window and drag it, the viewable area of the diagram changes with the mouse movement, providing a quick way to navigate a diagram.



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net