You can do this but you have to be a bit sneaky. My understanding of it is when you store a home baked object in Session is keeps all the data but loses all the functionality. To regain the functionality you have to "re-hydrate" the data from the session.
Heres an example in JScript for ASP:
<%@ Language="Javascript" %>
<%
function User( name, age, home_town ) {
// Properties - All of these are saved in the Session
this.name = name || "Unknown";
this.age = age || 0;
this.home_town = home_town || "Huddersfield";
// The session we shall store this object in, setting it here
// means you can only store one User Object per session though
// but it proves the point
this.session_key = "MySessionKey";
// Methods - None of these will be available if you pull it straight out of the session
// Hydrate the data by sucking it back into this instance of the object
this.Load = function() {
var sessionObj = Session( this.session_key );
this.name = sessionObj.name;
this.age = sessionObj.age;
this.home_town = sessionObj.home_town;
}
// Stash the object (well its data) back into session
this.Save = function() {
Session( this.session_key ) = this;
},
this.Render = function() {
%>
<ul>
<li>name: <%= this.name %></li>
<li>age: <%= this.age %></li>
<li>home_town: <%= this.home_town %></li>
</ul>
<%
}
}
var me = new User( "Pete", "32", "Huddersfield" );
me.Save();
me.Render();
// Throw it away, its data now only exists in Session
me = null;
// Prove it, we still have access to the data!
Response.Write( "<h1>" + Session( "MySessionKey" ).name + "</h1>" );
// But not its methods/functions
// Session( "MySessionKey" ).Render(); << Would throw an error!
me = new User();
me.Load(); // Load the last saved state for this user
me.Render();
%>
Its quite a powerful method of managing saving state into the Session and can easily be swopped out for DB calls/XML etc. if needed.
Interesting what Anthony raises about threads, knowing his depth of knowledge I'm sure its correct and something to think about but if its a small site you will be able to get away with it, we've used this on a medium sized site (10K visitors a day) for years with no real problems.