1
votes

I am not very much sure weather DTOs should be POCOs or it can depend on any technology. I am thinking, It is better to keep them as POCOs to support Loose coupling and make sure it works with any technology.

From service stack documentation It is mentioned:

The Request and Response DTO's used to define web services in ServiceStack are standard POCO's while the implementation just needs to inherit from a testable and dependency-free IService. As a bonus for keeping your DTO's in a separate dependency-free .dll, you're able to re-use them in your C#/.NET clients providing a strongly-typed API without any code-gen what-so-ever. Also your DTO's define everything Service Stack does not pollute your web services with any additional custom artefacts or markup

But if you see the actual implementation of DTO, It has dependency on Service stack.

[Route("/todos")]
[Route("/todos/{Ids}")]
public class Todos : IReturn<List<Todo>>
{
    public long[] Ids { get; set; }
    public Todos(params long[] ids)
    {
        this.Ids = ids;
    }
}

[Route("/todos", "POST")]
[Route("/todos/{Id}", "PUT")]
public class Todo : IReturn<Todo>
{
    public long Id { get; set; }
    public string Content { get; set; }
    public int Order { get; set; }
    public bool Done { get; set; }
}

I am totally confused with what mentioned in the documentation and what is actually implemented. Why do we need to make DTO dependent on technology, Its better to keep them clean and technology independent.

My understanding may be completely wrong.

1

1 Answers

3
votes

Any metadata attributes that are added to DTO's are stored in the dependency and impl-free ServiceStack.Interfaces project. Annotating the DTOs have the benefit of being available to C# clients which are able to re-use the custom route attributes in the New API and call services using the pretty urls rather than the fallback pre-defined routes.

The IReturn<T> interface marker allows more succinct typed API calls as seen in this answer.

You don't need to use the attributes as you can use ServiceStack's built-in fluent API to define custom routes, e.g:

public void Configure(Container container)
{
    Routes
      .Add<Todos>("/todos")
      .Add<Todos>("/todos/{Id}")
      .Add<Todo>("/todos", "POST")
      .Add<Todo>("/todos/{Id}", "PUT");
}