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.
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 ….
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.
Pingback: Migrate WCF to be hosted in Azure | .NET Development by Eric