The beauty of using an ORM like DLinq is that it makes this hydration and persisting process so simple.I do not have to write any SQL as the object model fits right into my architecture.Lets look at how I use this technique to persist changes to and from the match class, as shown in the above diagram this is a simple object that tracks the results of a tennis match between 2 players. The first thing that I do in the Page_Load is serialize the Match class to a memorystream and then use this to set the value of the hidden field ScoresXml. As a result the javascript in the client browser knows what the Scores looks like and changes can be made to the object. This code looks like:
protected void Page_Load(object sender, EventArgs e)
{
Match match = new Match();
XmlSerializer toXML = new XmlSerializer(typeof(Match));
MemoryStream m = new MemoryStream();
toXML.Serialize(m, match); m.Seek(0, SeekOrigin.Begin);
XmlDocument doc = new XmlDocument();
doc.Load(m); ScoresXml.Value = doc.OuterXml;
input id="set11" type="text" class="inputStyle1"
The ScoreNN XML nodes are used to populate the setNN HTML labels thus transferring the server state to the client, with the following code:
for (var j=0; j < 6; j++)
{
//get the first score label
var label = document.getElementById("set" + i);
//set the value to the "one" attribute
var node= root.selectSingleNode("Score" + i++);
label.value = node.text;
if (i%2 == 1)
i = i+8;
}
Then the user can change the scores as needed and the save button will persist the changes to the XML with the code
in the Save method of the $InputHandler object. This is invoked through the client side click handler as follows:
<input id="save" type="button" value="Save" onclick="$InputHandler.Save()" />
The code in save is the reverse of the above code as it is setting the scoreNN nodes in the XML to the values of the setNN HTML input boxes, that code looks like:
for (var j=0; j < 6; j++)
{
var node= root.selectSingleNode("Score" + i);
//set the int node to the corresponding score label (setnn where nn = 11,12,21 ..etc
node.text = document.getElementById("set" + i++).value;
if (i%2 == 1)
i = i+8;
}
PageMethods.SaveNewMatch(root.xml, OnSaveComplete, OnRequestError);
Note the PageMethods.SaveNewMatch which will return the modified XML to the server thru an ATLAS Pagemethod. It is interesting to drill into how this works as PageMethods is a javascript method provided by ATLAS that is defined in the HTML for our page as:
var cm=Sys.Net.PageMethod.createProxyMethod;
cm(this,"SaveNewMatch","XML");

[WebMethod]
public void SaveNewMatch(string XML)
{
Match match = new Match();
XmlSerializer xs = new XmlSerializer(typeof(Match));
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(XML));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
match = (Match) xs.Deserialize(memoryStream);
DataAcess.InsertMatch(match);