Code generation from Visual Studio UML class diagram

In the Ultimate edition of Visual Studio 2010 Microsoft has put the Architecture feature. With this I can create Graphs, Layer and UML diagrams. The feature is readonly in the Premium edition, but I’m not sure it can generate code and that is what I’m going to do.

First I create a new UML class diagram. From the toolbox I add some classes (blue and orange), some interfaces (purple and green), two packages and the dependencies, interface implementations and inheritance.
screenshot of the uml class diagram

In this sample (download at end of this post) I design a WCF service with a DataContract, an IServiceContract and the ServiceImplementation. The ServiceImplementation must use a best-practice of ours and call the business layer, which must call a factory to create a data layer manager for accessing data. This way an implementation for hosting in Windows Azure can be accomplished by inheritance. The specifics for the platform or different storage can be overwritten, leaving the general stuff untouched.

A design looks nice on the wall printed in poster format. But why not use it to kickstart the development and generate the projects, classes and interfaces? Microsoft must have been thinking the same and offers the Generate Code option in the context menu. The default T4 templates and some other things used for the toolbox items can be set in Configure Default Code Generation Settings ….
Screenshot of Configure Default Code Generation Settings

In the sample I used the Body Conditions property of a method to generate implementation and the Default property of a property to generate a default return. For this I modified the default ClassTemplate.t4 and saved it in a new file (added to the sample project). Then I set the default T4 for a class to this new file in the Configure Default Code Generation Settings ….

Every element on the classdiagram can be linked to a custom T4 template. In the model I defined the DataContract as an Interface, because I wanted it to get placed in a different project together with the IServiceContract and the interfaces. This can be done again in the Configure Default Code Generation Settings …. As the DataContract isn’t realy an Interface but a Class, I had to overwrite the output with a custom T4. Be sure to check the order in which the T4 are executed as I try to explain on the msdn forum.

Here is the Modeling sample project. This should work out-of-the-box as all settings and files are included. If you get it working without the Visual Studio Ultimate edition please let me know or leave a comment.

About erictummers

Working in a DevOps team is the best thing that happened to me. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

1 Response to Code generation from Visual Studio UML class diagram

  1. Pingback: Migrate WCF to be hosted in Azure | .NET Development by Eric

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.