5

I'm building a Web API. To give meaning to my controller methods I want classes that specify what properties are used for each particular operation. This would make the code easier to understand, and auto generated api docs would also be more meaningful.

Is there an existing pattern or at least semi standard that can give me some guidance on how to name classes such as those below?

// Used for POST /entities or PUT /entities/:id
class EntityCommandDto
{
 public string EditableProperty { get; set; }
}
// Used for GET /entities as IEnumerable<EntityQueryDto>
class EntityQueryDto : EntityCommandDto
{
 // Id is read only so no need to have it in the base class
 public int Id { get; set; }
}
// Used for GET /entities/:id
class ExpensiveQueryDto : EntityQueryDto
{
 // Don't want this when getting the whole colletion so I created a separate class
 public object ExpensiveRelatedEntity { get; set; }
}
Kilian Foth
111k45 gold badges301 silver badges323 bronze badges
asked Nov 26, 2016 at 22:31
0

1 Answer 1

5

Well you're right about one thing. These names are terrible. What's wrong here is you think telling us about the methods they're used in is going to give us a clue what they should be named.

You should be modeling something. That something needs to be a clear idea. So clear that it has a clear name. The idea should be better than "the data transfer object that is used by this one method I bothered to name"

Tell us what they are for. What they represent. What they model. Give your domain a chance to be expressed.

answered Nov 27, 2016 at 1:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.