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

Quick prototyping with XML/XSD and T4 templates

4.97/5 (14 votes)
20 Dec 2011CPOL4 min read 37.6K   1.1K  
T4 templetes with built-in support for CRUD operations, that utilize XML/XSD for DAL/BLL generation.

Introduction

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

Background

The original purpose of the prototype is to allow users of the software to evaluate the 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 generate entities accordingly, and then populates the entity collection from an XML file. The process is shown in the below diagram. The generated classes are partial classes so that we can extend the function list according to our needs without touching the original templates.

Image 1

How it works

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

Image 2

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 the XSD specifies.

Image 3

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

Image 4

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

Image 5

This template is designed keeping 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 the case of adding a new entity, you have to add the new element in the XSD and then according to the newly added element, you have to provide a separate XML data file. The XML file name should be plural, like "Users"/"Contacts"/"Orders", not "User"/"Contact"/"Order", etc. For editing, take one of the existing elements from the XSD, modify and make the same modifications in the respective XML file. In the case of delete, remove the element from the XSD first, then delete the respective XML data file from the APP_Data folder. Last, hit "Transform All Templates" from Solution Explorer.

XML
// 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 a good understanding of T4 templates, keywords, and scriptlets that are used. This is an excellent resource that I found useful while working with T4 templates.

Further work

Further enhancement in the provided T4 templates can be done in the following areas:

  • Multiple relationships between entities
  • Automated UI generation using a T4 template
  • Automating basic unit test cases

These are planned to be included in later versions. I am open to any suggestions regarding improvement of templates.

References

Thanks

I want to thank Mohammad Ashraful Alam for the initial idea and the inspiration.

History

  • QPro v1.0: Basic version with single parent child relationship.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)