1

for example, I have an API endpoint, which returns the following model:

public class DeviceDisplayAPI
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }
    [JsonProperty(PropertyName = "online")]
    public bool Online { get; set; }
    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }
    [JsonProperty(PropertyName = "batteryLevel")]
    public int BatteryLevel { get; set; }
    //....
}

I want to use this API in different project. Have I to create a copy of this model or I can "download" this model automatically like it was with WSDL?

2 Answers 2

2

Generally speaking, the consumer of the API, even if it's your own project should be ignorant of the implementation details of the API, including classes like this. The JSON response is a contract, and the client should simply create its own representation of that, if it needs it.

However, given that the consumer is also your project, if you like, you can share it by simply putting it into a class library that both the API and client can reference. I would still encourage that you segregate the two, though. An API is a form of an anti-corruption layer. The entire point is to mediate communication between the client and the backend, which will be speaking two different domain languages.

Sign up to request clarification or add additional context in comments.

1 Comment

I agree with you, that no need to share common API model, both projects should be independent (otherwise not necessary to use WebAPI at all, but can call services directly)
1

one option would be to separate the models into a separate project and create a NuGet package from it, then deploy that to an internal feed, which is not as scary as it sounds.

Then you can reuse it in as many projects as you need.

3 Comments

why need NuGet package if I can just include common library to every project?
you don't unless you plan to use it in other projects which are not part of your current solution
probably, it can have benefits (using nuget packages instead of including project) in some cases, thank you for idea!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.