Passing objects (OOP) between web pages.


I wish to pass an object (lets call it a Customer Object) to different web pages.  I know I can use Session Variables for this but I would prefer to pass the object as is.  Can I assign the Customer object to a Session 'object', or do I have to break the object up and assign each member to a seperate session variable and then pass those instead. If I have to split it up then doesn't it defeat the whole prupose of OOP?  Thanks in advance.
LVL 1
ProgramVBAsked:
Who is Participating?
 
bmatumburaConnect With a Mentor Commented:
No. Objects are reference types. If you make any changes to any of the object's properties via another variable, the same session object's properties also change.
0
 
tickettCommented:
You can pass the whole object :)

L
0
 
Akin DeluI’m a software engineer, tech enthusiast and UI/UX lover. Commented:
sure you can pass the whole object to a session, but mind you performance wise that might not be good enough.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ProgramVBAuthor Commented:
Ok, so say I have an object called MyCustomer with members:

Name
Address
ID


I want to pass it to another web page, and then extract all its members.  How do I pass it and then subsequently pass it with losing state?
0
 
ProgramVBAuthor Commented:
So if performace is an issue, how to develop applications in ASP.NET the reflect the OOP concepts we employ when developing Winforms? Isn't using session variables just like using Global Variables :(
0
 
Dale BurrellDirectorCommented:
// Setting
MyCustomer ThisCustomer = new MyCustomer ()
// set properties
Session["MySessionId"] = ThisCustomer;

// Accessing
MyCustomer ThisCustomer = Session["MySessionId"] as MyCustomer;
// make use of properties

Need some additional logic to deal with it being null and to clear it when done.
0
 
Dale BurrellDirectorCommented:
As to how to use OOP in asp.net... all depends on what you are trying to achieve... using session variables is the right solution to some problems. Storing in a database is another. Sometimes you need to use both if for example you want to collect data and validate is through a wizard type interface before storing it.

These days web servers are pretty powerful so storing a few items in the session variables isn't necessarily an issue unless you've got 1000's of user logged in at the same time.
0
 
bmatumburaCommented:

' In the calling Page
Dim varCustomer as MyCustomer = New MyCustomer with {.Name = "Edd", .Address = "Plot 20", .ID = 10}
Session("MyCustomer") = varCustomer

' In called Page
Dim varCustomer As MyCustomer = Session("MyCustomer")

Open in new window

0
 
ProgramVBAuthor Commented:
Can you explain you code above. Whats the member MySessionId? I have 3 members:
Name
Address
ID

Or is MySessionId the name you are calling the customer object?
0
 
ProgramVBAuthor Commented:
Ok. What if I want to pass my object to a class method (.vb). Can I pass it to the method as a method input parameter or in the same way as passing to a web page as described in the code above? I dd read somewhere that you might have to use HTTP.Context outside web pages?
0
 
ProgramVBAuthor Commented:
...... Or when passing to .vb classes, does the state remain automatically (and therefore state variables are not required) and only required when passing between web pages that state is lost?
0
 
Dale BurrellDirectorCommented:
When storing stuff in the session variable store you give it a key, you can store pretty much any object you like. Its hard to answer your question without seeing what you are trying to do.... web based apps are in themselves stateless i.e. each request that comes from the browser has no real relationship with the previous request. So you have to adjust your thinking to this. Whatever information you have about a given request is available for the duration of the request i.e. you pass the new page back to the client and the request ends.

If you extract a variable from the session variables you can then pass that object into any classes you create to handle your page processing. Or as you say you can access HttpContext from within your code. Personally I wouldn't do that because it ties that part of your code to being a web app, whereas if you separate it out and pass the object in you can use that code in a web app, a windows app... whereever you like.
0
 
ProgramVBAuthor Commented:
"....If you extract a variable from the session variables you can then pass that object into any classes you create to handle your page processing. Or as you say you can access HttpContext from within your code. Personally I wouldn't do that because it ties that part of your code to being a web app, whereas if you separate it out and pass the object in you can use that code in a web app, a windows app... whereever you like."

So what you mean above is that I can pass my object to a class and its state will remain
ie
ProcessSales(MyCustomer)
where MyCustomer is an object NOT a session object?
0
 
bmatumburaCommented:
@ProgramVB, you'll still need a session object (or a database) to be able to pass the object across pages. Only then can you be able to retrieve the object from the session or database and pass it on to your functions or subroutines.
0
 
ProgramVBAuthor Commented:
But for heavy hits web sites then I should use URL to pass values?
0
 
bmatumburaCommented:
You can use URLs to pass value data types like Strings and Numbers. You cannot pass objects via URLS unless you are persisting them to disk/database in the calling page, passing their IDs as a string in the URL, and retrieving them from disk in the called page. Otherwise, you'll have to use the session object or Server.Transfer as documented here: http://aspalliance.com/151
0
 
bmatumburaCommented:
If you are expecting heavy hits,

I suggest you persist the object to a disk/database and retrieve the persisted object from disk in the called page. This will keep your application's memory footprint at a minimum and won't slow down your server.
0
 
bmatumburaCommented:
and another interesting method of keeping session state: http://articles.techrepublic.com.com/5100-10878_11-5766717.html
0
 
ProgramVBAuthor Commented:
@bmatumbura - I understand that I need to put it into session object when passing between pages but do I need to do this when passing to a.vb class or can i pass it as normal (via imput parameter to function)?  
0
 
Akin DeluI’m a software engineer, tech enthusiast and UI/UX lover. Commented:
wont it be better to pass the ID of the MyCustomer as a querystring instead of the whole object, then use the id to get its info out.
0
 
bmatumburaCommented:
You don't need to do that when passing to a function etc. You simply pass in the name of the variable that contains the object retrieved from the session variable.
0
 
puru1981Commented:
In query string you can pass only value types. a reference type can't be passed through query string.

secondly query string has limitation on characters because of URL Max length.

using session variable is better option if you can manage a state server (if session's content is heavy per user). Each user has a different session. So be aware of this. If website is heavily hit then use session very efficiently. remove the objects when not required.

one more thing here which is not discussed the object should be serializable to be stored in session.

sessionid is auto-generated whenever the first aspx is hit by the client and it remains same until user closes his/her browser or session.abandon is not called. basically it is a store for user specific information. it is like global but only for the particular user no other users can access these values.
0
 
bmatumburaCommented:
To add on to what puru1981 has posted, refer to http://codeforeternity.com/blogs/technology/archive/2007/12/19/handling-asp-net-session-variables-efficiently.aspx when handling large objects
0
 
ProgramVBAuthor Commented:
@bmatumbura:  
"You don't need to do that when passing to a function etc. You simply pass in the name of the variable that contains the object retrieved from the session variable."

Can you explain why state is maintained when passing to class function?
0
 
bmatumburaCommented:
I am assuming that you have created an instance of the class in the "called" page... In this case, you simply call a member method of on the instance of the class passing to it the name of the variable that contains your retrieved session object.
0
 
Akin DeluI’m a software engineer, tech enthusiast and UI/UX lover. Commented:
yeah querystring has its limitation i agree with that but ID of a MyCustomer wont be up to 100 characters, also dont forget that session has its issue too when it comes to multiple tabs browser.
Am concern about the performance, if you have 5000 users connected to your site, thats lots of session and also the size of object you intend to pass to the session also matter, so that you don't just fill up your server memory with session.
0
 
ProgramVBAuthor Commented:
If I return the object from the .vb class to the original web page again, do I have to add it back into the current session?
0
 
puru1981Connect With a Mentor Commented:
No, you don't need to until unless you change the object
0
 
ProgramVBAuthor Commented:
TThanks to all who contributed to this topic. I've divided the points bbetween bmatumbura and puru1981.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.