Create dependency diagrams from your code - Visual Studio (Windows) (2023)

  • Article
  • 10 minutes to read

Applies to: Create dependency diagrams from your code - Visual Studio (Windows) (1)Visual Studio Create dependency diagrams from your code - Visual Studio (Windows) (2)Visual Studio for Mac Create dependency diagrams from your code - Visual Studio (Windows) (3)Visual Studio Code

To visualize your software system's high-level, logical architecture, create a dependency diagram in Visual Studio. To make sure that your code stays consistent with this design, validate your code with a dependency diagram. You can create dependency diagrams for Visual C# and Visual Basic projects. To see which editions of Visual Studio support this feature, see Edition support for architecture and modeling tools.

Create dependency diagrams from your code - Visual Studio (Windows) (4)

A dependency diagram lets you organize Visual Studio solution items into logical, abstract groups called layers. You can use layers to describe major tasks that these artifacts perform or the system's major components.

Each layer might contain other layers that describe more detailed tasks. You can also specify the intended or existing dependencies between layers. These dependencies, represented as arrows, show the layers that use or can use the functionality represented by other layers. To maintain architectural control of the code, show the intended dependencies on the diagram and then validate the code against the diagram.

Video: Validate your architecture dependencies in real time

Create a dependency diagram

Before you create a dependency diagram, ensure that your solution has a modeling project.

Important

Don't add, drag, or copy an existing dependency diagram from one modeling project to another modeling project, or to another place in the solution. This preserves the references from the original diagram, even if you change the diagram. This also prevents layer validation from working correctly and might cause other issues, such as missing elements or other errors when you try to open the diagram.

Instead, add a new dependency diagram to the modeling project. Copy the elements from the source diagram to the new diagram. Save both, the modeling project and the new dependency diagram.

Add a new dependency diagram to a modeling project

Note

Dependency diagrams for .NET Core projects are supported starting Visual Studio 2019 version 16.2.

(Video) Using VS 2017 / 2019 Code Map to find out the Code Dependencies
  1. On the Architecture menu, choose New Dependency Diagram.

  2. Under Templates, choose dependency diagram.

  3. Name the diagram.

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

    -or-

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

    Note

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

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

Drag and drop, or copy and paste, from a Code Map

  1. Generate a Code Map for the solution using the Architecture menu.

    (Video) Code Maps in Visual Studio 2019 | C# Code Maps Getting Started

  2. Consider applying a Code Map filter to remove solution folders and "Test Assets" if you only want to enforce dependencies in product code.

  3. On the generated Code Map, remove the "External" node, or expand it to show external assemblies, depending on whether you want to enforce namespace dependencies. Delete non-required assemblies from the Code Map.

  4. Create a new Dependency Diagram for the solution using the Architecture menu.

  5. Select all the nodes on the Code Map (use Ctrl + A, or use the rubber band selection by pressing the Shift key before you click, drag, and release.

  6. Drag and drop, or copy and paste, the selected elements to the new Dependency Validation diagram.

  7. This shows the current app architecture. Decide what you want the architecture to be and modify the dependency diagram accordingly.

Create dependency diagrams from your code - Visual Studio (Windows) (5)

Create layers from artifacts

You can create layers from Visual Studio solution items, such as projects, code files, namespaces, classes, and methods. This automatically creates links between layers and items, including them in the layer validation process.

You can link layers to items that don't support validation, such as Word documents or PowerPoint presentations. This lets you associate a layer with specifications or plans. You can also link layers to files in projects that are shared across multiple apps, but the validation process won't include those layers, which appear with generic names such as "Layer 1" and "Layer 2".

To see if a linked item supports validation, open Layer Explorer and examine the Supports Validation property of the item. See Managing links to artifacts.

ToFollow these steps
Create a layer for a single artifact
  1. Drag the item onto the dependency diagram from these sources:
    • Solution Explorer
      For example, you can drag files or projects.
    • Code maps
      See Map dependencies across your solutions and Use code maps to debug your applications.
    • Class View or Object Browser
    A layer appears on the diagram and is linked to the artifact.
  2. Rename the layer to reflect the responsibilities of the associated code or artifacts.

Important: Dragging binary files to the dependency diagram doesn't automatically add their references to modeling project. 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 artifactsDrag all the artifacts to the dependency 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 artifactPress and hold the SHIFT key while you drag all of the artifacts to the dependency diagram at the same time.
Note: 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 layerDrag the artifact to the layer.
Create a new unlinked layerIn the Toolbox, expand the Dependency Diagram section, and then drag a Layer to the dependency diagram.

To add multiple layers, double-click the tool. When you're done, choose the Pointer tool or press the ESC key.

(Video) Class Diagram in Visual Studio 2019 | Class Designer Getting Started

- or -

Open the shortcut menu for the dependency diagram, choose Add, and then choose Layer.

Create nested layersDrag an existing layer onto another layer.

- or -

Open the shortcut menu for a layer, choose Add, and then choose Layer.

Create a new layer that contains two or more existing layersSelect the layers, open the shortcut menu for your selection, and then choose Group.
Change the color of a layerSet its Color property to the color that you want.
Specify that artifacts associated with a layer shouldn't belong to the specified namespacesType the namespaces in the layer's Forbidden Namespaces property. Use a semicolon (;) to separate the namespaces.
Specify that artifacts associated with a layer can't depend on the specified namespacesType 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 namespacesType 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:

  • If a layer links to an artifact that contains other artifacts, but the layer doesn't 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 doesn't include those artifacts.

Manage links between layers and artifacts

  1. On the dependency diagram, open the shortcut menu for the layer, and then choose View Links.

    Layer Explorer shows the artifact links for the selected layer.

  2. Use the following tasks to manage these links:

ToIn Layer Explorer
Delete the link between the layer and an artifactOpen the shortcut menu for the artifact link, and then choose Delete.
Move the link from one layer to anotherDrag the artifact link to an existing layer on the diagram.

- or -

1. Open the shortcut menu for the artifact link, and then choose Cut.
2. On the dependency diagram, open the shortcut menu for the layer, and then choose Paste.

(Video) How to use Visual Studio Code as Your Diagram Tool

Copy the link from one layer to another1. Open the shortcut menu for the artifact link, and then choose Copy.
2. On the dependency diagram, open the shortcut menu for the layer, and then choose Paste.
Create a new layer from an existing artifact linkDrag the artifact link to a blank area on the diagram.
Verify that a linked artifact supports validation against the dependency 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 can't 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, open the shortcut menu for one or multiple layers, and then choose View Links. In Layer Explorer, examine the Supports Validation column. Dependencies won't be reverse-engineered for artifacts for which this column shows False.

  • Select one or multiple layers, open the shortcut menu for a selected layer, and then choose Generate Dependencies.

    If you see some dependencies that shouldn't exist, you can edit these dependencies to align them with the intended design.

Edit layers and dependencies to show the intended design

To describe the changes that you plan to make to your system or the intended architecture, edit the dependency diagram:

ToPerform these steps
Change or restrict the direction of a dependencySet its Direction property.
Create new dependenciesUse the Dependency and Bidirectional Dependency tools.

To draw multiple dependencies, double-click the tool. When you're done, choose the Pointer tool or press the ESC key.

Specify that artifacts associated with a layer can't depend on the specified namespacesType 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 namespacesType 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 namespacesType the namespace in the layer's Required Namespaces property. Use a semicolon (;) to separate the namespaces.

Change how elements appear on the diagram

You can change the size, shape, color, and position of layers or the color of dependencies by editing their properties.

Discover patterns and dependencies on a code map

While creating dependency diagrams, you might also create code maps. These diagrams can help you discover patterns and dependencies while you explore the code. Use Solution Explorer, Class View, or Object Browser to explore assemblies, namespaces, and classes - which often correspond well to existing layers. For more information about code maps, see:

See also

FAQs

What is the purpose of a dependency diagram? ›

A dependency diagram is a graphical display of all the tasks in a workflow which are required to complete a request. You can use it to create dependencies between tasks, that is, specify the order in which each will be completed, and which tasks need to be completed before others can be released.

How do I enable code map in Visual Studio? ›

Code Maps in Visual Studio 2019 | C# Code Maps Getting Started

What is code map in Visual Studio? ›

Code maps in Visual Studio can help you avoid getting lost in large code bases, unfamiliar code, or legacy code. For example, when you're debugging, you might have to look at code across many files and projects. Use code maps to navigate around pieces of code and understand the relationships between them.

How do I create a data flow diagram in Visual Studio 2017? ›

Start a data flow diagram

Click the File tab. Click New, click Software and Database, and then double-click Data Flow Diagram. From the Data Flow Diagram Shapes stencil, drag an External interactor shape onto the drawing page to represent each external source or destination for data.

What is dependency graph with example? ›

A dependency graph is a data structure formed by a directed graph that describes the dependency of an entity in the system on the other entities of the same system. The underlying structure of a dependency graph is a directed graph where each node points to the node on which it depends.

What is dependency in UML diagram? ›

In UML, a dependency relationship is a relationship in which one element, the client, uses or depends on another element, the supplier.

What is use of dependency graph in compiler construction? ›

A dependency graph is used to represent the flow of information among the attributes in a parse tree. In a parse tree, a dependency graph basically helps to determine the evaluation order for the attributes.

How do you show dependencies in a project plan? ›

In order to see the task dependencies you need to insert the Predeccessors Column by going to the Format menu – Insert Column, select the Predeccessors field from the “Field Name” box and then click OK in order for the changes to take effect.

How do I show dependencies between projects? ›

View and update external dependencies
  1. Choose View > Gantt Chart.
  2. If any filters are turned on, turn them off: On the View tab, in the Data group, select No Filter in the Filter list. ...
  3. Choose Project > Links Between Projects.
  4. View external predecessor tasks or successor tasks:

How do you show dependency in a flowchart? ›

The notation for a dependency is a dashed arrow, and the element at the tail of the arrow depends on the element at the arrowhead. So the arrow representing a dependency specifies the direction of a relationship, not the direction of a process.

What is code mapping? ›

In many programming languages, map is the name of a higher-order function that applies a given function to each element of a collection, e.g. a list or set, returning the results in a collection of the same type. It is often called apply-to-all when considered in functional form.

How do you use code graphs? ›

Press Alt+C to find callers / included files / functions using the variable. Press Alt+V to find callees / files including this file / used variables. Some callees may not be found by pressing Alt+V . In these cases, place the cursor on those callees and press Alt+H , then they will be added to Code Graph.

What is map in C#? ›

C# doesn't have any built-in Maps. The Maps is the concept that provides the functionality to map the value with the key inside the dictionary. So we can say a map is used inside the dictionary for mapping the key-value pair.

What is dependency class diagram? ›

You can use dependency relationships in class diagrams, component diagrams, deployment diagrams, and use-case diagrams to indicate that a change to the supplier might require a change to the client. You can also use a dependency relationship to represent precedence, where one model element must precede another.

What is IntelliTrace in Visual Studio? ›

IntelliTrace always records events that happen in the Visual Studio debugger. For example, starting your application is a debugger event. Other debugger events are stopping events, which cause your application to break execution.

How do I get the architecture menu in Visual Studio 2017? ›

You can open it from the Windows Start menu, or within Visual Studio by selecting Tools > Get Tools and Features.

Videos

1. Dependency Diagrams (VS2010 Quick Hit)
(VisualStudioALM)
2. Layer Diagrams and Dependency Validation
(EPC Group.net)
3. CppDepend Dependency Graph
(CoderGears Team)
4. Using PlantUML in VSCode
(Christopher Fuhrman)
5. Visual Studio Class Diagram Relationships
(Maurice Muteti)
6. How to Add Reference to your Visual Studio Project
(Aman Shukla)
Top Articles
Latest Posts
Article information

Author: Roderick King

Last Updated: 11/29/2022

Views: 6164

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.