Generate C# source from UML class diagram in SDE for Visual Studio

You can perform round-trip engineering in SDE for Visual Studio, to keep C# source code and class model in sync. In this tutorial, we will see how to create a class model in SDE for Visual Studio, and eventually generating source code from model.

February 23, 2010
User Rating: / 7
Views: 7,517
PDF Link Add comments
Edition: Professional or above (Edition comparison)

  1. Create a C# library project Map Viewer in Visual Studio.
    create csharp project
  2. Right click on the project node in Solution Explorer, and select Open SDE-VS from the popup menu.
    open sde from project
  3. SDE-VS is started. In Diagram Navigator (of SDE), right click on class diagram and select New Class Diagram from the popup menu.
    new class diagram
  4. A new diagram is created. You are asked to enter a package header on top of the diagram. Enter myapp and press Enter.
    name package header
  5. Click on the down arrow button near the shape selection Class in diagram toolbar, and select Interface.
    select interface
  6. Click on the diagram to create an interface class and name it as IMap.
    create imap
  7. Create operations in IMap. Right click on the class IMap and select Add > Operation from the popup menu.
    create oper
  8. Enter JumpToLocation(x : int, y : int) : void to create a public operation JumpToLocation with parameter x, y and return void.
    name oper
  9. Press Enter to create another operation, Name it Clear() : void. Click on diagram to confirm editing.
    add clear oper
  10. We need to create a class for town map which inherits IMap. Move the mouse pointer over interface IMap, press on the resource icon Realication -> Class and drag downwards to create a new class.
    create realization class
  11. Name the class as TownMap and press Enter to confirm.
    create town map
  12. As the TownMap class is implementing the interface IMap, we need to implement the operations defined in IMap. Right click on class TownMap and select Related Elements > Realize all Interfaces from the popup menu.
    realize interface
  13. You can see that operations JumpToLocation and Clear are both inherited.
    opers realized
  14. It is time to add properties to class. Right click on class TownMap and select Add > Property from the popup menu.
    add prop
  15. Enter name : string to name the property as name, and set the type as string.
    name prop
  16. Press Enter to proceed to the next property. Enter x : double as property name and type.
  17. Again, press Enter and create property y : double.
    prop created
  18. We need to create a new class Region with association from class TownMap. Move the mouse pointer to the class TownMap. Press on the resource icon Aggregation -> Class.
    aggregation resource
  19. Drag to the right of the class TownMap and release the mouse button. Name the new class Region.
    region created
  20. Follow the previous steps to create properties in class Region.
    Class Properties
    Region name : string
      desc : string
      x : double
      y : double
  21. Up to now, the diagram should look like:
    props created in region
  22. Select File > Save SDE-VS Project from the main menu to save.
  23. Now comes the code generation. Select the menu Update Code on Diagram Navigator.
    to update code
  24. Check the Solution Explorer. You should see a list of generated file. You can open them to fill in the code body.
    code generated
  25. This is the end of the tutorial. Instead of closing Visual Studio now, you may try something more by editing the code like to add, rename or delete class, properties and operations, and select Update UML Model from Diagram Navigator, and observe the changes that will make in the class model. Enjoy!

Rate this Article

Click on one of the stars below to rate this article from 1 (lowest) to 5 (highest).

Comments (0)

Write comment

Rating

Comment

Enter the calculation result

security code