3

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; }
}
4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.