Question

Iam trying to integrate the ServiceStack C# client to connect to the backend ( REST ). FTR, iam in a PCL Library, using MvvMCross as a the framework on top of Xamarin ( if of any intereset )

The basic communication is working, but now i have to map the response models ( DTOs? ) to the DomainModels i use in the Application.

Iam now getting pretty confused what ServiceStack offers, when DTO and DomainModel is different. There is the talking of AutoMapper, or using ConvertTo or how it relates when you DynamicModel.

In case, i have the feeling like a just have a misunderstandingwhat exactly DTO should be, so dont mind me if i mix up anything, ill try to adapt :)

Lets say my DomainModel would look like

public class Checkout {
   public int id;
   public String title;
   public String foo;
}

The response DTO looks like this ( in json )

public class CheckoutDto {    
   public int id;
   public String name;
   public String bar;
}

Now i would like to map name to title and bar to foo when using the ServiceStack client. Iam now not sure how to incorporate the idea of the "response" in the ServiceClient 4.x API comparing to the DTO itself ( should it be the same? )

_restClient.Get<CheckoutResponse>("/checkout/1");

My concrete questions are: - what bet approach should i take with ServiceStack? - should i use AutoMapper with Mapper.Create / Mapper.Map to convert from DTO / toDTO? - do i mix up terminology ( so my question is hard to understand or even to complex? )

I have read the several posts on service-stack regarding this topic, including the docs on servicestack / wiki and also googles resources - i seem to just dont get the point.

Thanks!

Was it helpful?

Solution

I recommend walking through some the existing examples.

Email Contacts is a good example to start with as it takes you through creating a ServiceStack solution from scratch with the recommended structure namely putting all your Data Transfer Objects (DTOs) in a separate assembly, e.g. Project.ServiceModel

Many ORM's like OrmLite lets you persist POCO's in your preferred RDBMS, so in a lot of cases you can just persist your populated DTO directly in the database as-is. But when the schema for your DTOs and Data Models diverge then it's a good idea to map them, which the built-in Auto-Mapping easily lets you do.

When calling services you typically will not need to specify any urls when using ServiceStack's typed C# Service Clients as it will automatically use the preferred route when you pass in the populated Request DTO, e.g:

[Route("/checkout/{Id}")]
public class Checkout : IReturn<CheckoutResponse>
{
   public int Id { get; set; }
   public String Title { get; set; }
   public String Foo { get; set; }
}

You can then use the above Request DTO in your client like:

var client = new JsonServiceClient(BaseUrl);
CheckoutResponse response = client.Get(new Checkout { Id = 1 });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top