Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ServiceStack DTO TranslateTo and PopulateWith?

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!

like image 617
davidpodhola Avatar asked Oct 23 '13 13:10

davidpodhola


2 Answers

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.

like image 78
Mike Mertsock Avatar answered Nov 01 '22 15:11

Mike Mertsock


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; }
}
like image 36
davidpodhola Avatar answered Nov 01 '22 17:11

davidpodhola