REST API design with big (detailed) and small objects

We're facing the issue that we often have classes with lots of properties. But we don't need them for most API calls. So we end up having multiple versions for basically the same business object.

E.g.

public class SmallCountry {     string Code { get; set; }     string Name { get; set; } } 

and

public class Country {     string Code { get; set; }     string Name { get; set; }      string Iso3166_2 { get; set; }     string Iso3166Alpha2 { get; set; }     string Iso3166Alpha3 { get; set; }     long Iso3166Numeric { get; set; }    } 

We discussed different options with Interfaces, Properties, special serializers, dynamic classes, etc. But nothing really convinced us. I'm sure we are not the only ones with this issue.

The classes can be much bigger and sometimes we have lists with hundreds of them. So I think it will make a difference for REST API calls.

We are using C# with an MVC Web API.

Replay

Use composition.

Taking your example, you could define two small types:

public class SmallCountry
{
    string Code { get; set; }
    string Name { get; set; }
}

and

public class IsoCountry
{
    string Iso3166_2 { get; set; }
    string Iso3166Alpha2 { get; set; }
    string Iso3166Alpha3 { get; set; }
    long Iso3166Numeric { get; set; }
}

And then compose the more complex classes from those, eg:

public class Country
{
    SmallCountry SmallCountry { get; set; }
    IsoCountry IsoCountry { get; set; }
}

Category: c# Time: 2016-07-28 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.129 (s). 12 q(s)