2,180

Quick prototyping with XML/XSD and T4 templates

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

Operational prototype can be very handy to give user a operational view of the system that helps them understand better and sometimes that is a technique to elicit hidden requirements as well. One of my recent projects I faced similar scenario where client wants to see how the system will look like before signing off the specification list. So I designed a t4 template which will generate BLL (Business Logic Layer)/DAL (Data Access Layer) on basis of XML/XSD and performs CRUD (Create/Retrieve/Update/Delete) operations in memory. The template is easily configurable according to individuals need.

Background

The original purpose of a prototype is to allow users of the software to evaluate developers’ proposals for the design of the eventual product by actually trying them out, rather than having to interpret and evaluate the design based on descriptions. The approach I adopted here is very simple, T4 templates read the entity definitions from XSD and generates entity according to that, and then populates the entity collection from XML file. The process is shown in the below diagram. Generated classes are partial class so that one can extend the function list according to needs without touching the original templates.

How it works

T4 templates generate and populate DAL entities and BLL objects in simple 2 steps. First T4 template needs to know the data type and the properties of a entity, this can be achieved by providing a XSD where we can define entity properties and data type as attribute. As you can see in the figure below “User” entity has seven properties. Name contains name of the property type is for data type of the respective property and use indicates whether it allows null or not.

Secondly T4 templates seek for xml data to populate entity collections. As the figure shows below each row refers to one user object and the attribute of each user node are properties and must be exact same as XSD specifies.

Thirdly the easy part generates templates. As the figure shows below, T4 template seeks for the XSD in a predefined location and XML data location can be configurable via web.config.

What the template does is read the XSD from the specified location and DataSet is configured according to schema, Thus as many as element is specified in XSD that many DataTable will be configured under same DataSet. Rest is easy from the App_Data XML is read and loaded to the respective DataTablSo now BLL/DAL is ready with built-in basic CRUD operations. Now utilize the CRUD functions to build a prototype. For this example and the attached demo project I used object data source which requires minimal effort to build a UI.

This template is designed keep in mind,

  • Built-in support for object data source
  • In memory operations
  • Partial classes 
  • Basic CRUD operations 
  • Searching
  • Single Parent Child table relationship

 

Using the code

For the attached demo project, you do not have to configure anything, just download, extract, open and run the solution. If you want to add/edit/delete new entities, you have to follow some rules. First in case of adding a new entity first you have to add the new element in XSD and then according to newly added element you have to provide a separate xml data file.XML file name should be plural like “Users”/”Contacts”/”Orders” not “User”/”Contact”/”Order” etc. For editing take one of the existing elements from XSD modifies and made the same modification in respective XML file. In case of delete the element from XSD first then delete the respective XML data file from the APP_Data folder. Last of all hit “Transform All Templates” from solution explorer.

 

Collapse | Copy Code
// XSD for User element 
<xs:element name="User">
    <xs:complexType>
        <xs:attribute name="UserId" type="xs:int" use="required"  />
        <xs:attribute name="UserName" type="xs:string" use="required" />
        <xs:attribute name="FirstName" type="xs:string" use="required" />
        <xs:attribute name="LastName" type="xs:string" use="required" />
        <xs:attribute name="MiddleName" type="xs:string" use="optional" />
        <xs:attribute name="Email" type="xs:string" use="required" />
        <xs:attribute name="Password" type="xs:string" use="required" />
    </xs:complexType>
</xs:element>

If you want to change the existing T4 template, you need to have good understanding of T4 templates keywords, scriptlets that are used. This is an excellent resource that I found useful while working with T4 templates.

prajapat