Reputation: 13575
This question is not to argue with the design but to understand how can this design pattern be fully leveraged. Now, just to summarize, my takeaway from ServiceStack's design docs was that using coarse-grained systems (DTO pattern) reduces back and forth from the server, along with other benefits like a small service surface area etc.
However, it begs a question that if I really want to take advantage of the coarse-grained Response DTO that the service stack service returns to me as a client, I need to re-use it as much as possible. I have to check existence of that object on the client side before going to server for the data. If I do not do it then I am not taking full advantage of the design and I could be better off with some thing like WebAPI which requires me to go to server each time.
Now, a few questions
Upvotes: 1
Views: 987
Reputation: 143284
Returning coarse-grained responses shouldn't have any impact in what client technology or view pattern you use on the client, i.e. it's not forcing you to maintain a cache on the client.
In Single Page Apps I'll use ServiceStack's TypeScript Add ServiceStack Reference to fetch responses populated in typed Response DTOs which I'll maintain in the client state directly.
The Gistlyn code-base shows examples of this where it uses a generic JsonServiceClient
and TypeScript DTOs to make API end-to-end Typed API Requests, exactly as you would when using any of other ServiceStack's supported Typed Clients (which is a benefit of ServiceStack's Gateway/DTO/Facade approach where API Requests are the same regardless of which language used).
Here's a typical example which fetches all the Variable Info from a Live Running C# Script and loads them directly to a Redux Store:
const client = new JsonServiceClient("/");
const request = new GetScriptVariables();
request.scriptId = state.activeSub.id;
client.get(request)
.then(r => {
store.dispatch({ type: "VARS_LOAD", variables: r.variables });
});
Then to render the view I'll just inject the state maintained in Redux into React Components directly properties using TypeScript decorators, e.g:
@reduxify(
(state) => ({
inspectedVariables: state.inspectedVariables,
})
)
class App extends React.Component<any, any> { ... }
So I'm using just data sourced from the populated DTOs directly and are not mapping them into any interim client View Model. So I don't even have to create the client View Models since they're automatically generated from TypeScript's Add ServiceStack Reference.
In (non-TypeScript) JavaScript databases, the JSON Response is returned as a vanilla JavaScript object which I'll use directly as above, the only difference is not having access to TypeScript's Type Safety for data access.
Some MVVM frameworks prefer a separate View Model, but this is independent if you're using ServiceStack or not, the benefit of using ServiceStack is that it provides a typed DTO for you to map onto your client view models giving you Type Safety benefits.
Upvotes: 3