Skip to content

Repository files navigation

SlnDependencyDiagramGenerator

Generates D2 and Mermaid dependency diagrams for Visual Studio Solutions.

NuGet NuGet


SlnDependencyDiagramGenerator parses Visual Studio solutions (.sln / .slnx), resolves project, package, and framework dependencies, and produces dependency diagrams in D2 and/or Mermaid formats — with optional image export (PNG, SVG, PDF) via the corresponding CLI tools.

The repository ships three deliverables around a shared core:

Deliverable What it is Best for
SlnDependencyDiagramGenerator A .NET library (NuGet package) Embedding diagram generation in your own code, tools, or build steps
SlnDependencyStudio CLI A cross-platform command-line tool Automating generation in scripts and CI pipelines
SlnDependencyStudio WPF A Windows desktop application Authoring, editing, and running dependency projects with a graphical UI

Pre-built versions of the SlnDependencyStudio CLI and WPF application are distributed together as SlnDependencyStudio — download them from the Releases page.

This D2 example and Mermaid example were produced from the solution in this repository.


Table of Contents


Quick Start

Using the core library via NuGet

dotnet add package SlnDependencyDiagramGenerator

See the DiagramGeneratorSample project for a complete working example that loads configuration from appsettings.json and runs the generator against the solution in this repository.


The Core Library (NuGet)

SlnDependencyDiagramGenerator is a .NET library targeting net8.0, net9.0, and net10.0, published to NuGet.

Key capabilities:

  • Parses .sln / .slnx solutions and resolves project, package, framework, and transitive dependencies from restored project.assets.json files.
  • Generates per-project (Individual) and solution-level (All) diagrams in D2 and/or Mermaid formats.
  • Auto-discovers target frameworks from restored project assets — no framework list required in configuration.
  • Detects multi-version package conflicts, grouping them visually and reporting them in the dependency summary.
  • Supports regex include/exclude filters plus package and framework exclusions.
  • Produces a Dependency Summary.md and can export PNG, SVG, and/or PDF images via the D2 CLI and Mermaid CLI (mmdc).

Getting started

Using the library is a four-step process: register the services, build a DependencyGeneratorConfig, validate it, and generate the diagrams. A complete example showing all four steps is included at the end of this section.

Step 1 — Register all required services

Install the NuGet package, then register everything the library needs with a single call to AddSlnDependencyDiagramGenerator():

dotnet add package SlnDependencyDiagramGenerator
using Microsoft.Extensions.DependencyInjection;
using SlnDependencyDiagramGenerator.Extensions;

var services = new ServiceCollection();
services.AddSlnDependencyDiagramGenerator();

That one call registers IDependencyGenerator — the main entry point — and every other service the library needs. It returns an SlnDependencyDiagramGeneratorRegistration (containing the service collection and a validation registry), which you only need if you want to chain additional registrations:

var (serviceCollection, validationRegistry) = services.AddSlnDependencyDiagramGenerator();

Step 2 — Build a DependencyGeneratorConfig

A DependencyGeneratorConfig (namespace SlnDependencyDiagramGenerator.Config) holds every option for a run. At minimum it needs a solution path. You can build it in code:

using SlnDependencyDiagramGenerator.Config;

var config = new DependencyGeneratorConfig
{
    Solution = new GeneratorSolutionOptions
    {
        SolutionPath = @"C:\dev\MySolution\MySolution.sln"
    }
};

or bind it from appsettings.json, which is what the bundled sample does. The JSON properties live under an options section:

{
  "options": {
    "solution": {
      "solutionPath": "MySolution.sln"
    }
  }
}
using Microsoft.Extensions.Configuration;
using SlnDependencyDiagramGenerator.Config;

var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

var config = new DependencyGeneratorConfig();
configuration.Bind("options", config);

DependencyGeneratorConfig exposes three option classes: GeneratorSolutionOptions (solution path, filters, exclusions, scopes), GeneratorDiagramOptions (direction, styles, grouping, formats), and GeneratorExportOptions (root path, clear behaviour, image formats). See configuration.md for the complete reference.

Note: JSON binding uses the standard Microsoft.Extensions.Configuration packages — Microsoft.Extensions.Configuration.Json for AddJsonFile() and Microsoft.Extensions.Configuration.Binder for Bind().

Step 3 — Validate the configuration

Resolve IDependencyGenerator from the service provider and call ValidateConfiguration(). It throws a FluentValidation.ValidationException if any validation rule is violated:

using SlnDependencyDiagramGenerator.Generator;

var serviceProvider = services.BuildServiceProvider();
var generator = serviceProvider.GetRequiredService<IDependencyGenerator>();
generator.ValidateConfiguration(config);

Step 4 — Generate the diagrams

await generator.CreateDiagramsAsync(config, CancellationToken.None);

This generates the dependency summary and diagram files for every discovered target framework, plus optional PNG/SVG/PDF images when the D2 and Mermaid CLIs are installed and image formats are configured.

Complete example

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SlnDependencyDiagramGenerator.Config;
using SlnDependencyDiagramGenerator.Extensions;
using SlnDependencyDiagramGenerator.Generator;

var services = new ServiceCollection();
services.AddSlnDependencyDiagramGenerator();                            // 1. Register all services
await using var serviceProvider = services.BuildServiceProvider();

var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

var config = new DependencyGeneratorConfig();
configuration.Bind("options", config);                                  // 2. Build the configuration

var generator = serviceProvider.GetRequiredService<IDependencyGenerator>();
generator.ValidateConfiguration(config);                                // 3. Validate the configuration

await generator.CreateDiagramsAsync(config, CancellationToken.None);     // 4. Generate the diagrams

Public API

Member Description
void ValidateConfiguration(DependencyGeneratorConfig configuration) Validates a configuration, throwing FluentValidation.ValidationException on any violation.
Task CreateDiagramsAsync(DependencyGeneratorConfig configuration, CancellationToken cancellationToken) Generates summaries, diagram files, and optional images for every discovered target framework.

Sample project: Samples/DiagramGeneratorSample is a thin console app that binds DependencyGeneratorConfig from appsettings.json (plus optional SETTINGS_VARIANT overlays), resolves IDependencyGenerator from DI, and calls CreateDiagramsAsync():

dotnet run --project Samples/DiagramGeneratorSample

Samples/NugetConflictSample is a companion project that deliberately references different package versions than the library, so the multi-version conflict table in the dependency summary can be validated. See Samples/README.md for details on the sample's configuration model.


SlnDependencyStudio CLI

SlnDependencyStudio.Cli is a cross-platform console tool (System.CommandLine) that reads a saved dependency-project file (.sds) and runs the same generation pipeline as the WPF application — designed for automation in scripts and CI pipelines.

Quick Start

# Publish the CLI (to a location of your choice)
dotnet publish Studio\SlnDependencyStudio.Cli -o D:\tools\SlnDependencyStudio

# Validate a project file
SlnDependencyStudio.Cli validate --pf sample.sds

# Run generation
SlnDependencyStudio.Cli run --pf sample.sds
  • Commands: validate (check a .sds file for configuration errors) and run (validate, optionally restore the solution, run optional pre/post-generation commands, and generate the diagrams).
  • Key options: --projectFile / --pf (required) and --verbose / -v (Debug-level console logging).
  • Deterministic exit codes (10011014, 1999 — see the Exit Codes reference) make scripting and CI integration predictable; rolling file logs are written to a logs subfolder beside the .sds file for troubleshooting past runs.

Note: Both frontends share the .sds document format via SlnDependencyStudio.Shared, so a project authored in the WPF application runs unchanged in the CLI and vice versa.

See the CLI User Guide for installation, the full command reference, and many usage examples.


SlnDependencyStudio WPF

SlnDependencyStudio.Wpf is a Windows desktop application (Windows 10+, net10.0-windows10.0.19041) built with ReactiveUI and MaterialDesignThemes. It provides a full graphical interface over the generator so you can author, edit, and run dependency projects without hand-editing JSON.

Quick Start

Build and run SlnDependencyStudio.Wpf from Visual Studio or the command line:

dotnet run --project Studio\SlnDependencyStudio.Wpf
  • IDE-style shell — File and Run menus, a navigation sidebar, a central configuration editor, and a docked output panel.
  • Five configuration pages — Project, Solution, Export, Diagrams, and Pipeline, including restore and pre/post-generation command settings with live d2/mmdc tool detection.
  • Analyse / GenerateShift+F5 runs a pre-flight dry run; F5 runs the full pipeline with real-time streaming output and user-initiated cancellation.
  • Output panel — Verbose/Wrap/Auto-scroll toggles, Cancel, Clear, Copy All, and Save As; preferences persist across sessions.
  • Settings & state — Default project folder, explicit d2/mmdc paths, log retention, Light/Dark theme, recent projects (capped at 10), and window placement, all persisted in AppData.

See the WPF User Guide for a full walkthrough with screenshots.


Repository Structure

├── Source/
│   └── SlnDependencyDiagramGenerator/    Core library (NuGet package)
├── Studio/
│   ├── SlnDependencyStudio.Shared/       Shared document, service, and process-execution contracts
│   ├── SlnDependencyStudio.Cli/          Cross-platform CLI frontend
│   └── SlnDependencyStudio.Wpf/          Windows desktop frontend
├── Samples/
│   ├── DiagramGeneratorSample/           Console sample using the library directly
│   └── NugetConflictSample/              Sample demonstrating multi-version packages
├── Tests/
│   ├── Source/                           Core library unit + integration tests
│   └── Studio/                           Shared, CLI, and WPF unit + integration tests
├── Docs/                                 User documentation
└── Studio Diagrams/                      Generated diagram output (per target framework)

License

See LICENSE.

About

Generates D2 diagram files and images for a Visual Studio Solution

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages