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.

Ads by Lake Quincy Media

Other Related Items:

Rhinestone Three Tiered Boot Chain JewelryRhinestone Three Tiered Boot Chain JewelryDress up your favorite boots with trendy style.  These fashion boot jewelry chains will transform your ordinary boots to extraordinary. Be stunning wearing Le Bootique's silver toned three tier link boot chain bracelet. There are random inset crystals th
5" Pink Multi-Layered Butterfly Curtain Holder Tieback tiebacks tie backs baby girls nursery room decor5" Pink Multi-Layered Butterfly Curtain Holder Tieback tiebacks tie backs baby girls nursery room decorWe specialize in the largest variety of nylon hanging butterflies, dragonflies, ladybugs, bees, and daisy flowers to capture your imagination and insp... Read More >
Don't Mess With Me, I am a Programmer - T-Shirt (40 colors)Don't Mess With Me, I am a Programmer - T-Shirt (40 colors)Cotton T-Shirt 5.6 oz., heavyweight 100% preshrunk cotton T-shirt, Quarter-turned. Seamless collar, Taped shoulder-to-shoulder, Double-needle stitched neck, sleeves and hemmed bottom. Direct printed on Front of T-Shirt, this is not a transfer. Won't peel or crack in washer/dryer.

Related Post

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

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor