You can write code that is executed whenever a change occurs in a UML model in Visual Studio. It will respond equally to changes that are made directly by the user, and by other Visual Studio extensions.

Warning: The techniques shown in this sample might not work in future releases of Visual Studio. They depend on features of the internal implementation of the UML tools. It might not be possible to achieve the same effect in future versions of Visual Studio.

To Exercise the Sample

  1. You must have installed:
  2. Download and open the solution.
  3. Press F5 to build the solution and open a debugging instance of Visual Studio.
  4. In the debugging instance, create a class diagram.
  5. Create two classes, and create an association between them.
    • Notice that the association has no arrows.
    • This is because the second example in Rule.cs responds to the new association by setting its navigability.
  6. Select a class, and in the Properties window, choose a stereotype.
    • Two test message boxes pops up in succession.
    • These are caused by the event example and the rule example.
  7. Unapply the stereotype.
    • Two messages are generated by the deletion event and rule examples.

Events and Rules

VMSDK provides two principal methods of detecting changes in the store:

To define rules and events, you must know the name of the implementation class or relationship that you want to monitor. These classes are defined in Microsoft.VisualStudio.Modeling.Uml.dll, and you can see the class names when you watch properties in the debugger. You can cast these class members to the appropriate interface types such as IClass, IStereotype. For a list of interface types, see Model Element Types.

Warning: The implementation class names might be different in future releases.


Defining Events and Rules

The sample code in Events.cs and Rules.cs demonstrates how to define and register rules. Events are represented by methods; rules are represented by classes.

The example demonstrates rules and events for adding and deleting elements and links in the model. You can also define handlers that are invoked when property values change, and when the ordering of links in a list changes.

Questions and Suggestions

Please provide feedback in the Visualization and Modeling SDK Forum.