简体   繁体   中英

Property name of output model API

Good morning, guys, I need some advice. I'm creating the output templates for my APIs but I'm very confused about the name of the classes. For example, I have an entity called User. In the output model I must return a list of User but it must not center with the entity but with another model created by me for output. Well, I don't know what to name this last class I told you about. I cannot call it User because it conflicts with the real entity. Tips?

There should be at least 3 layers of objects in your code, ViewModel, Dto and Entities.

Each layer should only be able to see the layer directly below it.

So, your service layer, can read Entities from your data layer, but if it exposes any objects, they should be in a Dto.

Then, your presentation layer (UI/API etc), will read from the service layer (DTO), and expose it's objects as ViewModels.

In many cases, this means that all 3 objects (Entity, Dto & ViewModel) have the same repeated properties, but this is to be expected, especially in smaller or newer projects.

This should then solve your naming problems.

Data layer: XXXEntity

Service layer: XXXDto

Presentation layer: XXXViewModel

This explanation is very simplified, and you could solve this problem in many different ways (you could use namespaces instead of class suffixes for example).

The guidance I try to work by is that naming should reflect intent - whereas a User just represents the concept of a 'user' in your domain, an API model is intended to be an API payload containing user information. In your position, I would consider something like UserApiModel or UserPayload.

*As an added note, for my money, the thing that matters the most here is consistency - no matter what you pick, what makes sense to you now may not be the most intuitive thing to you (or anyone else) maintaining the code later. As long as you apply your naming convention consistently across all your API models, don't stress too much about finding the 'right' one - just pick the first one that seems good enough , and keep rolling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM