Mapping response object to custom query based on FK from same table


I have table structure in which same primary key is used as foreign key. Logically this foreign key based entity is different than primary key. But to get details of foreign key based id, I am joining the same table in custom query. The query formed by SS also shows details like all the fields associated to that foreign key. However, I can’t map it to flat response object Or probably I don’t know how to map it in response object.

e.g. Table Owner has fields like, id, name, address, OwnerId etc., where OwnerId is field to represent considering fact that owner can also be a customer and whose Id is same as that of any primary key from table (Ownerid = Id). Hence I created query with join like “Select * from Owner Left Join Owner O on O.Id = Owner.OwnerId”. This query returns me all details associated to Owner whose OwnerId = Id. But how can I map it to my flat response object, OwnerReponse whose members are id, name, address, OwnerId ?

I can’t follow this example, please create a runnable example of what you mean on Gistlyn then post the link here:

Please note that all mapping in ServiceStack is 1:1 with a class matching the schema, i.e. there’s no implcit mapping into a flat structure and your class properties need to match the result-set returned. Any transformation that doesn’t map 1:1 with the class you want to map to you will need to implement yourself, which you can do with the help of the built-in Auto Mapping support.

Here is my non-compiling code-

You forgot to save, this just shows the OrmLite Todos example.

public class CustomerResponse
   public int CustomerId { get; set; }
   public string CustomerName { get; set; }
   public string CustomerAddress { get; set; }
   //What should be the mapping member which will give me details of Customer here ?    

You need to list the properties of the dataset returned by the query or you can prefix the table name in the property to specify a column from the particular table as mentioned in Joining Tables docs.

To control how the Response DTO is populated you can create a Custom AutoQuery implementation and add your own mapping logic behind an Extension method, e g:

public object Any(FindMovies query)
    var q = AutoQuery.CreateQuery(query, base.Request);
    var response = AutoQuery.Execute(request, q);
    return response.ToDto();