2
votes

I like to use the new api i ServiceStack, and have a few Pocos in a legacy project which I like to keep unchanged. However it feels a bit unnessasary to duplicate them to Dto's in my ServiceStack project. Can I be lazy and "pass through" properties to my legacy pocos somehow?

This works nice with a JsonServiceClient, but adds an extra level to the json, which I don't like:

[Route("/legacytype", "POST,OPTIONS")]
public class RequestLegacyTypePost : IReturn<int>
{
    public LegacyTypeLegacyType{ get; set; }
}

public class LegacyTypeService : Service
{
    public object Post(RequestLegacyTypePost request)
    {
        return db.Insert(request.LegacyType);
    }
}

Copy-n-paste properties to a DTO way naturally works fine:

[Route("/legacytype", "POST,OPTIONS")]
public class RequestLegacyTypePost : IReturn<int>
{
    public int Id { get; set; }
    public string SomeProp { get; set; }
    public string OtherProp { get; set; }
}

And perhaps that simplt is best practice?

2

2 Answers

2
votes

ServiceStack lets you re-use any existing Poco as a request DTO, if you don't want to modify an existing legacy dll, then you can non-invasively add Routes using the Fluent API in your AppHost.Configure():

Routes
    .Add<LegacyTypeLegacyType>("/legacytype", "POST,OPTIONS");
1
votes

You could also inherit your model into a custom DTO, something like:

[Route("/legacytype", "POST,OPTIONS")]
RequestLegacyTypeDTO : LegacyType, IReturn<int> { }

That would give you access to all the properties of your original LegacyType model without changing it.