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 993785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:28:40+00:00 2026-05-16T06:28:40+00:00

I have an object which I create from the response from a webservice. If

  • 0

I have an object which I create from the response from a webservice. If I get a list of these objects, they have a minimal amount of information (in the example below, ID, Prop1 and Prop2 are returned in the list response). If I get the object by ID, the full set of information is returned, including the child objects in Prop3 and Prop4.

public class Foo
{
    public Guid ID { get; set; }
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }

    public Lazy<IEnumerable<Bar>> Prop3 { get; }
    public Lazy<IEnumerable<Bar2>> Prop4 { get; }
}

What I want to be able to do is use this object when partially constructed, but if Prop3 or Prop4 are accessed, make a call to the webservice to download the more detailed data set and fill both Prop3 and Prop4.

If I use Lazy, then I can only fill each property individually by accessing it. This would result in identical webservice calls just to parse a small part of the response each time. What I want to be able to do is create each Lazy like this:

Prop3 = new Lazy<IEnumerable<Foo>>(() => LoadDetailedInformation());

private void LoadDetailedInformation()
{
    // Get info from web service
    Prop3.Value = ParseProp3(response);
    Prop4.Value = ParseProp4(response);
}

So in this pretend implementation of Lazy, the function would be called when the lazy object is accessed, but wouldn’t actually return the data. It would perform some calculations and initialise all lazy values at once.

Am I better off just rolling my own Lazy, or is there another way to do this without writing a ton of wrapper code for each property? One of the classes I am doing this for has about 20 objects that need to be wrapped like this so I don’t want to have to write actual getters and setters if I can get away with it.

  • 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-16T06:28:41+00:00Added an answer on May 16, 2026 at 6:28 am

    It sounds like you want a single Lazy<T> which constructs a compound object with both values in. A Tuple<Bar, Bar2> will to the job:

    public Bar Prop3 { get { return lazy.Value.Item1; } }
    public Bar2 Prop4 { get { return lazy.Value.Item2; } }
    
    private readonly Lazy<Tuple<Bar, Bar2>> lazy =
        new Lazy<Tuple<Bar, Bar2>>(LoadDetailedInformation);
    
    private Tuple<Bar, Bar2> LoadDetailedInformation()
    {
        ...
    }
    

    Of course, instead of the Tuple you could have a DetailedResponse type – I’d recommend that if you end up with more than a few properties. Effectively you want to lazily get the detailed response, and then provide simplified access to individual properties within it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.