⬆️ ⬇️

UML-based code generation

As we already know , Visual Studio 2010 Ultimate provides quite convenient opportunities for reverse engineering. Using the tools of Visual Studio, we can build on the basis of the existing code a UML model and understand how, in fact, everything works with us, but at the same time not make tremendous efforts to create diagrams manually and keep them up to date.



Everything is good, but this tool completely lacks the ability to synchronize the model with the code. That is, after changing the model, we have to manually change the classes. In the case of a large number of small changes, it turns out to be rather inconvenient, and for this reason, full-fledged modeling is often refused.



Recently, Microsoft has released an add-on called Microsoft Visual Studio 2010 Feature Pack 2. This tool gives us an excellent opportunity to synchronize changes in the model code. Briefly tell how it can be used.



For example, let's say we have a primitive blog. The subject area is represented by two classes: Author and Article. Add a new Modeling Project to the solution. In it we create UML Class Diagram.

')





Take advantage of the possibilities of Reverse Engineering. Dragging classes from Architecture Explorer onto the diagram. In this case, an entity appears on the diagram along with the attributes. Periodically, between entities, links are formed that should be (and even periodically the type of connection is correctly shown), but in some cases it has not yet been possible to determine.







As we all know, the UML 2.0 standard defines four standard data types: Boolean, Integer, String, and UnlimitedNatural. The remaining types are automatically created in packages according to their location in the .NET namespaces.



So, we will try to “fix” the model to an adequate state, and at the same time, we will slightly expand it. To do this, create a new class in the diagram, drag it into the required Package in the UML Model Explorer, and select the C # Class stereotype (Microsoft has added several .NET-specific stereotypes that are used in code generation).







Notice that this class is not yet present in the Domain assembly. In order to register it there, and at the same time apply all our changes, you need to do the following.



In the Model Explorer, select the Domain assembly, go to Properties, and in the Text Template Binding paragraph, poke a “...” button. We add a new item, in the Project Path field we indicate the name of the project to which the code will be generated, in the Target Directory field we specify the daddy relative to the project (we generate the root) and indicate the address of the template. By default, they are located in the "C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Common7 \ IDE \ Extensions \ Microsoft \ Visualization and Modeling Feature Pack \ 2.0 \ Templates \ Text" folder. You can set several templates for all occasions. In our case, choose ClassTemplate.t4.







After that, right-click the mouse in the free space of the diagram and select the item Generate Code.







And - voila! A new class is added to the assembly, all changes are applied in accordance with the model.







By the way, using stereotypes, it is possible to specify almost everything: the visibility of members, the attributes that are included when generating a namespace, etc.



So, M $ offers us a great tool that makes life easier for architects and developers. Unfortunately, this very necessary package is available only to MSDN subscribers, and the company for some reason does not allow using it to all legal users who wish it. And this is at the cost of VS Ultimate about 300 thousand rubles. Hopefully, this situation will change in the near future.

Source: https://habr.com/ru/post/117216/



All Articles