<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>My Fried Tech! - Design Patterns</title>
    <link>http://www.myfriedmind.com/techBlog/</link>
    <description>oddities from my work</description>
    <language>en-us</language>
    <copyright>Matt Mcknight</copyright>
    <lastBuildDate>Wed, 20 May 2009 17:17:13 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>matt@em.org</managingEditor>
    <webMaster>matt@em.org</webMaster>
    <item>
      <trackback:ping>http://www.myfriedmind.com/techBlog/Trackback.aspx?guid=80712240-5305-49d4-aefa-771ef499f69f</trackback:ping>
      <pingback:server>http://www.myfriedmind.com/techBlog/pingback.aspx</pingback:server>
      <pingback:target>http://www.myfriedmind.com/techBlog/PermaLink,guid,80712240-5305-49d4-aefa-771ef499f69f.aspx</pingback:target>
      <dc:creator>papabear</dc:creator>
      <wfw:comment>http://www.myfriedmind.com/techBlog/CommentView,guid,80712240-5305-49d4-aefa-771ef499f69f.aspx</wfw:comment>
      <wfw:commentRss>http://www.myfriedmind.com/techBlog/SyndicationService.asmx/GetEntryCommentsRss?guid=80712240-5305-49d4-aefa-771ef499f69f</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <em>
            <font color="#ff0000">Note: special thanks to Thomas who catches my error in using
QueryString vs url hash - I have made the appropriate changes in this entry...</font>
          </em>
        </p>
        <p>
Part 1 - <a href="http://www.myfriedmind.com/techBlog/2009/06/30/AjaxHistoryAHowToPart1.aspx"><font color="#5c80b1">Introduction</font></a><br />
Part 2 - <a href="http://www.myfriedmind.com/techBlog/2009/07/01/AjaxHistoryAHowToPart2ABasicExample.aspx"><font color="#5c80b1">Basic
Example</font></a><br />
Part 3 - <a href="http://www.myfriedmind.com/techBlog/2009/07/09/AjaxHistoryAHowToPart3ComplexExampleC.aspx"><font color="#5c80b1">Complex
Example</font></a><br />
Part 4 - <a href="http://www.myfriedmind.com/techBlog/2009/07/09/AjaxHistoryAHowToPart4FinalNotes.aspx"><font color="#5c80b1">Final
Notes</font></a><br />
Bonus - <a href="http://www.myfriedmind.com/techBlog/2009/05/20/AjaxHistoryAndTheMementoPattern.aspx"><font color="#5c80b1">Ajax
History and the Memento Pattern</font></a><br />
Extra Bonus - <a href="http://www.myfriedmind.com/techBlog/2009/09/21/Opera9x10xFailingOnAjaxHistoryAndTheHackToFixIt.aspx"><font color="#5c80b1">Issues
with Opera</font></a></p>
        <p>
Recently while watching a <a href="http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?culture=en-US&amp;EventID=1032414968">geekspeak
on Ajax history</a> it struck me that it reminded me strongly of a Memento pattern.
I mentioned this to Glen Gordon and he recommended I write a blog on it, so without
further ado...
</p>
        <p>
First off, a little background. Maintaining history has been one of the holy grails
of Ajax since it began. It is a wonderful feeling to produce a zippy, intiutive site
that has fantastic UI, but once someone hits the "back" button everything goes down
the tube. The reason is simple, browsers were created in the time before, even before
Javascript, much less XML. Back then the "back" button, or any sort of history action,
brought you back not to your last action but to the last page that fully loaded.
</p>
        <p>
To give a parallel, for anyone who has used the "undo" command in whatever program,
what it does is "undo" the last action that you did. Imagine typing for hours in Word,
clicking "undo", and having it wipe out EVERYTHING that you had just done, not just
the last little action. No one would want that, but this is what the "back" key effectively
did by default. Hence, the quest to maintain Ajax history. Wouldn't it be nice if
the "back" key, or other "back" command acted as an "undo" vs a true "back to the
previous page"?
</p>
        <p>
In addition, what if after working on a page (say a maps page) that uses Ajax, you
want to send someone the link. The option is to use a URL, but since Ajax by default
modified the page but not the URL, all that work would be useless. You would think
you were sending them a map to your cabin by the lake, they would simply get the default
map of the world. Not good.
</p>
        <p>
There have been ways to resolve this even before .net 3.5 came out. One merely has
to look at some of the larger Ajax enabled sites (such as google maps) to see this,
but this is such a constant and complex requirement in almost any Ajax development
that it is only logical that someone develop a abstractive pattern to simplify and
standardize it. The 3.5 framework does this, one of many reason it makes me squeal
like schoolgirl. But what is especially fun to look at is how it does it, specifically
what pattern was chosen. The pattern, which is extremely logical to choose, is akin
to the Memento pattern.
</p>
        <p>
I won't go into <a href="http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29">Design
Patterns </a>here. If you are not sure, follow this <a href="http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29">link</a>, <a href="http://en.wikipedia.org/wiki/Design_Patterns_%28book%29">buy
the book</a>, <a href="http://art.cafepress.com/item/black-gof-design-patterns-poster/238970064">get
the poster</a>. I want to focus on one pattern, the Memento pattern and how it relates
to Ajax history in the hopes that it will illumine your coding.
</p>
        <p>
The Memento pattern requires two players, commonly refered to as the Originator and
the Caretaker. To clarify the interaction:
</p>
        <ol>
          <li>
The Caretaker is going to change the Originator in some way but the Originator needs
to be able to undo the change. 
</li>
          <li>
The Caretaker receives a small "memento" object and stores it (hence the Caretaker
name). 
</li>
          <li>
The Caretaker then changes the Originator.</li>
        </ol>
        <p>
If the change needs to be undone:
</p>
        <ol>
          <li>
The Caretaker returns the "memento" object to the Originator. 
</li>
          <li>
The Originator uses the "memento" object to undo the change.</li>
        </ol>
        <p>
But it is also critical to understand about the "memento".
</p>
        <ul>
          <li>
The "memento" is an opaque object, meaning the Caretaker can not change it 
</li>
          <li>
The "memento" is small, it is not the entire Originator, merely the minimal information
needed to rollback state 
</li>
          <li>
The Originator is the one who packages and unpackages the "memento" 
</li>
          <li>
The Originator is the one who rolls back state (ie, the Caretaker does not modify
the Originator to roll it back)</li>
        </ul>
        <p>
So how does this apply to how .Net does Ajax history? In short, .Net uses the URL
of the page as the "memento", the History as the Caretaker, with the code you write
in the framework acting as the Originator.
</p>
        <ol>
          <li>
The Ajax action is going to change the webpage someway. 
</li>
          <li>
The current hash is already stored 
</li>
          <li>
The Ajax action makes the change and alters the hash</li>
        </ol>
        <p>
If the change needs to be undone:
</p>
        <ol>
          <li>
The hash (in the URL) that was from the previous page (the rollback
state) is returned. 
</li>
          <li>
The code that YOU wrote rollsback the state.</li>
        </ol>
        <p>
In this case the memento has these characteristics
</p>
        <ul>
          <li>
The "memento" is a url which is unchanged by the Caretaker (browser) 
</li>
          <li>
The "memento" is small (you should make sure of this) 
</li>
          <li>
The framework writes the URL that you need and allows you to access it 
</li>
          <li>
Your code is what rolls back state 
</li>
        </ul>
        <p>
So, what does this mean? Basically the key point is that you can apply the practices
that you have in place for working with the Memento pattern to working with Ajax history.
Chief among these is that the "memento" should be small! Do not try to squeeze too
much into your URL. It is not pretty and there are limits! Instead pare down to the
minimum of what you need to be able to restore state.
</p>
        <p>
One major bonus is that this not merely allows rollback, but enables your users to
use URLs to return an Ajax-enabled page on which certain steps have already been taken.
This is AMAZING!!!!!!! And compared to how we used to have to do it, it is like a
walk in the park! 
</p>
        <p>
 
</p>
        <img width="0" height="0" src="http://www.myfriedmind.com/techBlog/aggbug.ashx?id=80712240-5305-49d4-aefa-771ef499f69f" />
      </body>
      <title>Ajax history and the Memento pattern</title>
      <guid isPermaLink="false">http://www.myfriedmind.com/techBlog/PermaLink,guid,80712240-5305-49d4-aefa-771ef499f69f.aspx</guid>
      <link>http://www.myfriedmind.com/techBlog/2009/05/20/AjaxHistoryAndTheMementoPattern.aspx</link>
      <pubDate>Wed, 20 May 2009 17:17:13 GMT</pubDate>
      <description>&lt;p&gt;
&lt;em&gt;&lt;font color=#ff0000&gt;Note: special thanks to Thomas who catches my error in using
QueryString vs url hash - I have made the appropriate changes in this entry...&lt;/font&gt;&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
Part 1 - &lt;a href="http://www.myfriedmind.com/techBlog/2009/06/30/AjaxHistoryAHowToPart1.aspx"&gt;&lt;font color=#5c80b1&gt;Introduction&lt;/font&gt;&lt;/a&gt;
&lt;br&gt;
Part 2 - &lt;a href="http://www.myfriedmind.com/techBlog/2009/07/01/AjaxHistoryAHowToPart2ABasicExample.aspx"&gt;&lt;font color=#5c80b1&gt;Basic
Example&lt;/font&gt;&lt;/a&gt;
&lt;br&gt;
Part 3 - &lt;a href="http://www.myfriedmind.com/techBlog/2009/07/09/AjaxHistoryAHowToPart3ComplexExampleC.aspx"&gt;&lt;font color=#5c80b1&gt;Complex
Example&lt;/font&gt;&lt;/a&gt;
&lt;br&gt;
Part 4 - &lt;a href="http://www.myfriedmind.com/techBlog/2009/07/09/AjaxHistoryAHowToPart4FinalNotes.aspx"&gt;&lt;font color=#5c80b1&gt;Final
Notes&lt;/font&gt;&lt;/a&gt;
&lt;br&gt;
Bonus - &lt;a href="http://www.myfriedmind.com/techBlog/2009/05/20/AjaxHistoryAndTheMementoPattern.aspx"&gt;&lt;font color=#5c80b1&gt;Ajax
History and the Memento Pattern&lt;/font&gt;&lt;/a&gt;
&lt;br&gt;
Extra Bonus - &lt;a href="http://www.myfriedmind.com/techBlog/2009/09/21/Opera9x10xFailingOnAjaxHistoryAndTheHackToFixIt.aspx"&gt;&lt;font color=#5c80b1&gt;Issues
with Opera&lt;/font&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Recently while watching a &lt;a href="http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?culture=en-US&amp;amp;EventID=1032414968"&gt;geekspeak
on Ajax history&lt;/a&gt; it struck me that it reminded me strongly of a Memento pattern.
I mentioned this to Glen Gordon and he recommended I write a blog on it, so without
further ado...
&lt;/p&gt;
&lt;p&gt;
First off, a little background. Maintaining history has been one of the holy grails
of Ajax since it began. It is a wonderful feeling to produce a zippy, intiutive site
that has fantastic UI, but once someone hits the "back" button everything goes down
the tube. The reason is simple, browsers were created in the time before, even before
Javascript, much less XML. Back then the "back" button, or any sort of history action,
brought you back not to your last action but to the last page that fully loaded.
&lt;/p&gt;
&lt;p&gt;
To give a parallel, for anyone who has used the "undo" command in whatever program,
what it does is "undo" the last action that you did. Imagine typing for hours in Word,
clicking "undo", and having it wipe out EVERYTHING that you had just done, not just
the last little action. No one would want that, but this is what the "back" key effectively
did by default. Hence, the quest to maintain Ajax history. Wouldn't it be nice if
the "back" key, or other "back" command acted as an "undo" vs a true "back to the
previous page"?
&lt;/p&gt;
&lt;p&gt;
In addition, what if after working on a page (say a maps page) that uses Ajax, you
want to send someone the link. The option is to use a URL, but since Ajax by default
modified the page but not the URL, all that work would be useless. You would think
you were sending them a map to your cabin by the lake, they would simply get the default
map of the world. Not good.
&lt;/p&gt;
&lt;p&gt;
There have been ways to resolve this even before .net 3.5 came out. One merely has
to look at some of the larger Ajax enabled sites (such as google maps) to see this,
but this is such a constant and complex requirement in almost any Ajax development
that it is only logical that someone develop a abstractive pattern to simplify and
standardize it. The 3.5 framework does this, one of many reason it makes me squeal
like schoolgirl. But what is especially fun to look at is how it does it, specifically
what pattern was chosen. The pattern, which is extremely logical to choose, is akin
to the Memento pattern.
&lt;/p&gt;
&lt;p&gt;
I won't go into &lt;a href="http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29"&gt;Design
Patterns &lt;/a&gt;here. If you are not sure, follow this &lt;a href="http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29"&gt;link&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Design_Patterns_%28book%29"&gt;buy
the book&lt;/a&gt;, &lt;a href="http://art.cafepress.com/item/black-gof-design-patterns-poster/238970064"&gt;get
the poster&lt;/a&gt;. I want to focus on one pattern, the Memento pattern and how it relates
to Ajax history in the hopes that it will illumine your coding.
&lt;/p&gt;
&lt;p&gt;
The Memento pattern requires two players, commonly refered to as the Originator and
the Caretaker. To clarify the interaction:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
The Caretaker is going to change the Originator in some way but the Originator needs
to be able to undo the change. 
&lt;li&gt;
The Caretaker receives a small&amp;nbsp;"memento" object and stores it (hence the Caretaker
name). 
&lt;li&gt;
The Caretaker then changes the Originator.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
If the change needs to be undone:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
The Caretaker returns the "memento" object to the Originator. 
&lt;li&gt;
The Originator uses the "memento" object to undo the change.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
But it is also critical to understand about the "memento".
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The "memento" is an opaque object, meaning the Caretaker can not change it 
&lt;li&gt;
The "memento" is small, it is not the entire Originator, merely the minimal information
needed to rollback state 
&lt;li&gt;
The Originator is the one who packages and unpackages the "memento" 
&lt;li&gt;
The Originator is the one who rolls back state (ie, the Caretaker does not modify
the Originator to roll it back)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
So how does this apply to how .Net does Ajax history? In short, .Net uses the URL
of the page as the "memento", the History as the Caretaker, with the code you write
in the framework acting as the Originator.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
The Ajax action is going to change the webpage someway. 
&lt;li&gt;
The current hash is already stored 
&lt;li&gt;
The Ajax action makes the change and alters the hash&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
If the change needs to be undone:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
The&amp;nbsp;hash&amp;nbsp;(in the URL)&amp;nbsp;that was from the previous page (the rollback
state) is returned. 
&lt;li&gt;
The code that YOU wrote rollsback the state.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
In this case the memento has these characteristics
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The "memento" is a url which is unchanged by the Caretaker (browser) 
&lt;li&gt;
The "memento" is small (you should make sure of this) 
&lt;li&gt;
The&amp;nbsp;framework writes the URL that you need and allows you to access it 
&lt;li&gt;
Your code&amp;nbsp;is what&amp;nbsp;rolls back state 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
So, what does this mean? Basically the key point is that you can apply the practices
that you have in place for working with the Memento pattern to working with Ajax history.
Chief among these is that the "memento" should be small! Do not try to squeeze too
much into your URL. It is not pretty and there are limits! Instead pare down to the
minimum of what you need to be able to restore state.
&lt;/p&gt;
&lt;p&gt;
One major bonus is that this not merely allows rollback, but enables your users to
use URLs to return an Ajax-enabled page on which certain steps have already been taken.
This is AMAZING!!!!!!! And compared to how we used to have to do it, it is like a
walk in the park! 
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.myfriedmind.com/techBlog/aggbug.ashx?id=80712240-5305-49d4-aefa-771ef499f69f" /&gt;</description>
      <comments>http://www.myfriedmind.com/techBlog/CommentView,guid,80712240-5305-49d4-aefa-771ef499f69f.aspx</comments>
      <category>Ajax</category>
      <category>Asp.Net</category>
      <category>c#</category>
      <category>Coding Tricks</category>
      <category>Design Patterns</category>
      <category>VB.Net</category>
    </item>
  </channel>
</rss>