views:

91

answers:

4

So thanks to the Visualization and Modeling Feature Pack , I can build a uml model diagram and generate a bunch of classes.

But what now? Presumably, my developers will add code to those classes. Useful code, valuable code, and as the templates themselves indicate:

//     Changes to this file will be lost if the code is regenerated.

So what is the best solution here? Can I make the modeling project reflect changes to the actual classes? Should I generate partial classes? Modify the default templates to read class files and not auto-generate anything that has been modified? Should I tell developers not to edit model files under pain of....well, pain?

Thanks for the tips.

+5  A: 

As far as I know, this is really the key reason for partial classes in the first place. The custom code goes in one file, the auto-generated in another.

David
Partial classes let you add members to a class definition. However, in this case, I assume he gets a bunch of methods from UML, and the point is to add bodies to those methods.
Pavel Minaev
@Pavel, that is where Partial Methods come into play. However, partial methods only work with Private methods, so I don't see how it would help with this scenario.
Jonathan Allen
@Pavel: I guess I don't see the value of any ongoing code generation at that point, then. I suppose the generated code could just be interfaces and then the developers work on the files that implement those interfaces. Then the usefulness of the UML part is the external contract for the code.
David
Not only partial methods must be private, but they also must return `void`. In general, a partial method is something that may be omitted entirely - hence the requirements on it. It does not mandate the programmer to provide an implementation.
Pavel Minaev
@David: the point of ongoing code generation is to further develop the design on UML levels. Most UML modeling tools do in fact allow round-tripping like that.
Pavel Minaev
+1  A: 

You could also create classes derived from the generated ones, and put any changes in there. I also agree with above poster that partial classes could be the way to go.

Jeremy
A: 

Although the tools generate basic skeleton classes out of the box, that's really just a starting point. You can easily adapt the generator templates to create your own stuff. Different people want to generate different code from the classes - some even generate XML or SQL. And yep, in C#, partial classes are good to generate, so's to keep the hand-written code separate from the generated bits. It's good to put lots of extension points in the generated code, where you fill in the details by hand code. Another neat idea is "double derived": from each UML class, generate a base class and a derived class. The derived one has only constructors. The base class has any methods you generate. So your hand code can easily override generated methods where you need that.

Alan Wills - Microsoft
A: 

There are several options in the tool and recommending what is best is hard without knowing your scenario. Partial classes are great for some, but not all applications. If you want your UML class to generate a partial class, you can set it's C# stereotype's property to "Partial" and it will do so, and custom code can then be added in a partial class that won't be overwritten. If you want to prevent code from being overwritten, you can do this by setting the overwrite property to False on the template binding that corresponds to the package you are working on. This lets you set your extension code to be in a package that is not overwritten, while your model mastered code is overwritten with the latest model changes. Finally, if you want your code to be the master for your model so it always reflects the latest code, then you can reverse engineer your code by using the architecture explorer to select your classes and then dragging them in to a UML diagram. So for a given gesture, either the model is the master or the code is the master. In this version, we did not implement automated merge capabilities between the two.

David Trowbridge - Microsoft