Jamie Penney
Jamie Penney

Reputation: 9522

Manually setting the Value of Lazy<T>

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.

Upvotes: 3

Views: 2614

Answers (2)

&#181;Bio
&#181;Bio

Reputation: 10748

Lazy only gets what you ask for, so ask for them together...

public class Foo
{
    public Guid ID { get; set; }
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public Lazy<SubFoo> SubFoo{ get; }
}

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

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500465

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.

Upvotes: 2

Related Questions