Manage an ADAM object in ASP.NET Session state

When developing your own website or webapplication using ADAM, you sometimes need to manage an ADAM object between post-backs.

For this purpose, you can use the WebObjects framework available in the ADAM web framework. But, it could happen you want to manage your object in a different way. If you are in such a situation, there are some little things you need to know to properly manage this ADAM object.

ADAM Application object

First of all, you need an ADAM application object to instantiate this ADAM object. For instance, creating an Classification object:

C#
1
2
3
4
5
6
7
8
9
protected override void OnInit(EventArgs e)
{
  base.OnInit(e);

  Application app = new Application();
  app.LogOn("username", "password");
  Classification myClassification = new Classification(app);

}

The above code requires an ADAM log on everytime your page is hit. Within the ADAM web framework, you can get an logged on Application, by asking it to the AdamHelper:

C#
1
2
3
4
5
6
7
8
protected override void OnInit(EventArgs e)
{
  base.OnInit(e);

  Application app = AdamHelper.DefaultApplication;
  Classification myClassification = new Classification(app);

}

When the user is not logged in, the web framework will redirect the user to the login page. If the user was logged in before, an ADAM log on will occur, based upon the current ADAM session. This will improve your page performance.

This approach has another, less visible advantage. The AdamHelper.DefaultApplication property has a mechanism to perform the log on only once for a request. And it cleans up the Application object when the request is completed. It just makes your work easier.

Session

Next, we want to store this Classification object in the ASP.NET session state, so we can manipulate it between post-backs. When storing this Classification object in the ASP.NET session, you need to know that the Classification object has a reference to the ADAM Application object (this is true for any ADAM object). You should not store this Application object in your session state. So, just before your page is writing its content to the client, you must clear the Application object like this:

C#
1
myClassification.SetApplication(null);

Note that you don't need to call the Dispose method on the Application object when you are using AdamHelper.DefaultApplication. The web framework takes care of that. If you did create the Application object yourself, you still need to Dispose it.

The perfect time to clean up the reference to the Application object in the page life cycle is when the page is unloading. This method gets called, even when an unhandled expection has occured. It is also at this time you should store your object in the session:

C#
1
2
3
4
5
6
7
8
protected override void OnUnload(EventArgs e)
{
  base.OnUnload(e);

  myClassification.SetApplication(null);
  this.Session["classificationobject"] = myClassification;

}

There is only one step left: restoring your object from session state and connecting it back to ADAM.

C#
1
2
3
4
5
6
7
8
9
protected override void OnInit(EventArgs e)
{
  base.OnInit(e);

  myClassification = 
                  this.Session["classificationobject"] as Classification;
  myClassification.SetApplication(AdamHelper.DefaultApplication);

}

Finalizing the code

If we put all the above into one page, you can come up with something like this:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public partial class MyClassificationPage : System.Web.UI.Page
{
  private Classification _classification;

  protected override void OnInit(EventArgs e)
  {
    base.OnInit(e);

    // If this is the inital load of the page
    if(this.IsPostBack == false)
    {
      _classification = new Classification(AdamHelper.DefaultApplication);
      _classification.AddNew(null);
    }
    else
    {
      _classification = 
                  this.Session["classificationobject"] as Classification;
      if (_classification == null)
      {
        throw new Exception("Classification missing.");
      }
      // Remove the classification from the session, 
      // because we don't want an Application object in the session.
      this.Session["classificationobject"] = null;
      _classification.SetApplication(AdamHelper.DefaultApplication);
    }
  }

  protected override void OnUnload(EventArgs e)
  {
    base.OnUnload(e);

    if (_classification != null)
    {
      _classification.SetApplication(null);

      // The application has been removed from set object, 
      // now it is safe to put the object in the session.
      this.Session["classificationobject"] = _classification;
    }
  }

  protected void OnMyButtonClick(object sender, EventArgs e)
  {
    _classification.Name = "Date: " + DateTime.Now.ToLongDateString();
  }
}

Comments

Thursday, 31 March 2011EL ASSAS AKRAM says
Very useful, Thanks.
Leave a comment
You must be logged in to post comments.
Sign in now
 
 
Technical
Business
rss feed