ASP.NET Three Tiered w/ Client Side Data

G07L0077 Last week, I created a tool that would allow the user to upload an XML file and have the web site process the file and return a report. All pretty standard stuff until you realize that if you want to use a three-tiered model you have to get the data that the client uploaded into a place that the Business Logic Layer (ObjectDataSource in this case) can see EACH time the object is instantiated.

The obvious solution would have been to store the object into a session variable, but I ruled that out for a number of reasons:

  • I use a session server and wasn’t sure the object I was using was serializable and I didn’t want to go to the trouble of making sure it was.
  • Session objects hang around much longer than I need.
  • There was a cleaner solution available.

One of the little known features of .NET is the HttpContext class, which has a static property named, “Current”. This represents the current round trip as the client requests data from the server. There are a number of objects hanging off of Current. But the one we are interested in today is “Items” which is a collection that works a lot like the Session object. If you only need to store data in a Session object-like thing for the duration of the request and no longer, this is the preferred place to store that information.

What I ended up doing is that I created a Business Logic Layer class that had a property that represented the data that came from the client. Only instead of actually storing the data in a member variable, I stored the data in the HttpContext.Current.Items[] collection.

public MyData Data
        Object o = HttpContext.Current.Items["myData"];
        if (o == null)
            o = new MyData();
            HttpContext.Current.Items["myData"] = o;
        return (MyData)o;

Once I had that set up, everything else worked like a normal multi-layered architecture.

Related Post

One Response to “ASP.NET Three Tiered w/ Client Side Data”