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 WindowBy 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.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 DiagramsThe 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.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
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.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.
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 RelationshipsA 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 RelationshipsA 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 RelationshipsIn 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 RelationshipsA 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.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. |