3

I have a situation where I am querying a RESTful web-service (using .NET) that returns data as XML. I have written wrapper functions around the API so that instead of returning raw XML I return full .NET objects that mirror the structure of the XML. The XML can be quite complicated so these objects can be pretty large and heavily nested (ie. contain collections that in turn may house other collections etc.).

The REST API has an option to return a full result or a basic result. The basic result returns a small subset of the data that the full result does. Currently I am dealing with the two types of response by returning the same .NET object for both types of request - but in the basic request some of the properties are not populated. This is best shown by a (very oversimplified) example of the code:

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 PersonRequest class has two methods that both return a PersonResponse object. However the GetBasicResponse method is a "lite" version - it doesn't populate all the properties (in the example it doesn't populate the LifeHistory collection as this is an 'expensive' operation). Note this is a very simplified version of what actually happens.

However, to me this has a definite smell to it (since the caller of the GetBasicResponse method needs to understand which properties will not be populated).

I was thinking a more OOP methodology would be to have two PersonResponse objects - a BasicPersonResponse object and a FullPersonResponse with the latter inheriting from the former. Something like:

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()
        {
            // ...
        };
    }
}

However, this still doesn't quite "feel" right - for reasons I'm not entirely sure of!

Is there a better design pattern to deal with this situation? I feel like I'm missing something more elegant? Thanks!

3
  • 1
    LifeHistory can return an empty collection, rather than null.
    – Robinson
    Jun 13, 2011 at 15:44
  • @Robinson, this could be misleading, however, as an empty life history could be a valid state from the FullResponse. It may be important to distinguish between data that was not queried and data that is legitimately empty.
    – Dan Bryant
    Jun 13, 2011 at 15:50
  • @Robinson Normally I would, but @Dan Bryant is correct in this instance as null means "no data queried" rather than "no data returned".
    – Dan Diplo
    Jun 13, 2011 at 15:54

2 Answers 2

3

I my opinion you have describe a proxy pattern. See details here: Illustrated GOF Design Patterns in C#

4
  • I think you might be on to something here, but I could do with a clearer example, if possible...
    – Dan Diplo
    Jun 13, 2011 at 16:03
  • I am no pattern expert, but I am not sure I agree. The proxy pattern typically has an internal reference to the actual instance. The proxy is then responsible for instantiating the internal reference at the appropriate time and delegating all requests to the proxy to the instance being proxied. Btw. I would say the example you pointed to on CP is also not ideal, because it exposes the proxied object directly rather than delegating the proxy calls to the proxied object. Jun 13, 2011 at 16:11
  • @Chris Taylor, as I'm understand, you mean an authentication or remote proxy those indeed hide a real object from client. But I'm mention a virtual proxy that sevres as a temporary lightweight object instance and call real data after first call. Maybe the Lazy Loading more widely used term for this pattern. Jun 13, 2011 at 16:31
  • both Protection and Virtual proxies are defined as delegating requests to subject at least in the pure forms. Which fits with the 'Law of Demeter' or the Principal of Least Knowledge. Jun 13, 2011 at 16:49
2

I also have a nagging bad feeling about using inheritance to add on 'extra data', rather than adding/modifying behavior. The main advantage of this is that your methods can specify which level of detail they require in their argument types.

In this particular example, I would be inclined to use the first approach for the data transfer object (the Response object), but then immediately consume this data transfer object to create data model objects, the exact nature of which depends heavily on your specific application. The data transfer object should be internal (as the presence or absence of the data field is an implementation detail) and the public objects or interfaces should provide a view that's more suitable to the consuming code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.