Click here to Skip to main content
15,888,026 members
Articles / Web Development / ASP.NET
Article

Implementing state-full functionality behavior of custom object instances in ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.33/5 (7 votes)
1 Nov 20052 min read 26.7K   199   16   2
Providing state-full functionality of objects in ASP.NET.

UML model

Foreword

I wouldn’t be able to write this article without Anastasia. You are wonderful, you are beautiful and you give me inspiration and new strengths. I love you.

Introduction

When you have some business logic that have to be implemented as a class and presented in your program code via an object of the corresponding class, it very often becomes a bare necessity to save the state of this object during aspx page postbacks. Some times it becomes hard to find an easy and convenient solution for this issue because of the stateless nature of HTTP. Hope that this article will help you to find some ideas on how to implement this functionality. The following solutions are based on the Singleton pattern. In short Singleton pattern allows you to manipulate objects (call its properties, methods) and guarantees that there is only one instance of this object presented at runtime.

Using the code

Solution I

This solution forces you to implement two static methods:

C#
// returns singeltone instance from current page Session
public static ShonnCapushonClass GetSingletoneInstance (Page page)
{
 if (page.Session[ShonnCapushonClass.SessionId] != null)
 {
  return (ShonnCapushonClass)page.Session[ShonnCapushonClass.SessionId];
 }
 else
 {
  page.Session[ShonnCapushonClass.SessionId] = new ShonnCapushonClass ();
  return (ShonnCapushonClass)page.Session[ShonnCapushonClass.SessionId];
 }
}

and:

C#
public static void SetSingletoneInstance (Page page, 
                        ShonnCapushonClass instance)
{
  page.Session[ShonnCapushonClass.SessionId] = instance;
}

In the aspx page code-behind, it will look like:

C#
ShonnCapushonClass ShonnaObject = 
         ShonnCapushonClass.GetSingletoneInstance(this);
ShonnaObject.Name = "Tusenna";

When you do not need this object anymore, remove it from the session by calling:

C#
ShonnCapushonClass.SetSingletoneInstance(this, null);

Solution II

This solution will be helpful when all actions of the object is encapsulated in a single aspx page. Let’s define a page property like this:

C#
private ShonnCapushonClass PageShonnaObject
{
  get
  {
    return ShonnCapushonClass.GetSingletoneInstance(this);
  }
  set
  {
    ShonnCapushonClass.SetSingletoneInstance(this, value);
  }
}

In this case, manipulations can be easily done by referencing to the page property:

C#
PageShonnaObject.Name = "PageTysena";
LabelShonnaName.Text = PageShonnaObject.Name;

When you need to reset the object state, just execute the following code:

C#
PageShonnaObject = null;

Solution III

This is the most flexible solution that can be provided without the page instance and in this case your business object will be accessed from code until you explicitly destroy it or the user’s session expires. Also your object will be accessed from any page in your Web application: let’s define the Current property of your class like that:

C#
public static ShonnCapushonClass Current
{
 get
 {
   if ( HttpContext.Current.Session[ShonnCapushonClass.SessionId] == null )
   {
     ShonnCapushonClass shonnCapushon = new ShonnCapushonClass();
     HttpContext.Current.Session[ShonnCapushonClass.SessionId] = shonnCapushon;
     return shonnCapushon;
    }
   else
   {
     return (ShonnCapushonClass)
             HttpContext.Current.Session[ShonnCapushonClass.SessionId];
   }
  }
 set 
 {
   HttpContext.Current.Session[ShonnCapushonClass.SessionId] = value;
  }
 }

After that your business object can be at any page of your application by implementing calls like:

C#
ShonnCapushonClass.Current.Name = "CurrentTysena";

or

C#
ShonnCapushonClass.Current = null;

Summary

In this article I’ve shared some ideas about managing business object state between aspx page postbacks and described some solutions for bypassing the constraint of the stateless nature of HTTP using session state and the Singleton pattern.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Ukraine Ukraine

Comments and Discussions

 
GeneralMore photo needed Pin
Pascal Ganaye2-Nov-05 1:21
Pascal Ganaye2-Nov-05 1:21 
GeneralRe: More photo needed Pin
mbaskey2-Nov-05 7:31
mbaskey2-Nov-05 7:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.