question

TimothyFischer-5097 avatar image
0 Votes"
TimothyFischer-5097 asked DuaneArnold-0443 answered

2 Classes same name one interface needed

So, I have a solution which pulls json from a REST API. The problem is that There are multiple APIs that return the same class name in the response:

eg: /../GetTwoNumbers

The response comes in a JSON message which includes a "result" element with an int with the two numbers


another api might be:
../../GetTwoAddresses
The response comes in a JSON element which includes a "result element" with child nodes like
Address1,
Address2,
City,
State,
Zip
...

I am wondering if there is a way to create an interface that can get both types of "result" elements like this:

public interface IResponse
{
public DateTime DateReceived {get;set;}

public string Process {get;set;}

public Result result {get;set;}
}

If that won't work, I suppose I could create separate interfaces, but is there a way to make the elements from the class I don't want to use nullable -- as I understand you need to implement all elements of an interface.

Thanks for the help

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered

IMO, any Jason coming back to the client should be converted into a class/object like a DTO. I don't see an interface being viable in this situation.

https://en.wikipedia.org/wiki/Data_transfer_object

VS has the ability to a Json data example and convert it into a class.

https://www.matthewproctor.com/json-to-c-sharp-class-using-paste-special/

Like in the below definition of DTO that can contain other DTO types as list within the single DTO. The main container DTO can have lists or a single DTO object definition with in it.

 public class DtoCache
 {
     public List<DtoProjectType> ProjectTypes { get; set; } = new List<DtoProjectType>();
     public List<DtoStatus> Statuses { get; set; } = new List<DtoStatus>();
     public List<DtoResource> Resources { get; set; } = new List<DtoResource>();
     public List<DtoDuration> Durations { get; set; } = new List<DtoDuration>();
 }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.