943,640 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 1245
  • ASP.NET RSS
Dec 31st, 2008
0

Custom class object question

Expand Post »
I have a custom class that is an entire structure within itself, many layers deep. However, this object is stored in memory through sessions. I need a way to retrieve the session variable without having to call it through a return method. An Example:

This is what I currently have to do:

ASP.NET Syntax (Toggle Plain Text)
  1. ClassName variable = new ClassName().GetSessionObject();
  2.  
  3. variable.MethodCall();

I want to be able to cast it just like this, and retrieve it from the session at the same time:

ASP.NET Syntax (Toggle Plain Text)
  1. ClassName variable = new ClassName();
  2.  
  3. variable.MethodCall();

I am not sure how to do this since I cannot return something from a constructor, and I cannot assign something to the class with the "this" keyword, or alike.

Thank you.
Last edited by SheSaidImaPregy; Dec 31st, 2008 at 10:05 pm.
Similar Threads
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Jan 1st, 2009
0

Re: Custom class object question

Check out 'class factory' design pattern. You might find it useful for this type of problem.
Also, what does GetSessionObject() do? Does it initialize a private instance of that session object in Class Name?
Reputation Points: 31
Solved Threads: 7
Light Poster
iDeveloper is offline Offline
49 posts
since Jul 2008
Jan 1st, 2009
0

Re: Custom class object question

All GetSessionObject() would do is return the session variable and convert it into the ClassName object.

I have never heard of the class factory design pattern, and was an interesting read. However, it follows the pattern I was hoping to avoid, which would basically be creating the instance of a class and calling another method to return the session object. An example of what I have is below:

ASP.NET Syntax (Toggle Plain Text)
  1. ClassName obj = new ClassName().GetSessionObject();
  2.  
  3. public class ClassName
  4. {
  5. public ClassName()
  6. {
  7.  
  8. }
  9.  
  10. public GetSessionObject()
  11. {
  12. return (ClassName)HttpContext.Current.Session["ClassName"];
  13. }
  14. }

Now what I am trying to do is minimize the request and call only ClassName obj = new ClassName(); instead of calling the additional GetSessionObject(); explicitly. Now it would be easy if ClassName was a container / wrapper, and there was another class beneath it that contained all the information, but this is not the case.

Is it possible to do this? The best, but unavailable, method would be to re-assign the "this" reference, however, this is not allowed through csharp.
Last edited by SheSaidImaPregy; Jan 1st, 2009 at 9:53 am.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Jan 2nd, 2009
0

Re: Custom class object question

Alright, that cleans things up a bit. The proper way to do this would be to create a static method GetSessionInstance() to return an instance of ClassName from session cache. This behavior is not instance-specific, meaning it doesn't and shouldn't rely on an existing instance of ClassName hence it should be static.
As far as overwriting the current class instance. For one thing you really cannot do this. Well, what you CAN do is retrieve your class from session cache and then simply assign every property of your current class instance to the one returned from session. This is ugly and most would tell you not to do this for the sake of software engineering practices.
Hope that helps.
Reputation Points: 31
Solved Threads: 7
Light Poster
iDeveloper is offline Offline
49 posts
since Jul 2008
Jan 2nd, 2009
0

Re: Custom class object question

All I will be doing for this is creating the large object when initialized and stored in a single session variable. When needed, it will be brought in and be a reference, so when updated, it will immediately reflect that within the session state. This is a relatively large application that relies on the session completely and only for this object. There will not be a lot of users for this, at least concurrent. At max, there will be 15 to 20 concurrent users, and more than enough memory, processing power, and everything to handle this at impressive speeds.

And thank you for your help, but that would still involve creating another class or repeated function, which in turn will still require me to reference it the way I am, one way or another. Thank you, though.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: regarding drpo down list
Next Thread in ASP.NET Forum Timeline: a creative question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC