Managing Shared Objects – How to Handle Objects Between Back-End and Front-End Services

asp.net-mvccmicroservicesrest

I have multiple REST-ful APIs that I'm using as my "back-end". I also have multiple web apps (.NET MVC). These web apps make calls to the APIs for data. So, an object being returned by an API will be the same as the object being expected on the web app side (serialized from JSON of course).

For example, if my web app makes a call to an API for a Customer object, that same object will be used across both the API and web app projects.

So, the question is, how do you manage this scenario (create NuGet packages for shared objects/interfaces, duplicate code, etc.)?

Best Answer

It is just a DTO with the same properties. I would strongly suggest just copy it between the server and the client.

See also this StackOverflow answer that has the same conclusion

The reason is, in reality, once you have a wire format it is very difficult to change once the system is in production. Unless you deploy server and client at the same time which will include increased risk and downtime. That means normally we cannot just rename fields. A rename will include adding a new field - then at a later release delete the unused field. With this in mind, we see that we gain nothing from sharing the DTO in a NuGet or shared assembly.

Related Topic