Do I instantiate a property object in Constructor?

Hi I'm using C#, Asp.net 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.
lapuccaAsked:
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
0
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.
0
Michael FowlerSolutions ConsultantCommented:
From what you are describing these classes would take up less than 1mb of memory, so no, not a problem
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

khan_webguruCommented:
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.

https://msdn.microsoft.com/en-us/library/ms178472.aspx
0
MlandaTCommented:
"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).
0
lapuccaAuthor Commented:
MlandaT,
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.
0
MlandaTCommented:
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:
http://stackoverflow.com/questions/1298122/where-are-methods-stored-in-memory
http://stackoverflow.com/questions/18744553/when-instantiating-an-object-does-it-all-get-stored-in-memory
http://www.beingdeveloper.com/memory-layout-of-a-program/
https://en.wikipedia.org/wiki/Virtual_method_table
0

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!
0
lapuccaAuthor Commented:
Thank you everyone.
0
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
C#

From novice to tech pro — start learning today.

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.