Reputation: 9803
I've a question about how iI should consume services in ServiceStack from others services.
I've response DTOs for all my requests, witch implements IReturn
interface. With a client like JsonServiceClient
, when I make the requests, it returns the object response with the type implemented in IReturn
, great!
Now that I'm re-factoring a project writing plugins for the services to use in future projects, when I inject a service in another service and make a request, it returns the object type, so I have to cast it to HttpResult
, then cast the object response to the DTO type.
In most of my service method signatures, I've object as return type and then I return an HttpResult
with the response. For example:
public object Get(Request request)
{
return new HttpResult(responseObj);
}
Do I have any alternative, besides consume the services from a client?
I've a service and DTOs just for views, which consume others services from plugins. That's why I need to get the response DTO properly (the services for views act like controllers).
Upvotes: 4
Views: 210
Reputation: 2051
I am afraid have not understood your question and I am out of subject,
but I would use the same response object, changing only the request.
in Service Model
public class TestRequest : IReturn<TestResponse>
{
public int Id { get; set; }
public string name { get; set; }
}
public class LowLevelRequest : IReturn<TestResponse>
{
public int Id { get; set; }
public string name { get; set; }
public string permissions { get; set; }
}
public class TestResponse
{
public bool success { get; set; }
public string message { get; set; }
}
in Routing table
Routes
.Add<TestRequest>("/TestAPI/{Id}", "POST,GET, OPTIONS")
.Add<LowLevelRequest>("/InternalAPI/{Id}", "POST,GET, OPTIONS");
in Services
public TestResponse Post(TestRequest request)
{ JsonServiceClient client=new JsonServiceClient();
LowLevelRequest lowrequest=new LowLevelRequest() { Id=request.Id, name=request.name, permissions="RW" }
return client.Post<TestResponse>(lowrequest);
}
public TestResponse Post( LowLevelRequest request)
{ ....
return new TestResponse() { success=true, message="done" };
}
Upvotes: 2