I am looking for a mechanism to transform dataobjects into HTML. The elements in the dataobject are of both, simple and complex types. I have tried playing with HtmlTextWriter for rendering but got stuck with complex types.
Mine is an ASP.Net website project. I have to avoid using server side controls (and therefore do away with built in binding capabilities) as the front end processing is done with the help of jQuery. I need to just churn out basic HTML for my dataobjects and the rest of enrichment (content arrangement and styling) will be done at the frontend.
I am looking for a simple solution (I found Spring.Net an overkill and overwhelming and NHAML also very confusing).
Further, my application is expected to grow over a period of time so I need to have some respect for performance. Therefore I am avoiding bringing XML/XSLT in the picture.
For eg. A Person object will be something like this:
String: Name
Int: Age
Complex Type: Address (includes Street, City, Zip)
Array of Type “Qualification” : Qualifications (includes Degree, Passing Year, Grades)
Desired output is:
<p id="userName" class="userName">John</p>
<p id="age" class="age">35</p>
<div id="address" class="address">
<p id="street" class="street">Express Highway</p>
<p id="city" class="city">Mumbai</p>
<p id="zip" class="zip">400101</p>
</div>
<div id="qualifications" class="qualifications">
<div id="qualification1" class="qualification">
<p id="degree1" class="degree">B.Sc.</p>
<p id="year1" class="year">1990</p>
<p id="grade1" class="grade">A</p>
</div>
<div id="qualification2" class="qualification">
<p id="degree2" class="degree">M.Sc.</p>
<p id="year2" class="year">1992</p>
<p id="grade2" class="grade">A</p>
</div>
</div>
A point to note here is that a mapper would be required to map the properties from the source dataobject, add some metadata to it (like HTML element attributes, etc) and then carry out the transformation.
I’m looking at it as a design problem and elaborate answer on much higher perspective that is design and not the code! The correct way to do this would be as following.
Person type is holding the information and it is data-centric so I recommend not to put any html-rendering responsibility into this class.
First you will need to have an abstract base-class for all your business/data objects. Let us assume [becuase you’ll need to have it] BusinessBase.
So you should start writing a server-control that derives from System.Web.UI.WebContorl. Expose a property that takes an object of type BusinessBase in it’s set accessor.
Now you need define some custom Attributes that is applied to properties of any sub-class of type BusinessBase. This attribute holds the renderring output information for that particular property of the business/data object. Decorate all properties which you want to be renderred in html.
Come back to your web-server-control and via use reflection to iterate through all properties [having your custom-attribute] of object which has been assigned to the server control property of type BusinessBase. Render the html as per the attribute.
Now use this web-server-control and business object in your asp.net front-ends. Have fun.