A design pattern for returning a data structure object that may not always be completely populated?

I have a situation where I am requesting a RESTful web service (using .NET) that returns data as XML. I wrote wrapping functions around the API, so instead of returning raw XML, I return full .NET objects that reflect the XML structure. XML can be quite complex, so these objects can be quite large and highly nested (i.e., contain collections, which in turn can contain other collections, etc.).

The REST API has the ability to return a full result or a base result. The main result returns a small subset of the data that gives the full result. I am currently dealing with two types of responses, returning the same .NET object for both types of requests, but some properties are not populated in the main request. This is best shown by a (very simplified) code example:

public class PersonResponse
{
    public string Name { get; set; }
    public string Age { get; set; }
    public IList<HistoryDetails> LifeHistory { get; set; }
}

public class PersonRequest
{
    public PersonResponse GetBasicResponse()
    {
        return new PersonResponse() 
        { 
            Name = "John Doe", 
            Age = "50", 
            LifeHistory = null 
        };
    }

    public PersonResponse GetFullResponse()
    {
        return new PersonResponse() 
        { 
            Name = "John Doe", 
            Age = "50", 
            LifeHistory = PopulateHistoryUsingExpensiveXmlParsing()
        };
    }
}

As you can see, the class PersonRequesthas two methods that return an object PersonResponse. However, the method GetBasicResponseis a "lite" version - it does not fill all the properties (in the example, it does not fill the collection LifeHistory, since this is an "expensive" operation). Please note that this is a very simplified version of what is actually happening.

( GetBasicResponse , ).

, PersonResponse - BasicPersonResponse FullPersonResponse , . - :

public class BasicPersonResponse
{
    public string Name { get; set; }
    public string Age { get; set; }
}

public class FullPersonResponse : BasicPersonResponse
{
    public IList<object> LifeHistory { get; set; }
}

public class PersonRequest
{
    public BasicPersonResponse GetBasicResponse()
    {
        return new FullPersonResponse()
        {
            // ...
        };
    }

    public FullPersonResponse GetFullResponse()
    {
        return new FullPersonResponse()
        {
            // ...
        };
    }
}

"", !

? , - ? !

+3
2

, -. . : GOF #

+3

, / . , , .

( Response), , . ( ), , .

+2

All Articles