Designing a new feature? Trying to figure out someone else’s code? Got a class diagram completed and need to start coding? Take advantage of the modelling features in Visual Studio to save yourself some time!
Using an agile methodology doesn’t mean you never document or design anything. The class diagram is still a great tool to help you design a new application or to visualize an existing application so you can plan a new feature. Using Visual Studios architecture features can save you time on documentation or digging through files so you can focus on the design and implementation.
In this post we will look at how you can use Visual Studio 2013 to
Note: The architecture features described in this post are only available in Visual Studio Ultimate. If you are you a student in a technical program check if your department has DreamSpark Premium which gives you access to Visual Studio Ultimate.
In Visual Studio choose File | New Project | Modeling project. The Modeling project is only available in Visual Studio Ultimate.
Go to Solution Explorer, right click in the project and choose Add New Item and select UML Class Diagram.
As you can see from the screenshot below you are not limited to class diagrams, you can also create Sequence, Use Case, Activity Diagrams and more.
Once you create a class diagram, you will see the modelling objects in the Toolbox. Drag and drop the classes, associations and inheritance diagram elements to your class diagram. If you need it, here is the documentation for How to Edit UML Models and Diagrams
Open a C# project (this feature is not supported for all programming languages at the moment)
From the menu choose Architecture | New Diagram. If you can’t find Architecture in the menu, your version of Visual Studio may not support this feature, this feature is only available in Visual Studio Ultimate.
Select the diagram you want to create, e.g. UML Class Diagram, UML Sequence Diagram. You will be prompted to provide a name for the diagram and a modelling project to contain your diagrams.
You will see a blank Class Diagram
Because we want to add existing code to our model instead of drawing new objects manually, go back to the menu and select Architecture | Windows | Architecture Explorer. This will open a new window where you can see all the namespaces in your project. We will use this window to select the elements we want included in our class diagram.
In Architecture Explorer, expand the Namespace that contains the classes you want to add to your diagram. Select the elements you want to include in the diagram and then select the Create a new graph from selected nodes button in the top left corner of the Architecture Explorer Window
Visual Studio will reverse engineer the code and create a class diagram for the selected elements.
I am not going to go into all the details of how you navigate the architecture explorer window and select individual elements, I just want you to be aware of the capability to create class diagrams from your code. For more complete documentation visit Create UML Class Diagrams from Code.
When you have a class diagram you can use the Generate Code command to create C# for your classes.
Open your modelling project with the class diagram and select the class or classes you want to create as code. Right click the selected element or elements and select Generate Code from the pop-up menu.
By default you will get a single file created for each class
Here you can see the code generated for the Carnivore class
public class Carnivore : Animal
public virtual void Hunt()
throw new System.NotImplementedException();
It is possible to Customize the Generate Code Command to change the default code generation settings. You can find more complete documentation at Generate Code from UML Class Diagrams
As you can see Visual Studio 2013 Ultimate has a number of features to make it easier to work with Class Diagrams whether you are starting a new project, or working with existing code.
You can probably do all that, but remember the good time it was when the developer was the wizard.
@Johan - Oh yeah, done that many times! But I do like being able to spend more time on implementing functionality and spending less time going Property ...Set Property ... Get Property ...Set Property ... Get over and over again. Though in C# when they added auto-implemented properties that helped a LOT!
How to access Visual Studio classes and read its contents?
@Amir - Go to the Solution Explorer Window, double click on the file with the file name of <classname>.cs that will bring up the class in the code editor so you can see and modify the code
Thanks Susan for this wonderful article.
Why is there no "View Code" option for classes in referenced assembly/projects?
Hi Susan, more details here
That is a really good question to post to the Visual Studio Community Forums, in particular I would suggest posting that to the Visual Studio Code Visualization and Modelling tools discussion group so you can get an answer directly from someone on the Visual Studio team! social.msdn.microsoft.com/.../home
If the feature doesn't exist, you can also put in a request for it to the Visual Studio team at www.visualstudio.uservoice.com
hope that helps!