Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

eXtremecode, Generated Code Customization Methods

0.00/5 (No votes)
25 Feb 2011CDDL5 min read 7.1K  
eXtremecode, Generated Code Customization Methods

Thank you for giving a good response for my first post, related to the introduction of eXtremecode ASP.NET Generator. Now it's time to dive further to reveal the meaning of using and saving custom code.

Overview

Usually generated code is not the final output for any project. Some customization in generated code is always required. As there are chances of losing custom work, most of the time developers don't want to take the risk of code regenerating if some project specific modifications have been done in generated code. But normally, it always happens to have to regenerate the code.

Summary

In order to save custom code or code modifications, eXtremecode generator offers two methods of customization. One is by configuring XML and the other is by keeping modifications in custom regions.

Code Customization by configuring XML File

If required customization can be done by configuring XML, eXtremecode recommends to do it by using XML configuration rather than to do it by using custom regions.

XML
<entity name="Company" fieldsCountInSingleRow = "2" 
	gridSortedBy= "ImportedOn" typeCode="BOPage">
    <fields>
      <field name="CompanyId" ordinal="1" displayed="false" />
      <field name="Description" ordinal="2" />
      <field name="RegionCode" ordinal="3" />
      <field name="CountryId" listTable="Country" 
	listTableKeyField="CountryId" listTableDisplayFields="Country" 
	caption="Country" />
      <field name="CurrencyId" listTable="Currency" 
	listTableKeyField="CurrencyId" 
	listTableDisplayFields="Currency" caption="Currency" />
      <field name="ImportedOn" ordinal="40" usingTime="true" />
 </fields>
 </entity>

Custom XML Definition

Before knowing about the list of modifications which can be done by XML configurations, we first need to know about the structure of that XML file.
As this file is nothing but the skeleton of entity definition, so we can call it Custom XML Definition. See my first article (eXtremecode ASP.NET Generator Introduction) for basic details.
Unlike Normal eXtremecode's XML Definition, Custom XML Definition can contain multiple entity elements(<entity>) of the same name. Same named entities are individually identified by different values of typeCode.

typeCode

typeCode identifies the type of output file which is using particular customization.
Type Codes are totally based on template definitions so according to our needs, new Type Codes can be defined in future. Currently we have the following Type codes:

  • BOPage - Common for all pages of particular entity
  • BOListSearch - For search panel of particular entity
  • BOListGrid - For Grid panel of particular entity (result set)
  • BOEditPage - For edit page of particular entity

Entity Level Modifications

User can do the following modifications at entity level:

  • gridKey

    In order to sort paginated grid properly, it is mandatory to define the key for Views and for the Tables which don't have any primary and unique key. Multiple columns can also be provided by separating with comma (,).

  • gridSortedBy

    It is used to define the default sorting for the grid. gridSortedBy will be considered same as gridKey if it is not defined.

  • fieldsCountInSingleRow

    It is used to define number of fields which will be rendered in a single row. It is used by search and edit panel. In the below screen shot of search panel, there are two fields in each row:

Entity Field Level Modifications

User can make the following modifications at entity field level:

  • listTable

    It is used to define the dictionary table of particular filed. System will render this field as dropdown list in web pages. No need to define listTable if relation/FK is already defined in database for this field.

  • listTableKeyField

    If you need to define listTable, you must also need to define the key field of that table.

  • listTableDisplayFields

    It is used to define the display fields for dropdown list. Multiples columns can also be provided by separating with comma (,).

  • formatString

    It is used to define the format of display fields of dropdown list. It must be ensured that format string will be (Format method of .Net String object) compliance.
    Suppose if we define listTableDisplayFields = "UserName,Age" and formatString = "{0} is {1} years old" then, display value in dropdown list will something like "Naveera is 2 years old".

  • usingRange

    It is a flag to define whether the range is taken in search criteria or not. It is only used for all numerics and datetime data types. By default, its value is true.

  • displayed

    It is a flag to identify whether to render this field or not. Default value is true.

  • usingTime

    It is a flag to identify whether to show time with date or not. Default value is false.

  • confirmationRequired

    It is a flag to identify whether a confirmation field is also required or not, e.g. Password Confirmation.

  • ordinal

    It is used to define the order in which field will be rendered in web pages.

  • aspxAttributes

    It is used to define others, aspx webcontrol related attributes. If we define
    aspxAttributes = "TextMode@Password|Visible@True" then it will be rendered into aspx like ( TextMode = "Password" Visible = "True")

Custom Regions

C#
protected void gvCountryBO_RowCreated(object sender, GridViewRowEventArgs e)
{
 switch (e.Row.RowType)
 {
  case DataControlRowType.DataRow:

   /** Custom Region Start [Action Button Server Side Code] **/
            //delete button
   ImageButton btnDelete = e.Row.FindControl("btnDelete") as ImageButton;
   btnDelete.Attributes.Add("onclick",
    string.Format("if (!confirm('{0}')) return false;",
     ResourceProvider.GetGeneralResourceString("Message_WantToDeleteThis")));
   
   /** Custom Region End **/

   break;

  case DataControlRowType.Header:
   //delete selected button
   ImageButton btnDeleteSelected = e.Row.FindControl("btnDeleteSelected") as ImageButton;
   btnDeleteSelected.Attributes.Add("onclick",
    string.Format("if (!confirm('{0}')) return false;",
     ResourceProvider.GetGeneralResourceString("Message_WantToDeleteSelected")));

   break;
 }
}

Customer regions are special code area in generated code, which is supposed not to be modified by eXtremeCode generator if code is regenerated. In other words, code will remain safe in these regions.

Region Syntax

Custom codes can be enclosed by one of the following:

  • For code behind files

    C#
    /** Custom Region Start [Code Generator] **/
    custom code will be here.
    /** Custom Region End **/
  • For HTML files

    HTML
    <!-- Custom Region Start [Code Generator] -->
    custom code will be here.
    <!-- Custom Region End -->
  • For ASPX pages

    ASP.NET
    <%-- Custom Region Start [Code Generator] --%>
    custom code will be here
    <%-- Custom Region End --%>

The text is enclosed by square brackets in the above examples "Code Generator" is the name of custom region in a particular file. Per file, this name should be unique.

Latest Update

As much experience of using eXtremecode generator, it has been identified that most of the time we want to regenerate custom regions as well specially if we want to incorporate new added database fields in our generated code. For resolving this issue, now it is mandatory to attach a tag with each region. Only those custom regions are supposed to have been modified which are tagged. Tag can be any leading word of the text, "Custom Region Start". In the below mentioned example "Modified#" is the tag.

ASP.NET
<%-- Modified# Custom Region Start [Code Generator] --%>
custom code will be here
<%-- Custom Region End --%>

By searching the text, "# Custom Region Start", we can easily identify the regions which we have modified.


License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)