Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CodeSmith Basic Syntax - Declare and use properties


May 25, 2021 CodeSmith


Table of contents


Basic syntax - Declare and use properties

CodeSmith's core is the template, and what energies the template is the properties, which enable the code template to generate the required code based on the configuration by defining the properties. W hen you use a code template, you must first define values for the properties defined by the template before codeSmith can generate code from the template. S ome properties have default values that can be configured without need. The properties in the template are defined by the Property directive:

<%@ Property Name="ClassName" Type="String" Default="Class1" Category="Context"   Description="The name of the class to generate" Optional="true" %>

Introduction to property parameters:

  • Name: The name of the parameter used by the template.
  • Type: The parameter type can be any .NET valid data type, such as a simple String type or CodeSmith's SchemaExplorer.DatabaseSchema type. Note that the type must be the type of the base class library, such as string or Int32 instead of string and int.
  • Default: Set the default.
  • Category: Used to illustrate what type of property this property appears in CodeSmith Explorer's property panel, such as pull-down selection, direct input, and so on.
  • Description: A description of this property in the property panel.
  • Optional: Setting whether this property is required, setting true indicates that this parameter value is optional, and setting false, this parameter must have a value.
  • Editor: Indicates what GUI (graphical interface editor) editor is used to enter the value of this property in the property panel.
  • EditorBase: The editor uses the basic type, and if not explained, UITypeEditor is the default editor.
  • Serializer defines the IPropertySerializer type for properties.
  • OnChanged defines event handling code when properties change.
  • DeepLoad is only used in SchemaExplorer objects, and when true, SchemaExplorer obtains all the information about the database Schema at once and avoids querying the database multiple times.

When you configure properties, each property uses a different configuration interface depending on its type and Editor, corresponding to simple types such as Int, String can be edited directly, and for database types you can use Schema Explorer, CodeSmith has defined some property editors in advance, as well as a new property editor that can be customized to define certain special property types, which is described later. Customization is usually not required.

In some cases, if the defined property value is a value in a list, such as in CodeSmith's own template SortList.cst, a property is defined to set visibility for the resulting class:

CodeSmith Basic Syntax - Declare and use properties

This can be done by defining an enumeration type:

<script runat="template">
Public Enum AccessibilityEnum
      [Public]
      [Protected]
      [Friend]
      [ProtectedFriend]
      [Private]
End Enum
</script>

The type of property defined is then defined as this enumerumered type:

<%@ Property Name="Accessibility" Type="AccessibilityEnum"
Category="Options" Description="The accessibility of the class to be generated." %>

Because properties can be defined as optional, you need to detect in the template whether a property has been configured, such as the following defined property as Optional

<%@ Property Name="ClassNamespace" Type="System.String" Optional="True" 
Category="Context" 
Description="The namespace that the generated class will be a member of." %>

In the template, the script needs to detect whether this property has a value that can be implemented by the following code:

<% if (ClassNamespace != null && ClassNamespace.Length > 0)
{ %>namespace <%= ClassNamespace %>{<% }
%>

As you can see from the above, you can use properties directly in your script without prefixing them (such as $).