Share via


How to: Create Layer Diagrams from Artifacts

In Visual Studio Ultimate, you can use a layer diagram to visualize the high-level, logical architecture of your system and to make sure that the code stays consistent with the design. To perform this task, organize and associate the physical artifacts in a Visual Studio solution into logical, abstract groups called layers. These layers describe major tasks that these artifacts perform or the major components of your system. Each layer can also contain nested layers that describe more detailed tasks. You can create layer diagrams for managed code, such as Visual C# and Visual Basic .NET projects.

You can specify the intended or existing dependencies between layers. These dependencies, which are represented as arrows, indicate which layers can use or currently use the functionality represented by other layers. To enforce architectural constraints on the code, describe the intended dependencies on the diagram, and then validate the code against the diagram.

For more information about layer diagrams, see the following topics:

Tip

When you create a layer diagram, you might also consider creating dependency graphs to help you explore and understand the code. These graphs can help you discover clusters, patterns, natural layers, and dependencies in the code. This makes it easier for you to assign artifacts to layers. You might also use Architecture Explorer to explore namespaces and classes, which often correspond well to the existing layers. You can then use layer diagrams to help you update the code.

For more information about dependency graphs, see:

For more information about layering patterns, visit the Patterns & Practices Web site.

In This Topic

  • Create a layer diagram.

  • Create layers from existing artifacts.

  • Manage links between layers and artifacts.

  • Reverse-engineer existing dependencies.

  • Update the layer diagram to show the intended design.

  • Change the appearance of elements on the diagram.

Create a Layer Diagram

Before you can create a layer diagram, your solution must have a modeling project. You can add a new layer diagram to an existing modeling project, create a new modeling project for the new layer diagram, or copy an existing layer diagram within the same modeling project. For more information about how to create modeling projects, see How to: Create UML Modeling Projects and Diagrams.

Important

Do not add, drag, or copy an existing layer diagram from a modeling project to another modeling project or to another location in the solution. A layer diagram that is copied in this way will have the same references as the original diagram, even if you modify the diagram. This will prevent layer validation from working correctly and might cause other issues, such as missing elements or other errors when you try to open the diagram.

To add a new layer diagram to a modeling project

  1. On the Architecture menu, click New Diagram.

  2. Under Templates, click Layer Diagram.

  3. Name the diagram

  4. In Add to Modeling Project, browse to and select an existing modeling project in your solution.

    -or-

    Click Create a new modeling project to add a new modeling project to the solution.

    Note

    The layer diagram must exist inside a modeling project. However, you can link it to artifacts that exist anywhere in the solution.

  5. Make sure to save both the modeling project and the layer diagram.

To copy an existing layer diagram

  1. Add a new layer diagram to the modeling project.

  2. Copy the elements from the source layer diagram to the new layer diagram.

Create Layers from Artifacts

You can create layers from artifacts in a Visual Studio solution, such as projects, code files, namespaces, classes, and methods. This automatically creates links between those layers and the artifacts, which are included in the layer validation process. You can also link layers to certain kinds of artifacts that do not support layer validation. For example, you can link layers to files such as Word documents or PowerPoint presentations. This is useful for associating a layer to specifications or plans. To see whether a linked artifact supports validation, open Layer Explorer and examine the Supports Validation property of the artifact. For more information, see Managing Links to Artifacts.

Use the following tasks to create layers:

To

Follow these steps

Create a layer for a single artifact

  1. Drag the artifact onto the layer diagram from any of the following sources:

  2. Rename the layer to reflect the responsibilities of the associated code or artifacts.

Important noteImportant
Dragging binary files to the layer diagram does not automatically add their references to modeling project. You must manually add the binary files that you want to validate to the modeling project. To add binary files to the modeling project
  1. In Solution Explorer, open the shortcut menu for the modeling project, and then choose Add Existing Item.

  2. In the Add Existing Item dialog box, browse to the binary files, select them, and then choose OK.

    The binary files appear in the modeling project.

  3. In Solution Explorer, choose a binary file that you added, and then press F4 to open the Properties window.

  4. On each binary file, set the Build Action property to Validate.

Create a single layer for all selected artifacts

Drag all of the artifacts to the layer diagram at the same time.

A layer appears on the diagram and is linked to all of the artifacts.

Create a layer for each selected artifact

Press and hold the SHIFT key while you drag all of the artifacts to the layer diagram at the same time.

NoteNote
If you use the SHIFT key to select a range of items, release the key after you select the artifacts. Press and hold it again when you drag the artifacts to the diagram.

A layer for each artifact appears on the diagram and is linked to each artifact.

Add an artifact to a layer

Drag the artifact to the layer.

Create a new unlinked layer

In the Toolbox, expand the Layer Diagram section, and then drag a Layer to the layer diagram.

To add multiple layers, double-click the tool. When you are finished, click the Pointer tool or press the ESC key.

- or -

Right-click the layer diagram, point to Add, and then click Layer.

Create nested layers

Drag an existing layer onto another layer.

- or -

Right-click a layer, point to Add, and then click Layer.

Create a new layer that contains two or more existing layers

Select the layers, right-click a selected layer, and then click Group.

Change the color of a layer

Set its Color property to the color that you want.

Specify that artifacts associated with a layer must not belong to the specified namespaces

Type the namespaces in the layer's Forbidden Namespaces property. Use a semicolon (;) to separate the namespaces.

Specify that artifacts associated with a layer cannot depend on the specified namespaces

Type the namespaces in the layer's Forbidden Namespace Dependencies property. Use a semicolon (;) to separate the namespaces.

Specify that artifacts associated with a layer must belong to one of the specified namespaces

Type the namespace in the layer's Required Namespaces property. Use a semicolon (;) to separate the namespaces.

The number on a layer indicates the number of artifacts that are linked to the layer. However, when reading this number, remember the following:

  • If a layer links to an artifact that contains other artifacts, but the layer does not link directly to the other artifacts, then the number includes only the linked artifact. However, the other artifacts are included for analysis during layer validation.

    For example, if a layer is linked to a single namespace, then the number of linked artifacts is 1, even if the namespace contains classes. If the layer also has links to each class in the namespace, then the number will include the linked classes.

  • If a layer contains other layers that are linked to artifacts, then the container layer is also linked to those artifacts, even though the number on the container layer does not include those artifacts.

  1. On the layer diagram, right-click a layer, and then click View Links.

    Layer Explorer shows the artifact links for the selected layer.

  2. Use the following tasks to manage these links:

To

In Layer Explorer

Delete the link between the layer and an artifact

Right-click the artifact link, and then click Delete.

Move the link from one layer to another

Drag the artifact link to an existing layer on the diagram.

- or -

  1. Right-click the artifact link, and then click Cut.

  2. On the layer diagram, right-click the layer, and then click Paste.

Copy the link from one layer to another

  1. Right-click the artifact link, and then click Copy.

  2. On the layer diagram, right-click the layer, and then click Paste.

Create a new layer from an existing artifact link

Drag the artifact link to a blank area on the diagram.

Verify that a linked artifact supports validation against the layer diagram.

Look at the Supports Validation column for the artifact link.

Reverse-Engineer Existing Dependencies

A dependency exists wherever an artifact that is associated with one layer has a reference to an artifact that is associated with another layer. For example, a class in one layer declares a variable that has a class in another layer. You can reverse-engineer existing dependencies for artifacts that are linked to layers on the diagram.

Note

Dependencies cannot be reverse-engineered for certain kinds of artifacts. For example, no dependencies will be reverse-engineered from or to a layer that is linked to a text file. To see which artifacts have dependencies that you can reverse-engineer, right-click one or multiple layers, and then click View Links. In Layer Explorer, examine the Supports Validation column. Dependencies will not be reverse-engineered for artifacts for which this column shows False.

To reverse-engineer existing dependencies between layers

  • Select one or multiple layers, right-click a selected layer, and then click Generate Dependencies.

Typically, you will see some dependencies that should not exist. You can edit these dependencies to align them with the intended design.

Update the Diagram to Show the Intended Design

To show the changes that you plan to make or the architecture that you want, update the layer diagram.

To

Perform these steps

Delete a dependency that should not exist

Click the dependency, and then press DELETE.

Change or restrict the direction of a dependency

Set its Direction property.

Create new dependencies

Use the Dependency and Bidirectional Dependency tools.

To draw multiple dependencies, double-click the tool. When you are finished, click the Pointer tool or press the ESC key.

Specify that artifacts associated with a layer cannot depend on the specified namespaces

Type the namespaces in the layer's Forbidden Namespace Dependencies property. Use a semicolon (;) to separate the namespaces.

Specify that artifacts associated with a layer must not belong to the specified namespaces

Type the namespaces in the layer's Forbidden Namespaces property. Use a semicolon (;) to separate the namespaces.

Specify that artifacts associated with a layer must belong to one of the specified namespaces

Type the namespace in the layer's Required Namespaces property. Use a semicolon (;) to separate the namespaces.

Change the Appearance of Elements on the Diagram

To help you identify layers and dependencies or discuss them with team members, edit the appearance and layout of the diagram in the following ways:

  • Change the sizes, shapes, and positions of layers.

  • Change the colors of layers and dependencies.

    1. Select the layers or dependencies.

    2. Right-click the selected layers or dependencies, and then click Properties.

    3. In the Properties window, change the Color property.

See Also

Concepts

Visualizing Existing Code