Wednesday, June 23, 2004

I had a request from Scott Hanselman (who has been vocal on the subject of ViewState on many occasions) to share my approach to file-based ViewState persistence that I mentioned in my previous post.  Feel free to comment on my approach.  I need to get code formatting set up.  Pasting from Visual Studio is a pain.

Here's my overrides of my base class page's LoadPageStateFromPersistenceMedium and SavePageStateToPersistenceMedium:


protected override object LoadPageStateFromPersistenceMedium() {
     return _viewStatePersister.LoadViewState();
}

protected override void SavePageStateToPersistenceMedium(object viewState) {
     _viewStatePersister.SaveViewState(viewState);
}


Doesn't tell you much, except I'm delegating persistence to a ViewStatePersister, which looks like (with some things renamed to protect the innocent):


public abstract class ViewStatePersister {
     public ViewStatePersister(BasePage page) {
          _page = page;
     }
     protected BasePage Page {
          get {return _page;}
     }
     BasePage _page;
     public abstract object LoadViewState();
     public abstract void SaveViewState(object viewState);
}


Delegating this responsibility to a separate class gives me finer control over how the persistence happens, as well as modularizing that functionality.  Naturally, I have a class that wraps the default ViewState persistence functionality (which is fairly uninteresting), as well as a FileBasedViewStatePersister.  It extends the DefaultViewStatePersister and harnesses that existing behavior to store a single Guid in the __VIEWSTATE field used to uniquely identify the request.  It's methods of interest look like: (You might look at SaveViewState first so Load makes more sense.  I'm not about to screw with the formatting again to re-order them.)


public override object LoadViewState() {
     object viewState = base.LoadViewState();
     if (viewState != null) {
          Guid guid = (Guid)viewState;
          LosFormatter formatter =
new LosFormatter();
          using (FileStream fileStream = new FileStream(CreateOfflineViewStateFilePath(guid), FileMode.Open, FileAccess.Read, FileShare.None)) {
               viewState = formatter.Deserialize(fileStream);
          }
     }
     return viewState;
}
public override void SaveViewState(object viewState) {
     //create a guid for this viewstate
     Guid guid = Guid.NewGuid();
     LosFormatter formatter =
new LosFormatter();
     using (FileStream fileStream = new FileStream(CreateOfflineViewStateFilePath(guid), FileMode.CreateNew, FileAccess.Write, FileShare.None)) {
          formatter.Serialize(fileStream, viewState);
     }
     //trick the regular system into thinking all it needs to save is the guid
     base.SaveViewState(guid);
}


The appropriate persister is created with a call to a virtual CreateViewStatePersister() method, which a page can use to create the persister of its choice.  As I said in my previous post, I was dissapointed that I had to use the LosFormatter rather than the BinaryFormatter.  The downside of this approach is the possibility for creating LOTS of files, as opposed to creating a single file per session.  But I already have a file cleaning mechanism in place that cleans up files created by my graphing library, which creates more files.

posted on Wednesday, June 23, 2004 10:02:08 AM (Pacific Standard Time, UTC-08:00)  #    Comments [0]
Related posts:
LinqToStdf now on CodePlex
Image Slicer for Deep Zoom in Silverlight 2
Silverlight limitations and Constrained Callvirt in IL
What are the generic Delegates in the framework for?
What is System.__Canon and why is it on my stack?
Adding support for .ashx-based views in ASP.net MVC
Tracked by:
http://www.hanselman.com/blog/PermaLink.aspx?guid=c2e0af97-fd0c-44f4-8cca-114cdb... [Pingback]
http://devbox/blog/PermaLink.aspx?guid=8fd581a1-43d7-4dac-aa69-21d2921ebdc6 [Pingback]
"diet pills and your heart" (diet pills and your heart) [Trackback]
"Updating my "multi-request-safe" File-based ViewState persistence for Whidbey" ... [Trackback]
http://www.marklio.com/marklio/PermaLink,guid,50f8adeb-bef6-43c5-a48e-81575ac99a... [Pingback]
"1970 Pontiac bonneville" (1970 Pontiac bonneville) [Trackback]
"remote backup service" (remote backup service) [Trackback]
"ilo dvd recorder" (ilo dvd recorder) [Trackback]
"alcohol rehab center" (alcohol rehab center) [Trackback]
"samsonite 700 series" (samsonite 700 series) [Trackback]
"radio controlled cars" (radio controlled cars) [Trackback]
"boat transport trailer" (boat transport trailer) [Trackback]
"Triumph the Insult Comic Dog Downloads" (Triumph the Insult Comic Dog Downloads... [Trackback]
"italian handbag" (italian handbag) [Trackback]
"rural houses in majorca" (rural houses in majorca) [Trackback]
"Delorean Replicas" (Delorean Replicas) [Trackback]
"18 and i still wet the bed" (18 and i still wet the bed) [Trackback]
"Pin Striping Tape" (Pin Striping Tape) [Trackback]
"dual channel ram" (dual channel ram) [Trackback]
"Singles Cruises" (Singles Cruises) [Trackback]
"accomplishments of Martin Luther King Jr." (accomplishments of Martin Luther Ki... [Trackback]
"Se47 Full Housing" (Se47 Full Housing) [Trackback]
"samsonite tripods" (samsonite tripods) [Trackback]
"motivation scale" (motivation scale) [Trackback]
"Facts about Georgia" (Facts about Georgia) [Trackback]
"rules for private water easement in greene county" (rules for private water eas... [Trackback]
"Oklahoma City Memorial Marathon relay results" (Oklahoma City Memorial Marathon... [Trackback]
"wholesale plus size lingerie" (wholesale plus size lingerie) [Trackback]
"estetista" (estetista) [Trackback]
"Armoire Hand Painted Furniture" (Armoire Hand Painted Furniture) [Trackback]
"rj-45 CAT5 crossover split pairs wiring pin1" (rj-45 CAT5 crossover split pairs... [Trackback]