Thursday, April 25, 2024
HomeHow ToPowerful Code Generation in Entity Developer

Powerful Code Generation in Entity Developer

New version of Entity Developer comes with completely redesigned template system, which allows much more comfortable template editing and much more flexible code generation.

Code Generation

New template framework allows you to create additional templates for a wide range of tasks:

  • Generating code for other languages than Visual Basic and C#
  • Use custom base class
  • Generate classes without namespace (for example, in Visual Basic, where project has implicit namespace)
  • Implement GetHashCode() and Equals() for the class, which allow to add entities into HashTable.
  • Implement default value setting for properties
  • Create an interface, which contains only public properties, for a class
  • IPOCO
  • Property validation

New T4 like template language features the full power of C# or Visual Basic in the templates, allowing virtually anything you may want for code generation. Organizing iterated or conditional output has never been easier. You may even import your own assemblies and use classes from them.

The model now has two new properties, affecting code generation. They are File Per Class and View Generation.

File Per Class property is available for both LINQ to SQL and Entity Framework models. If it is set to True, each model class will be placed to the separate file when generating code, otherwise, all model classes will be placed into a single file.

View Generation property is available for Entity Framework models. If it is set to True, the code for pre-generating views will be added, that will decrease the first query execution time. Otherwise, the dynamic view generation will be performed when query is executed. It may cause serious performance lack when executing queries first time on a large models. Note that if the generated code is too large, you may encounter problems when working with it in Visual Studio.

Template Editing

Intellisense

Template editor now provides syntax highlighting, code completion, and parameter information features. Syntax highlighting allows you to easily distinguish text and code blocks. Code completion provides you with automatic completion list, containing available objects and methods, and reduce code typing. Parameter information displays method parameter list for all available overloads with brief parameter description and helps you not to make errors when entering method parameters.

You can perform search and replace operations in the template code with Find and Replace window and Incremental search. Increasing and decreasing indent, changing text case operations have become convenient and easy to use. Bookmarks allow you to quickly navigate within the template.

Creating Custom Template

To get acquainted with Entity Developer template editing lets create a template, generating code with property validation.

We will create the C# template for Entity Framework model which will validate length of the data, assigned to string and binary fields. In the default template such validating is not performed, and no exception is raised when assigning too long string to an entity field.

First of all, perform the following steps to create a new template:

  1. In the View menu choose Templates.
  2. Click the New button.
  3. Select Entity Framework in the appeared document
  4. In the Load from existing template drop-down list choose Entity C#.
  5. Specify the name of your new template in the Name box, for example Entity C# with Validation.

Now let’s edit the template code.

Find the GenerateProperty method in the Template text. Then find the lines that create setter.
GenerateProperty method searching

Then select the code, which assigns value to the sting and binary fields.

SourceCode

and replace this code with the following:

TargetCode

The code, generated with this template, will throw InvalidOperationException when assigning the value, exceeding the MaxValue, to the entity field.

RELATED ARTICLES

Whitepaper

Social

Topics

Products