25 February 2011

eXtremecode, Generated Code Customization Methods.


Thank you for giving good response to my first post, related to the introduction of eXtremecode ASP.Net Generator. Now it's time to dive into further to reveal the mean 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 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 to methods of customization. One is by configuring XML and 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 to do it by using custom regions.

<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 defnition 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 contains 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 out put 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 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 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 coma (,).

  • 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 single row. it is used by search and edit panel. In below screen shot of search panel, there is two fields in each row

Entity Field Level Modifications

User can do 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 coma (,).

  • 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

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 to should not be modified by eXtremeCode generator if code is regenerated. In other word, code will remain safe in these regions.

Region Syntax

custome codes can be enclosed by one of the followings.
  • For code behind files

    /** Custom Region Start [Code Generator] **/
    custom code will be here.
    /** Custom Region End **/

  • For HTML files

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

  • For ASPX pages

    <%-- Custom Region Start [Code Generator] --%>
    custom code will be here
    <%-- Custom Region End --%>
The text enclosed by square barackets in above examples "Code Generator" is the name of custom region in 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's 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 below mentioned example "Modified#" is the tag.
<%-- 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.
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

2 comments: on "eXtremecode, Generated Code Customization Methods."

Chief Editor said...

Excellent work Abdul Wahid. Keep it up!

Do you plan to share it on codeplex? It would help you/users gather specific feedback and trim the functionality in right direction.

Sheikh Abdul Wahid Ahmed said...

I have posted the same on codeproject.

http://www.codeproject.com/Articles/162327/eXtremecode-Generated-Code-Customization-Methods.aspx

Post a Comment