C# and Visual Basic Code Generation
Entity Developer incorporates a large number of innovative features which makes it a perfect C# and Visual Basic code generation tool even for the most demanding developers. Using Entity Developer, you can fine-tune the code generation process according to particular needs by flexibly using different types of new templates that are shipped with the application.
Common C# and Visual Basic Code Generator Functionality
Entity Developer code generator lets you use templates to generate source code for ORM models. Entity Developer has a very flexible code generation system, based on templates, written in T4-like Template Language.
Entity Developer has a template gallery with various predefined templates for different use cases. Templates can be added to your model either when you create it in the Create Model wizard or you may add them later via Add Template dialog box.
It is much more convenient to use our templates in the model than usual T4 templates, because Entity Developer manages the process of code re-generation (when text templates are used, code is re-generated only when the template is saved or a specific button is clicked). In other words, if a changed model is saved, its code is automatically regenerated.
Note, that Entity Developer supports integration with Visual Studio refactoring that enables renaming of model objects. For example, when renaming an entity, refactoring will ensure that any references to that class are renamed across your solution. Refactoring is performed when you save your project and is applicable to namespaces, classes and complex types as well as their properties, enums and their members; for LinqConnect, Telerik Data Access, and Entity Framework models it also applies to the context class and its properties.
Entity Developer models can have a freely defined set of templates. This allows generating non-connected code against different templates. The advantage of using Entity Developer is that Entity Developer manages all actions related to code generation. That is, all the templates are re-generated when the model is changed. The Model Explorer window provides a convenient means for managing templates.
When developing a model that at certain moments can become invalid, you may disable template generation by using the Enabled property of a model template.
Templates can be stored both within the model and in Entity Developer resources.
The advantages of the first approach are the following:
- the template is stored in the model and can be stored in a source control system;
- the template can be moved to other computers or into different folders;
- the template is editable.
The advantage of the second approach is the following:
- If you need to frequently update versions of Entity Developer, you can always be sure that your template remains actual.
- It is possible to add templates to the model both at the creation stage and at any other moment using a handy editor functionality.
- the template is editable.
To add a template stored on the disk to the model, you should use the Add Existing command in the editor.
Code generation can be configured with the template properties, which are declared with the property directive in the templates.
Properties declared in this way are then displayed in the properties window and can be easily edited. In addition to standard properties of the property directive, Entity Developer supports the definition of a group and description.
The standard T4 generator implements the IVsSingleFileGenerator interface and by default generates one file per template. Entity Developer extends this behavior using the Output class. Example: the entity-generating template generates one single cs file that is included into the .tt file project item, which is not very convenient in large projects.
All templates have the FilePerClass option as well as the EntitiesOutput and ContextOutput. EntitiesOutput and ContextOutput provide a capability that slightly differs in Visual Studio and in the stand-alone version, as described below.
If you are using Entity Developer as a Microsoft Visual Studio add-in, you can define the project and project folder to store generated code for contexts and entities of your model. In the Entity Developer stand-alone application, it is possible to define folders, in which generated code for entities and contexts is to be stored.
This set of options makes the development of multilayer applications easier and more convenient. For example, you can easily create the following structure of the application by setting the following options.
Custom Attributes Support
All templates support the generation of .NET attributes that are defined through a convenient dialog in the designer. In the designer, for example, you can define the System.ComponentModel.DataAnnotation attributes that will be generated in your classes.
At some stage, the capabilities of the existing templates might seem insufficient and you may want to create new templates (based on the blank template) or to extend the existing ones (for that purpose, you can use the Copy-to-model option when adding a template or make the currently added template refer to a template in the resources).
Custom templates can be easily made shared using the "make shared" option. This allows using custom templates in other models. Shared templates are displayed in the Custom category and are stored in the C:\Program Files\Common Files\Devart\EntityDeveloper\Shared Templates folder.
Templates are created and edited with convenient editor, which provides context-sensitive code completion, error check, syntax highlighting and other features to speed up your work. All errors and warnings are automatically displayed in the Error List window. The Error List displays an error or warning message and the error location. You can easily navigate to the line that caused the error by double-clicking the error in the Error List window. Thus you may find and fix template errors quickly.
Advanced Output Configuration
In custom templates you can use the following additional parameters with the PushOutputRedirection function that can be called inside the template code:
- the name of the parent project item (the so-called Code Behind). This allows flexibly defining where all generated files should be stored within a large project.
- OverwriteBehavior. For partial classes, the new Overwrite Behavior functionality enables the developer to manage how already generated files are treated during subsequent generations. This also allows creating and storing unchanged sample files that can be re-used within a different solution. Using predefined templates enables the fine-tuning of overwrite behavior for code generation. At each generation, it is possible to define whether code should be overwritten, left unchanged or whether the newly generated code is to be appended to the existing one.
- BuildAction. In Visual Studio, this capability allows selecting an option of how a file should be processed during compilation.
- CopyToOutputDirectory. In Visual Studio, this capability allows selecting an option that defines the circumstances, in which the source code should be placed alongside the compiled file.
At certain stage of development, the existing properties of model objects might not be sufficient. Using Entity Developer you can extend the property lists for objects in the model. These extended properties are either declared in the template, or defined in the model, and then used in code generation. For example, you can add the Generate property to every class and then exclude some classes from generated code by changing the value of this property in the designer.