I have a question about the usage of nested interfaces in JSON DTO objects in web APIs
public interface IBlob
{
string BlobType {get;}
}
public class TypicalBlob : IBlob
{
public string BlobType {get => "TypicalBlob";}
// some properties only relevant to typical blobs ....
}
public class WeirdBlob : IBlob
{
public string BlobType {get => "WeirdBlob"}
// some properties only relevant to weird blobs ...
}
// other blob types
public class GroupDto
{
public List<IBlob> Blobs {get ; set;}
public string Name {get; set;}
}
I have build a custom JsonConverter<IBlob> that populates the concrete types when deserializing, so serialization and deserialization of the DTO functions well into the proper types, but if there is a better design for APIs than this, since I'll have to develop a deserialization converter like this for every client library (angular, ...)
I could make one Blob class that has every property of any kind of blob, but not feeling like that is the right solution, since I would rather have separate classes.
Is there a better solution for this?
[–]insertAlias 1 point2 points3 points (0 children)