Code Generation - Overview

The topics in this section demonstrate how to specify various additional code generation options, such as code generation templates, mapping type and output folder for the generated objects.

Code Generation Options

The Code Generation Options could be specified during the domain model generation, in the Advanced Options Dialog. Once your model is generated, you are still able to change the code generation options. In order to do so, you need to go to the Code Generation Settings tab page in the Model Settings Dialog.

  • Code Generation Template - code generation lets you use templates to generate part of your application's source code and other resources from a model. For example, your model could be a diagram that defines the flow of control through a series of user dialogs. Whenever you edit the model during development, the text templates generate part of the application code and configuration files from the diagram. For example, Telerik Data Access uses T4 templates to generate your domain classes. In the Customizing Code Generation section, you will learn how to customize the code generation templates used by the Visual Designer.

    The code generation templates of Telerik Data Access are updated and the newer versions can be used only in Visual Studio 2010 and Visual Studio 2012. Visual Studio 2008 uses the original ones.

  • Mapping Type - Telerik Data Access maps a database to a domain model by either applying attributes or by using an external XML mapping file. When you use the Attributes Mapping Type, the mapping information is exposed as CLR attributes applied to the domain classes and their properties. A domain class is just like any normal object class that you might define as part of your application, except that it is annotated with special information that associates it with a particular database table. These annotations are made as custom attributes on your class declaration. In case of a XML Mapping Type, the mapping information is preserved in the form of a Xml file.

    There is a third mapping type - Fluent Mapping. However, it can be enabled from a different place. For more information, see the Fluent Mapping Code Generation section below.

  • Generate Context - the Generate Context option specifies if OpenAccessContext will be generated for the domain model or not. This option is useful when you want to separate your domain classes and the context in different assemblies. For more information, please read How to: Define an OpenAccessContext in a Separate Assembly.

  • Generate DataAnnotation Attributes - the Generate DataAnnotation Attributes option specifies if the members of the domain classes should be decorated with the RequiredAttribute, KeyAttribute and StringLengthAttribute attributes. It is useful when you are implementing an ASP.NET MVC or an ASP.NET scenario, since these attributes provide both client-side and server-side validation of the data.
  • Implement INotifyPropertyChanged - this option specifies if the generated domain classes should implement the INotifyPropertyChanged interface. It is useful in data binding scenarios when the client-side needs to be aware that the value of a property is changed.
  • Implement INotifyPropertyChanging - this option specifies if the generated domain classes should implement the INotifyPropertyChanging interface. It is useful in data binding scenarios when the client-side needs to be notified when the value of a property is changing.
  • Implement IDataErrorInfo - this option indicates whether the generated domain classes should implement the IDataErrorInfo interface. You can use it when you need to provide custom error information that the UI can bind to.

    The Generate DataAnnotation Attributes, Implement INotifyPropertyChanged/ing and Implement IDataErrorInfo options are available only in Visual Studio 2010 and Visual Studio 2012.

  • Implement ISerializable - this option indicates whether the generated domain classes should implement the ISerializable interface. You can use it when you need to serialize the objects you retrieve from the database. By default this option is unchecked.

    • Ignore Reference Association - this option indicates whether the reference navigation properties of a given persistent class should be serialized. By default, it is checked and Telerik Data Access will not serialize them.
    • Ignore Collection Association - this option indicates whether the collection navigation properties of a given persistent class should be serialized. By default, it is checked and Telerik Data Access will not serialize them.
  • Generate Multiple Files - if the Generate Multiple Files option is checked, then different file for each object from the domain model will be generated. If you uncheck this option, then all classes will be defined in a single file.

    • Project Folder/File Folder - a destination folder in the solution could be optionally chosen. If a folder is specified, then all objects will be generated in that folder. Those settings are enabled only if the Generate Multiple Files option is set to True.
    • Generate in Nested Namespace - when this option is checked, the Code Generation Engine will respect the class namespaces and generate the classes in the corresponding folders (which will be automatically created by the code generation engine). That setting is enabled only if the Generate Multiple Files option is set to True. For more information, check out How to: Generate Classes in Different Namespaces.

Fluent Mapping Code Generation

The Fluent Mapping Code Generation functionality allows you to mix the Code Only with Database First and Model First approaches. It allows you to generate most of the necessary code automatically as it is done with the Visual Designer domain models. At the same time, since the generated code contains Fluent Mapping API, you can benefit from the capabilities this API offers you for modifying and extending the domain model.

The XML and Attributes mapping types can be enabled from the Advanced Options Dialog or from the Model Settings Dialog. However, the Fluent Mapping code generation can be enabled from a different place. Telerik Data Access provides two templates for Fluent Mapping code generation.

The Telerik Data Access Fluent Library project template will add a new project to the solution and it will run the Create New Model Wizard. This template works in the same way as the Telerik Data Access Class Library project template. However, it will create a new model (from an existing database or an empty one) by using the fluent mapping code generation.

Basically, the Telerik Data Access Fluent Model item template will add a new fluent model to an existing project. This template works in the same way as the Telerik Data Access Domain Model item template. It will run the Create New Model Wizard and it will create a new model from an existing database or an empty one. However the used code generation will be Fluent Mapping.

The third possible way is to use the Model Settings Dialog in an existing .RLINQ file. For more information see the Fluent Mapping.