Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
- Create a new Class Diagram
- Create a class diagram from existing code
- Generate code from a class diagram
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.
Create a new Class Diagram
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
Generate a class diagram from existing code
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.
Generating code from a class diagram
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.
Anonymous
September 19, 2014
You can probably do all that, but remember the good time it was when the developer was the wizard.Anonymous
September 19, 2014
@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!Anonymous
November 21, 2014
Hi, i have a public class MyClass{ public class MyInnerClass{ } } so you can show me how i display relationship between MyClass and MyInnerClass ?Anonymous
December 01, 2014
How to access Visual Studio classes and read its contents?Anonymous
December 02, 2014
@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 codeAnonymous
December 12, 2014
@Susan: The DreamSpark program doesn't offer Visual Studio Ultimate, but it offers Visual Studio Pro and lately Visual Studio Community editions.Anonymous
January 08, 2015
please help us how can add time event tools and other tools to toolbox in visual studio 2013 for activity diagram?Anonymous
January 08, 2015
how can add tools to toolbox for active diagrams in visual studio 2013? please help usAnonymous
January 28, 2015
Thanks Susan for this wonderful article.Anonymous
February 03, 2015
Why is there no "View Code" option for classes in referenced assembly/projects?Anonymous
February 03, 2015
Hi Susan, more details here stackoverflow.com/.../why-is-there-no-view-code-option-in-class-diagrams-for-classes-from-referenced Thank you.Anonymous
February 04, 2015
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! SusanAnonymous
June 17, 2015
Great explanation. I found more information and examples in the diagram community of http://creately.comAnonymous
November 12, 2015
Hi this 'Architecture Explorer' is awesome helps a lot to inspect structure. The ability to filter public members helps a lot. I am using first time, I liked although I have couple of suggestions.As we are forced to use mouse doe complexity make it double click to go to definition. At least for Types and Members view it will work perfectly. For calls view it would help a way to sync the code with the calls clicked or double clicked. It will help inspect even faster the code.
In Members and Calls window sort by order appearing in the code rather than name, it will give us an idea of how the code looks like, the order of calls and dependencies. I know for Members window there is no problem but for Calls window some members might be called twice or more and appear in different places, maybe a filter could help there to disable it and show it just in alphabetic order.
Another easy way to get until the Types window rather than have to go by the first window then namespace window. Those 2 first seems a waste of space and makes longer the steps to get where you really want to inspect.
Anonymous
January 27, 2016
The comment has been removed