0
votes

I am a bit confused with ServiceStack 'old' and 'new' API and need some clarification and best practices, especially with Request / Response DTO's and routing. I watched some courses on Pluralsight and have the first three books listet on servicestack.net in my electronic bookshelf.

I like to 'restify' an existing application which is built using DDD patterns which means I have a high level of abstraction. The client is WPF and follows the MVVM pattern. I have 'client side service', 'server side service' and repository classes (and some aggregates too). I use NHibernate 4 (with fluent API and a code-first approach) as ORM. Only my repository classes know about the ORM. I have DTO's for all my Entity objects and in my WPF client I only work with those DTOs in the ViewModel classes. I heavily use AutoMapper to 'transfer' Entity objects to my DTO's and vice versa.

My confusion starts exactly with these DTO's and the Request / Response DTOs used in ServiceStack. Here is a very much simplified example of an Address Entity which illustrates the problem:

All my Entity Objects derive from EntityBase which contains basic properties used in all Entities:

public abstract class EntityBase : IEntity
{
    public virtual Guid Id { get; protected set; }
    public virtual DateTime CDate { get; set; } //creation date
    public virtual string CUser { get; set; }  //creation user
    public virtual DateTime MDate { get; set; }  //last modification date
    public virtual string MUser { get; set; } //last modification user
    //
    // some operators and helper methods irrelevant for the question
    // ....
}

public class Address : EntityBase
{
    public string Street { get; private set; } 
    public string AdrInfo1 { get; private set; }
    public string AdrInfo2 { get; private set; }
    public string ZipCode { get; private set; }
    public string City { get; private set; }
    public string Country { get; private set; }
}

Of course there are collections and references to related objects which are ignored here as well as database mappers, naming conventions etc. The DTO I have looks like this:

public class AddressDto
{
    public Guid Id { get; set; }  // NHibernate GUID.comb, NO autoincrement ints!!
    public DateTime CDate { get; set; }
    public string CUser { get; set; }
    public DateTime MDate { get; set; }
    public string MUser { get; set; }
    public string Street { get; private set; } 
    public string AdrInfo1 { get; private set; }
    public string AdrInfo2 { get; private set; }
    public string ZipCode { get; private set; }
    public string City { get; private set; }
    public string Country { get; private set; }
}

To use this with ServiceStack I need to support the following:

  1. CRUD functionality
  2. Filter / search functionality

So my 'Address service' should have the following methods:

  • GetAddresses (ALL, ById, ByZip, ByCountry, ByCity)
  • AddAddress (Complete AddressDTO without Id. CDate, CUser are filled automatically without user input)
  • UpdateAddress (Complete AddressDTO without CUser and CDate, MDate and MUser filled automatically without user input)
  • DeleteAddress (Just the Id)

For me it is pretty clear, that all Requests return either a single AddressDto or a List<AddressDto> as ResponseDTO except for the delete which should just return a status object.

But how to define all those RequestDTO's? Do I really have to define one DTO for EACH scenario?? In the books I only saw samples like:

[Route("/addresses", "GET")]
public class GetAddresses : IReturn<AddressesResponse> { }

[Route("/addresses/{Id}", "GET")]
public class GetAddressById : IReturn<AddressResponse>
{
    public Guid Id { get; set; }
}

[Route("/addresses/{City}", "GET")]
public class GetAddressByCity : IReturn<AddressResponse>
{
    public string City { get; set; }
}

// .... etc.

This is a lot of boilerplate code and remembers me a lot of old IDL compilers I used in C++ and CORBA.....

Especially for Create and Update I should be able to 'share' one DTO or even better reuse my existing DTO... For delete there is probably not much choice....

And then the filters. I have other DTOs with a lot more properties. A function approach like used in WCF, RPC etc is hell to code... In my repositories I pass an entire DTO and use a predicate builder class which composes the LINQ where clause depending on the properties filled. This looks something like this:

List<AddressDto> addresses;

Expression<Func<Address, bool>> filter = PredicateBuilder.True<Address>();
if (!string.IsNullOrEmpty(address.Zip))
    filter = filter.And(s => s.Zip == address.Zip);
// .... etc check all properties and dynamically build the filter

addresses = NhSession.Query<Address>()
                .Where(filter)
                .Select(a => new AddressDto
                {
                    Id = a.Id,
                    CDate = a.CDate,
                    //.... etc
                }).ToList();

Is there anything similar I could do with my RequestDTO and how should the routing be defined?

1

1 Answers

1
votes

A lot of questions raised here have been covered in existing linked answers below. The Request / Response DTOs are what you use to define your Service Contract, i.e. instead of using RPC method signatures, you define your contract with messages that your Service accepts (Request DTO) and returns (Response DTO). This previous example also walks through guidelines on designing HTTP APIs with ServicesStack.

Use of well-defined DTOs have a very important role in Services:

You want to ensure all types your Services return are in DTOs since this, along with the base url of where your Services are hosted is all that's required for your Service Consumers to know in order to consume your Services. Which they can use with any of the .NET Service Clients to get an end-to-end Typed API without code-gen, tooling or any other artificial machinery.

DTOs are what defines your Services contract, keeping them isolated from any Server implementation is how your Service is able to encapsulate its capabilities (which can be of unbounded complexity) and make them available behind a remote facade. It separates what your Service provides from the complexity in how it realizes it. It defines the API for your Service and tells Service Consumers the minimum info they need to know to discover what functionality your Services provide and how to consume them (maintaining a similar role to Header files in C/C++ source code). Well-defined Service contracts decoupled from implementation, enforces interoperability ensuring that your Services don't mandate specific client implementations, ensuring they can be consumed by any HTTP Client on any platform. DTOs also define the shape and structure of your Services wire-format, ensuring they can be cleanly deserialized into native data structures, eliminating the effort in manually parsing Service Responses.

Auto Queryable Services

If you're doing a lot of data driven Services I recommend taking a look at AutoQuery which lets you define fully queryable Services without an implementation using just your Services Request DTO definition.