西西河

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

共:💬4 新:
全看树展主题 · 分页首页 上页
/ 1
下页 末页
家园 【评论】闲谈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.

家园 评论

Highway的这个评论很有意思,前面是一段不相干的个人历史(它表明了美国的某所学校可以让学生通过一个基本合格而简单的Project拿到学位),后面则是微软的一篇文章,有趣!

开个玩笑,Highway不要和我急,下面言归正传。

我不知道微软的这篇文章是什么时候写的,但是至少是从前年开始,Borland Together已经做到了UML Diagram和对应代码(相对于Class Diagram)的同步,后来Rational XDE也做到了同样的功能(当然是针对Java的),因此微软不能够说自己是头一个做到的,而顶多能够说自己是头一个做到UML Diagram和对应.Net代码同步的,否则会有官司吃的。

平心而论地讲,这篇文章有一个地方是对的,那就是目前的UML还不能够支持.Net语言的全部特性,因此需要微软自己的一些发明(例如对事件的描述)(也许这会导致另一场关于开放和专有的官司)。

同样平心而论地讲,UML Diagrams的组织和处理并不是很复杂(基本就是一些树下面的一些图),而UML Diagram和对应代码(相对于Class Diagram)的映射与同步应该也不是什么复杂的事情。

更加平心而论地讲,以微软制作用户界面的本事,一个好用的UML工具不是什么很复杂的事情。

可以真正复杂的是如何处理好UML语法属性和对应编程语言语法属性的映射关系,否则双方的信息损失太多了。这点是MDA的基础和迫切需要解决的一个主要问题,它才是对微软学术/技术能力的真正挑战。

评论
家园 三年前使用Together的时候,就感觉已经很成熟了。

那时候我们的BA(Business Analyst) 隔三差五的用Together生成我们程序的流程图以及Class diagram,做成的Report交到上面。看起来像模像样的。

Together的缺点就是太贵。像当时我们那种“奢靡无度”巨型公司,也只给我们组安装了两套。和VS2005的价位不可同日而语!

家园 那就麻烦了

我感觉要达到Borland Together 6.2所达到的标准和程度,Visual Studio.Net 2005还有很长的路要走,我很怀疑微软有无可能在Visual Studio.Net 2005正式版发布的时候完成这项艰巨的任务。

随着Together被Borland收购和Rational被IBM收购,微软基本上只有依靠自己来制作这类工具了。不过如果没有这类工具的话,Visual Studio.Net 2005在软件工程领域只能够做个落后分子了。不管怎么说,Visio都应该退休了。

全看树展主题 · 分页首页 上页
/ 1
下页 末页


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

Copyright © cchere 西西河