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:

ClassName variable = new ClassName().GetSessionObject();

variable.MethodCall();

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

ClassName variable = new ClassName();

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.

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?

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:

ClassName obj = new ClassName().GetSessionObject();

public class ClassName
{
    public ClassName()
    {
        
    }

    public GetSessionObject()
    {
        return (ClassName)HttpContext.Current.Session["ClassName"];
    }
}

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.

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.

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.

This article has been dead for over six months. Start a new discussion instead.