Andy T
Andy T

Reputation: 9881

Custom DTOs for Web services

I currently have an MVC website and am about to create an API to expose limited functionality to a mobile client. My site has a logic layer where I have DTOs that I use to display information on the site.

When creating an API, should I expose these same DTOs to the client or should I create DTOs specific for use in the API? I will be creating new controllers and action methods, so might make sense to also create API-specific DTOs.

I feel that creating an API forces you into a contract with the mobile client so that changes are harder to make. Having separate DTOs allows you to freely make changes to DTOs that are only used on the site.

However, the down side is that I am basically duplicating my code, since the DTOs will basically be the same.

Thoughts?

Upvotes: 1

Views: 148

Answers (1)

Mahesh Velaga
Mahesh Velaga

Reputation: 21971

IMO, Decision on this would mainly be guided by what all data do I need to pass to the view, if its the same in both cases, I would go with the same DTOs. If this is not the case, then I would create a base class for a DTO which has the common stuff across APIs for a particular view, and inherit from that base DTO and add more properties in the derived class specific to the API needs.

Upvotes: 1

Related Questions