Do I instantiate a property object in Constructor?

Hi I'm using C#, webform and VS2013
I have a class that has several methods in it and a dozen of public attributes.  I was putting an instance of this class object in Session memory but now I'm thinking I should only put in session those public attributes that need to retain the values and not the methods code.   I would then just get the xxxxModel object from session in the code where the original class is instantiated so it always get the attribute value and I only need session to carry the attribute values and not all those method coding.

So, now I created a new class that holds all these attributes and I call it xxxxModel.  in the original class I then remove all the public attributes and put in

public xxxxModel {get; set;};

I'm looking for advice if that is a good way to implement.  Also, where do I instantiate this class, xxxxModel,  In the constructor?

Thank you.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael FowlerSolutions ConsultantCommented:
Unless this class holds a large amount of data it really should not matter so long as it is instantiated before you try to use it. Given this, creating the instance in the constructor would be the best way to ensure that the class is available when needed, otherwise if you use this class in several different methods you would need to check if the class has been instantiated and then create it if needed causing you to duplicate code
lapuccaAuthor Commented:
The original class with those attributes and method code is a total of 368 lines of code.  Every user running this web application would only have one instance of this class object.   However, let say if 20 users using this then 20 class objects would be stored in server memory, is that still okay?

Thank you.
Michael FowlerSolutions ConsultantCommented:
From what you are describing these classes would take up less than 1mb of memory, so no, not a problem
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

As you also asked where you want to instantiate this model, it depend on your requirement. Please read the page life cycle and understand then you can easily decide yourself as per your requirement.
"Session memory but now I'm thinking I should only put in session those public attributes that need to retain the values and not the methods code."

I'm a bit concerned with that statement... In terms of memory usage, your methods and code are not sitting in the session variable at all. Only a reference to the object. The methods are not copied along with each instance. the methods remain in the in-memory copy of the DLL (on copy for the whole applicaiton).
lapuccaAuthor Commented:
What you said there is very interesting.  So, if I instantiate the original class which has all these attributes and 5,6 methods in this class object, when I put this class object into session memory to save it so I don't lose the attributes values when page posts back.  You're saying the session memory will only save the attributes values part of the class object?  wow, never knew that before.  Is there an article that you can point me to?
Thank you.
I can't really find a nice article, but this is actually the logical thing to do in applications... think of a class with 1000 functions (hypothetical)... and 10 properties. If I create 1000 instances of that class... WHY should the functions be copied around? We would be wasting memory. There are reasons why you can only have on version of an assembly per application domain. All the functions/methods are stored in a separate area from the data they operate on. The actual details get technical with virtual function tables and how the compiler handles things like "this" and "me" and so on. In memory organisation, you have concepts like the stack, the heap, static vs instance variables, allocation tables, pointers and so on. Managed code hides these details away from us... but deep down, that is still how programming languages work.

These might help:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lapuccaAuthor Commented:
MlandaT, Thank you for all these links.  They're all good to read to learn about this.  I'm so glad to learn this is how memory is utilized for class object  because it makes so much sense to be efficient for memory utilization and coding efficiency.  Thank you for sharing this area of knowledge!
lapuccaAuthor Commented:
Thank you everyone.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.