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
{
    get
    {
        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.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Is LINQ Multi Layered?Is LINQ Multi Layered? Several weeks ago, we discussed the point of Multi-Layered Architectures.  We discussed what a multi-layered architecture looks like, and the problems it solves. Today, what I want to […]
  • Tracking Down Performance Issues in ASP.NETTracking Down Performance Issues in ASP.NET Last night, one of my clients assigned me a problem that I thought was going to require one solution, and in the end it was just poor programming. But the process reminded me of the need […]
  • ASP.NET MVC – Model != BLL or DALASP.NET MVC – Model != BLL or DAL Last week I introduced the ASP.NET MVC framework by talking a bit about what the model, view and controller are.In the comments, John Meyer said, I respectfully disagree with your […]
  • ASP.NET Substitution ControlASP.NET Substitution Control Tucked away on the toolbar is a little-used and often overlooked control.  Not using this control could be costing you in performance. The control I’m referring to is the […]
  • HttpContext.Items[] vs Session[]HttpContext.Items[] vs Session[] Since .NET first became available, passing data around during a request has become a lot easier.  The ability to set a property has made that so.  Still, there are times when […]

About Dave Bush

Dave Bush is a .NET programmer and Certified ScrumMaster who is passionate about managing risk as it relates to developing software. When he is not writing or speaking about topics related to Application Lifecycle Risk Management (ALRM), he is an example to his peers as he develops web sites in the ASP.NET environment using industry best practices.

One Pingback/Trackback