Example: Generate C# Code
This example shows you how to generate C# code with UModel. You will first create a sample C# namespace that contains a couple of classes, configure the project for code generation, and then generate the actual code.
In this example, the target platform is .NET Standard 2.0 for C# 7.1. As shown in the instructions below, this is possible thanks to a profile built into UModel that defines all the types of the .NET Standard 2.0 for C# 7.1. UModel also includes built-in profiles for specific .NET Framework versions should you need them, see also Including Subprojects.
Create a new UModel project and its structure
On the File menu, click New. This creates an empty project with two default packages ("Root" and "Component View"). Next, right-click the "Root" package, and create a few more packages, as illustrated below. (If you are completely new to the UModel graphical user interface, see the UModel Tutorial and How to Model... chapters to get started.)
In this example, the "Design View" package acts as a container for whatever is going to be the design part of your model (classes and class diagrams, for example), while the "SampleNamespace" package will act as a namespace for all classes that are to be created. In general, however, the package structure is not prescriptive in any way; you may organize your packages in a different way if so required.
Right-click the "Design View" package and select Code Engineering | Set as C# Namespace Root from the context menu. When prompted by UModel that the C# profile will be applied to the package, click OK to confirm. The C# profile built into UModel is now included to the project.
Next, click the "SampleNamespace" package and select the <<namespace>> check box in the Properties window. This applies the "namespace" stereotype to the package and its icon changes to . You can now create classes under this namespace.
So far, the model includes the C# profile, which contains the data types applicable for C#. However, it does not include yet the types specific to .NET Standard 2.0; these are available in a separate UModel profile. To add this profile to the project, do the following:
1.On the Project menu, click Include Subproject.
2.On the C# tab, select .NET Standard 2.0 for C# 7.1 (types only).
4.When prompted to select the include kind, select Include by reference.
The additional profile is now added to the project.
Create C# classes
You can either create classes directly from the Model Tree window, or from a class diagram. For the scope of this example, create a class diagram from the Diagram Tree window as shown below:
This example assumes that all your classes must be generated under the "SampleNamespace" namespace. Therefore, when prompted to select an owner for the diagram, select the "SampleNamespace" package. If you choose a different package, any elements that you add to the diagram will belong to the same package as the diagram (which may or may not be the intended goal).
Next, create the classes, types, and other elements required in your model, for example, a simple diagram that contains an Artist class and a MusicStore class:
In the diagram above, the Artist class was created first. That's because the CreateTestArtists method of the MusicStore class returns a List<Artist>, so it's necessary that the Artist type already exists. For step-by-step instructions about designing classes and their members, see Class Diagrams, as well as the How to Model... chapter.
Optionally, click the MusicStore class on the diagram and add some documentation by typing the text in the Documentation Window. This lets you generate code comments for this class.
Configure the project for code engineering
If you haven't done this yet, save the project to a directory. Next, right-click the "Component View" package in the Model Tree Window and add a new Component (that is, a software component) to it. Click the new software component and, in the Properties window, set the following properties:
•Code language of the component ("C# 7.1", in this example)
•Code generation directory ("C:\codegen", in this example).
Also, ensure that the "use for code engineering" property is set to True.
Next, create a ComponentRealization relationship between the classes from which C# code must be generated and the code engineering component. This can be done either from a Component diagram, or, more simply, as follows:
•In the Model Tree window, click the class to be realized by the component (Artist, in this example) and drag and drop onto the code engineering component (Component1).
Perform the same step for the MusicStore class.
|Note:||In case you forget to create a ComponentRealization relationship for a class, UModel still generates the corresponding code file, even though warnings will be issued in the Messages window. This setting is configurable from Tools | Options | Code Engineering tab (the check box name is Generate missing ComponentRealizations).|
Generate C# code
You can now generate the actual C# code, as follows:
1.On the Project menu, click Merge Program Code from UModel Project. (Alternatively, press F12). A dialog box appears where you can adjust whether changes in code should be merged with those in the code, or overwrite them (if applicable). For the scope of this example, you can select Overwrite... since a new project is getting generated.
2.To include the class documentation as comments in the generated code, click Project Settings, and then select the Write Documentation as DocComments check box. For more information, see Code Generation Options.
3.Click OK. The Messages window displays the code engineering result.
If you have added any documentation to the MusicStore class, notice that it appears as code comments in the generated code: