5

I am a ServiceStack newbie. I have a quite large .NET C# solution using Cambium ORM.

I am adding ServiceStack WebService project to my solution. I have followed the guide. Working correctly.

Now I want to add UserService:Service returning User DTO using TranslateTo or PopulateWith like written here.

[Route("/user")]
[Route("/user/{Id}")]
public class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}


public class UserService : Service
{
    private Users users = new Users();

    public UserResponse Get(User request)
    {
        return new UserResponse { Result = users.Single(request.Id).TranslateTo<User>() };
    }
}

However I am unable to locate these methods.

I am getting <my_object_returned_from_database> does not contain a definition for 'TranslateTo'.

I did cloned the ServiceStack repository and I cannot find any implementation of those methods in any extension.

What am I missing? Thanks a lot for your help!

Community
  • 1
  • 1
davidpodhola
  • 1,030
  • 10
  • 17

2 Answers2

4

For ServiceStack v3, which is the stable version currently available in NuGet, TranslateTo and related methods are extension methods in the ServiceStack.Common namespace. Note that if you are cloning the GitHub repo, v3 is not the master branch. So adding a using ServiceStack.Common to your file should be sufficient to import the extension method.

ServiceStack v4, the master branch in GitHub, is a preview release. I think the TranslateTo extension method got renamed to ConvertTo.

Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75
  • Thanks @esker, did not notice the other branch :-( However I will need to stay with the `DynamicModel` implementation as the object returned from the database is `dynamic` so `TranslateTo` (probably iterating through properties) has nothing to copy. – davidpodhola Oct 23 '13 at 14:42
2

Just for the future me: although I was able to get the code compiled, it is not copying anything. It is because Cambium ORM returns DynamicModel:Gemini, that has no (DTO-related relevant) properties defined (see below to compare both User classes).

For example my User class in "data-access layer" looks like this:

public class User : DynamicModel, IUser
{
    public User() : base() { }

    public User(object dto)
        : base(dto)
    {
    }

    public Guid InternalId { get; set; }
    public DateTime Created { get; set; }
    public bool IsBlocked { get; set; }

    public static implicit operator idata.User(User u)
    {
        idata.User result = new idata.User();
        u.CopyProperties(result);
        return result;
    }
}

So at the end for DTO I used DynamicModel again and my DTO User class is:

[Route("/user")]
[Route("/user/{Id}")]
public class User : DynamicModel
{
    public User(object dto)
        : base(dto)
    {
    }

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}
davidpodhola
  • 1,030
  • 10
  • 17