2

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?

joeriks
  • 3,382
  • 8
  • 32
  • 42

2 Answers2

2

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");
mythz
  • 141,670
  • 29
  • 246
  • 390
  • I tested BiffBaffBoffs suggestion, and it seems to pickup the props of the inherited model, which is very neat, any drawbacks to that method that I dont see right now? – joeriks Mar 20 '13 at 14:42
  • 1
    Not really inheritance works fine on Request DTOs as a stop gap, I just generally don't like [recommending inheritance inside DTOs](http://stackoverflow.com/a/10759250/85785). – mythz Mar 20 '13 at 14:46
  • Ok, got it. Is it even possible to make the new api get the LegacyType route mappings when I use Routes.Add? So I can use JsonServiceClient, client.Post(legacyTypeObject) – joeriks Mar 20 '13 at 15:07
  • Yeah you can use any POCOs to as your service models, problem is if it's not in a [impl and dep-free dll](http://stackoverflow.com/a/15369736/85785) then trying to re-use them on your client may force coupling with Server-only classes and logic, which is generally not ideal to have leak into your client applications. – mythz Mar 20 '13 at 15:15
  • 1
    Right! The DTO project should not have a dependency to my poco project. Makes perfect sense. – joeriks Mar 20 '13 at 15:27
1

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.

CallumVass
  • 11,288
  • 26
  • 84
  • 154
  • Cool that this works! But for clean code I understand I was wrong in the first place having a dependency from my DTO-project to my legacy project. – joeriks Mar 20 '13 at 16:01