avatarEdson Moisinho

Summary

The website content provides a guide on generating UML class diagrams for .NET Core projects using PlantUML within Visual Studio Code, highlighting the benefits of automated diagram generation over manual methods.

Abstract

The article discusses the importance of UML class diagrams for system documentation, understanding class relationships, and planning refactoring in .NET Core development. It emphasizes the inefficiency of manual diagram creation and introduces PlantUML as an open-source solution for automatically generating UML diagrams from plain text. The author explains how to use the PlantUML Class Diagram Generator tool to create diagrams from C# code in Visual Studio Code, detailing the installation process, command usage, and integration with the vscode/plantuml extension for exporting diagrams. The guide includes examples of generating diagrams for various C# components, such as classes, interfaces, and enums, and concludes by encouraging exploration of PlantUML's capabilities beyond C#.

Opinions

  • The author positively endorses PlantUML and the PlantUML Class Diagram Generator tool for their flexibility and support in automating the creation of UML diagrams in .NET Core projects.
  • There is a clear preference for automated diagram generation tools over manual methods, citing the painstaking and arduous nature of the latter.
  • The Visual Studio Class Designer is acknowledged for its greatness and flexibility but is critiqued for its lack of support for .NET Core and absence in Visual Studio Code.
  • The author suggests that the PlantUML tool's language is easy to learn and use, implying that users can quickly start creating diagrams in various editors.
  • The article implies that the use of PlantUML is not limited to C# and can be extended to other programming languages due to its open-source nature.
  • The author expresses that the benefits of using PlantUML extend to team knowledge dissemination and overall project documentation and understanding.

Generating Class Diagrams for .Net Core

Use PlantUML directly in the Visual Studio Code

Class diagram generated in Visual Studio Code

UML Class Diagrams are a very great way to document your system, inspect and understand the relationships between the classes and plan refactorings and disseminate knowledge inside the team.

Although having class diagrams brings many benefits, generating them manually is a painful and arduous task, fortunately, we can use tools to generate them automatically

Visual Studio Class Designer

The Visual Studio Class Designer is a great and very flexible tool to generate class diagrams, it allows drag and drops on a single or in multiple .cs files directly to the diagram, and it automatically syncs changes both in the .cs files or in the diagram.

As we can see, using the Class Designer has many advantages but unfortunately, it does not exist in the Visual Studio Code and even in the Visual Studio, it does not support .Net Core.

PlantUML

PlantUML is an open-source tool that allows generating many types of UML diagrams from plain text, it uses a specific language and you can learn and start creating your diagrams in many different editors.

Fortunately exists many extensions and add-ons that automatically generate PlantUML from source code, in the next section I will show how to generate the PlantUML and the diagrams using C#.Net Core directly in Visual Studio Code.

Creating the example classes

In order to generate the diagrams, I will create a few components like classes, interfaces, enum, public and private properties, constructors, and constants.

I am creating these components just to exemplify how the generating diagram tool works, so please do not pay attention to the classes and their content.

Base classes, interface, and enum:

Mammal, Dog, Cat classes and IHunt interface

Derived classes and folder structure:

Dog and Cat child classes

Generating PlantUml from the code

We can generate the PlantUml from the code using the PlantUml Diagram Generator tool.

First, install the tool via Nuget Package running the command dotnet tool install --global PlantUmlClassDiagramGenerator --version 1.2.4

Next, execute the command to search the .cs files inside the folders and generate the .puml files running dotnet puml-gen Entities generatedPuml -dir -execludePaths bin,obj,Properties -createAssociation -AllInOne these command parameters are very simple and can be checked on the tool documentation.

The command will generate a folder in the root with all the .puml individual files and the include.puml file with putting all of them together.

Printing logs, generating the output folder with all individual .puml files

Or you can use Visual Studio Code to generate the files without customizing the generation process.

Visual Studio Code generates a diagram option

Reading the PlantUML files

To read the PlantUML files we will need to use the vscode/plantuml extension, once installed it will provide some options in the IDE for exporting .puml files to images.

To install it, search the extension in the Visual Studio Code Marketplace:

PlantUML extension for VSCode in the Marketplace

Then, reload the IDE and select the PlanUml: Export Workspace Diagramsoption. It will open a dialog asking for the image format you want to use before generating an output folder with all the exported diagrams images:

Export Workspace Diagrams option
Diagram format option

As we can see, it was generated an ./out folder with one diagram for each individual class and a big one with all the classes together in one single image file.

Folder structure with all generated files and full classes diagram

There are many other options to explore that I am not covering here, you can check the tools documentation and explore all the other available features.

Remember we do not need to limit this only to C# code, the PlantUML is an open-source initiative, so other implementations already exist to support it for other languages.

I hope this article can help you and your team to generate nice and useful class diagrams, thank you for reading, and see you next time.

Csharp
Programming
Software Development
Coding
Code
Recommended from ReadMedium