西西河

主题:【评论】闲谈VS2005的Class Desinger -- Highway

共:💬4 新:
全看分页树展 · 主题
家园 【评论】闲谈VS2005的Class Desinger

记得在读书的时候,一个中国女孩子的毕业论文就是讨论如何使用Java来实现Software Engineer中的几个主要原则(Principles)。她的演示程序就是用Java写的一个Program。这个程序让用户输入Class的property和function名称等等,程序自动生成UML Diagram和源程序。当然了,这个程序功能很有限,你不能保存UML Diagram,也不能从Byte code甚至是源程序中自动分析classes中的关系,反向生成UML Diagram.......Anyway,这个女孩子拿到了学位。

随着Java的不断成熟,以Java技术为基础的UML多起来了,功能也越来越丰富。但是在这个领域,微软一直比较落后。直到VS2005我们才看到了一个比较成形的Class Desinger方案。不管怎么样,这是一个好消息。

下面就是这个Class Desinger的一个简介,有兴趣的朋友可以看看。

===============================================

[SIZE=4]The Class Designer[/SIZE]

Most object-oriented developers like to create diagrams of their application designs and often these take the form of Unified Modeling Language (UML) documents. UML defines several types of diagrams you can use to model your application. The most commonly used diagram type is the class diagram, which shows each class in your application, along with their relationships to other classes. Typically, each class is displayed in a box with its fields and methods listed.

点看全图

外链图片需谨慎,可能会被源头改

Unfortunately, UML has fallen behind current technologies such as the .NET Framework. For instance, there is no provision in UML for the Friend and internal scope in Visual Basic® .NET and C#. Nor is there any elegant way to list property methods or events, which are key parts of the interface for .NET classes.

It is difficult to keep code and diagrams in sync. Some UML diagramming tools allow code generation from a diagram and diagram generation from code, but there are limitations. Therefore, most developers avoid the use of code generation, which means they must manually keep the diagrams and code in sync over time.

To help address these problems, Microsoft is developing a Class Designer tool which is scheduled to ship as part of Visual Studio 2005. The Class Designer produces diagrams similar to a UML class diagram, with the addition of features to support all the design concepts available to programmers using .NET. Additionally, the Class Designer is fully integrated into Visual Studio 2005, so the diagrams it creates are part of your project along with your code.

The integration with Visual Studio 2005 is not only convenient, it means that the Class Designer can do real-time code generation and generating diagrams from code. Put simply, the diagram is instantly updated as you change the code, and your code is instantly updated as you change the diagram.

The following figure shows the Class Diagram tool in Visual Studio 2005, along with the matching code that was generated for the diagram. Changing either the diagram or the code is fine. Both remain in sync automatically.

点看全图

外链图片需谨慎,可能会被源头改

Figure 7

Using the Class Designer requires that you add a Class Diagram to your project in Visual Studio 2005. You do this by adding a new item to the project and choosing the Class Diagram template.

If you already have classes in the project, they can be added to the diagram by dragging them from the Class View window onto the diagram's designer surface. This will cause the Class Designer to add the class to the diagram, and list all the methods, fields, properties, and events in the class. An example of such a class is shown in Figure 8. Note that not only are the details of the class shown in the diagram itself, but when the class is selected in the diagram, the details also appear in the Class Details window at the bottom of the display.

点看全图

外链图片需谨慎,可能会被源头改

Figure 8

Using the Class Details window you can view and change the interface of the class. This includes adding and removing methods, fields, properties, and events. You can also change the scope, parameters, and return types of these elements. Any change made in the Class Details window is immediately applied to the code.

Notice the Summary column in the Class Details window. This column displays the summary information about the interface element from the XML documentation in the code. You can also change the XML documentation in the code by editing this column. Better still, when you select this column, a button is available to bring up a dialog where you can edit more of the XML documentation.

The resulting Visual Basic .NET comments are automatically added to the code, as shown in the following code snippet.

''' <summary>
''' Returns the ID value of the product.
''' </summary>
Public ReadOnly Property ID() As Guid
  Get
    Return mID
  End Get
End Property

The same is true for C#, C++, or J# code (though there are some restrictions for C++). Changes to the diagram are immediately reflected in the code of your project; likewise, changes to your code are reflected in the diagram. Suppose, for example, you start with the C# code in Figure 9. Dragging this class from the Class View to the Class Diagram produces the illustration shown in Figure 10.

点看全图

外链图片需谨慎,可能会被源头改

Figure 10

Not only can you drag and drop your classes, but you can drag and drop classes from other assemblies referenced by your assembly. When you do this, the external classes are shown in the diagram in a read-only mode since they aren't directly part of the current assembly. This allows you to provide a more comprehensive diagram that illustrates the relationships between your classes and the classes from referenced assemblies.

You will also be able to drag and drop an entire project from Solution Explorer onto the diagram if you want all the items in the assembly displayed in the diagram. Initially there won't be any auto-formatting provided for the newly added elements in the diagram, but hopefully that feature will be added prior to the release of the tool.

To illustrate how the diagram is updated when the code changes, you can add the following property to the class:

int _id;

public int ID
{
  get { return _id; }
  set { _id = value; }
}

The diagram is immediately updated to display the newly added ID property and field.

It is also possible to start with the diagram, rather than with code. When the Class Diagram Designer is open, you can drag and drop elements from the Toolbox onto the diagram. This automatically creates the associated code file and adds the item to the diagram. Using the Toolbox, you can add most common coding constructs, including classes (both abstract and concrete), enumerations, interfaces, structures, delegate definitions, and Visual Basic .NET modules.

You can also use the Toolbox to add inheritance and association relationships between elements on the diagram. These relationships are immediately reflected in the code. For instance, if you click the Association tool, then click and drag from a Product class to an InventoryQOH class, the following code is generated in the Product class file:

Public Property InventoryQOH() As Inventory.InventoryQOH
  Get

  End Get
  Set(ByVal Value As Inventory.InventoryQOH)

  End Set
End Property

This new InventoryQOH property does not appear as a property in the Product class because it is displayed graphically as an association line between the Product and InventoryQOH classes in the diagram. Likewise, if you add an Inheritance relationship between two classes, the appropriate inheritance relationship is added to the code.

From the Toolbox you can also add arbitrary comments to the diagram itself. These comments are only part of the diagram and have no impact on the code in the project. These are different from the XML documentation associated with each code element in the diagram, which are part of both the diagram and the code.

The Class Diagram file in a project has a .cd file extension. It is a simple XML file that contains details about each element in the diagram. Since most of the data in the diagram flows directly from your code, there is not a lot of information stored in the XML file itself. For instance, the Product class shown earlier has the XML shown in Figure 11.

Figure 11 XML File for the Class Diagram

<Class>
  <AbsoluteBounds X="0.5" Y="0.5" 
   Height="1.794798172314962" Width="1.5" />
  <ShowAsAssociation>
    <Property Name="InventoryQOH" />
  </ShowAsAssociation>
  <TypeIdentifier>
    <FullName>Inventory.Product</FullName>
    <FileName>
         C:\Documents and Settings\Administrator\My Documents\Visual 
         Studio\Projects\Inventory\Inventory\Product.vb</FileName>
    <SiblingOrder>1</SiblingOrder>
    <HashCode>AAAAEAAAAAAAgAAAAIAAAAQAAAAAAAAAAAgAAAAAAAA=</HashCode>
  </TypeIdentifier>
</Class>

The core of the element is the <TypeIdentifier> element, which contains the name of the class and a pointer to the actual class file. This allows the designer to read the class file to derive most of the details about the class itself. The <AbsoluteBounds> element indicates the position of the item in the diagram.

There is also a <ShowAsAssociation> element. This exists because our diagram, shown in Figure 7, has an association between Product and InventoryQOH. Because this relationship is reflected in the diagram, the XML includes a reference to the specific property in the Product class that creates the association.

If we return to the Class Diagram, we can right-click the association line and choose the Show as Property option. When you select this option, the association line between the Product and InventoryQOH classes is removed from the diagram, and the InventoryQOH property is displayed as a property within the Product class itself. To reverse the process, you can right-click on the InventoryQOH property in the Product class and choose the "Show as Association" menu option.

By turning off the "Show as Association" option, we have changed the diagram, but not the code. In other words, the association between Product and InventoryQOH is unchanged and only the display is affected. This is reflected in the XML for the diagram because the <ShowAsAssociation> element has been removed from the Product entry in the XML.

[SIZE=3]Conclusion[/SIZE]

The Class Designer should be a very nice tool for object-oriented designers and developers using Visual Studio 2005. It provides a UML-like class diagram that is capable of reflecting all the language features of Visual Basic .NET and C# in the Microsoft .NET environment. Not only does it allow you to create accurate diagrams, but it keeps the diagram and code in sync in real time. You can now look forward to a time when your class diagrams truly reflect your code. The ability to move from diagram to code and back will ensure that your pretty pictures are more than just a memory of something that could have been but, in fact, are accurate maps of your functioning system.

全看分页树展 · 主题


有趣有益,互惠互利;开阔视野,博采众长。
虚拟的网络,真实的人。天南地北客,相逢皆朋友

Copyright © cchere 西西河