Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 460115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:50:27+00:00 2026-05-12T22:50:27+00:00

Suppose I have a class like the following: public class Stage { public int

  • 0

Suppose I have a class like the following:

public class Stage
{
    public int ID {get; set;}
    public strint Code {get; set;}
    public string Name {get; set;}

    private List<Machine> _machines;
    public List<Machine> Machines
    {
        get{ return _machines; }
        set{ _machines = value; }
    }

    .........
    .........

    // This method returns all the Stages stored
    // in the Stage-table in the database.
    public static List<Stage> Get()
    {       
    }
}

Now the question is, when should I load the _machines list?

I have anticipated 2 ways:

(1) In the Stage.Get() method:

Like this:

public static List<Stage> Get()
{
    List<Stage> stages = null;

    try
    {
        //Begin transaction...


        //Retrieve all Stages and put them in List<Stage> stages
        .................
        .................

        //End transaction...

        if(stages!=null)
        {
            //Now load Machines for each Stage
            for(int i=0 ; i<stages.Count ; i++)
            {
                //Get Machines by Stage ID and put them on List<Machine>
                stages[i].Machines = Machine.Get(stages[i].ID);
            }
        }       
    }
    catch(Exception ex)
    {
        stages = null;

        throw ex;
    }

    return stages;
}

The only problem with this is, if Machine has a List<T> – property (for example, List<Part> Parts, etc.), and the Machine.Get(stages[i].ID) – method has similar coding, this will end up in a recursive loading of entire table. And like this, the entire database may have been loaded in the memory.

(2) Direct Database Access in the property:

private List<Machine> _machines;
public List<Machine> Machines
{
    get
    { 
        //Get Machines by Stage ID and put them on List<Machine>
        _machines = Machine.Get(this.ID);

        return _machines; 
    }
    set{ _machines = value; }
}

The problem with this is:

(i) This will end up in a huge performance loss:

Stage stage = Stage.Get(...ID...);

foreach(Machine m in stage.Machine)
{
    //Do something
    ..............
}

Coz, everytime a loop of this kind is put into action, database must be accessed.

(ii) get and set have to be handeled differenly in Save(), Update() and so on.

Can anyone suggest me any better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:50:27+00:00Added an answer on May 12, 2026 at 10:50 pm

    Given your design, you shouldn’t need to load the data each time the property is accessed. Instead, you could check to see if it has been set yet and only do the load if needed. This is a lazy loading design.

    private List<Machine> _machines;
    public List<Machine> Machines
    {
        get
        { 
            //Get Machines by Stage ID and put them on List<Machine>
            //only if we have not already done so
            if (_machines == null)
            {
                _machines = Machine.Get(this.ID);
            }
    
            return _machines; 
        }
        set{ _machines = value; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
See the following class public class Parent { private String name; private int age;
Suppose you have a simple class like this: class foo{ private: int* mData; int
Suppose I have something like this: class A { public B mem; public int
Say I have a class, User: public class User { private String name; private
Suppose I have an application with the following code: public class Node { public
Suppose I have following code: public class CBase: AbstractC,IRenderable { //code here } public
Suppose you have a class like the following: public class Container { Element topElement;
Suppose I have a simple Java class like this: public class User { String
Suppose I have the following set up: Public Interface IA Property Name() Property Id()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.