Solved

Passing objects (OOP) between web pages.

Posted on 2010-08-25
29
468 Views
Last Modified: 2012-08-13

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.
0
Comment
Question by:ProgramVB
  • 11
  • 9
  • 3
  • +3
29 Comments
 
LVL 11

Expert Comment

by:tickett
ID: 33519142
You can pass the whole object :)

L
0
 
LVL 5

Expert Comment

by:Akin Delu
ID: 33519182
sure you can pass the whole object to a session, but mind you performance wise that might not be good enough.
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33519197
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
 
LVL 1

Author Comment

by:ProgramVB
ID: 33519212
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
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 33519223
// 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
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 33519237
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
 
LVL 11

Expert Comment

by:bmatumbura
ID: 33519255

' 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
 
LVL 1

Author Comment

by:ProgramVB
ID: 33519266
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
 
LVL 1

Author Comment

by:ProgramVB
ID: 33519292
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
 
LVL 1

Author Comment

by:ProgramVB
ID: 33519305
...... 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
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 33519393
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
 
LVL 1

Author Comment

by:ProgramVB
ID: 33519488
"....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
 
LVL 11

Expert Comment

by:bmatumbura
ID: 33519614
@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
 
LVL 1

Author Comment

by:ProgramVB
ID: 33519659
But for heavy hits web sites then I should use URL to pass values?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:bmatumbura
ID: 33519707
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
 
LVL 11

Expert Comment

by:bmatumbura
ID: 33519727
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
 
LVL 11

Expert Comment

by:bmatumbura
ID: 33519736
and another interesting method of keeping session state: http://articles.techrepublic.com.com/5100-10878_11-5766717.html
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33519738
@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
 
LVL 5

Expert Comment

by:Akin Delu
ID: 33519746
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
 
LVL 11

Expert Comment

by:bmatumbura
ID: 33519788
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
 
LVL 9

Expert Comment

by:puru1981
ID: 33519795
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
 
LVL 11

Expert Comment

by:bmatumbura
ID: 33519830
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
 
LVL 1

Author Comment

by:ProgramVB
ID: 33519861
@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
 
LVL 11

Expert Comment

by:bmatumbura
ID: 33519918
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
 
LVL 5

Expert Comment

by:Akin Delu
ID: 33519962
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
 
LVL 1

Author Comment

by:ProgramVB
ID: 33520068
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
 
LVL 11

Accepted Solution

by:
bmatumbura earned 250 total points
ID: 33520472
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
 
LVL 9

Assisted Solution

by:puru1981
puru1981 earned 250 total points
ID: 33520986
No, you don't need to until unless you change the object
0
 
LVL 1

Author Closing Comment

by:ProgramVB
ID: 33521237
TThanks to all who contributed to this topic. I've divided the points bbetween bmatumbura and puru1981.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now