Blog

2009 04 posts (1)

1. API as remote Model

2009-04-15 15:38:05 by Martynas Jusevičius

When developing with the DIY Framework we're using internal XML serialization of objects, and XSLT stylesheets as templates.
I'm not proud of the serialization code, it started as a quick hack to get the data into a usable format, and has improved only a little since then. It is building XML on a string level, when in fact it should be using DOM for that. Object serializations retain their database IDs, on which later they can be joined in the stylesheet from several XML documents.

Later I was developing a RESTful API to publish XML intended to be public and used remotely, not internally. Then I caught myself producing yet another serialization of Model objects using DOM, which was based on HTTP principles and resource URIs.

I soon realized that I was doing a double job, and that a single XML serialization could serve both purposes. It should be based on the API version, i.e. employ URIs and not IDs (and use DOM of course). I would have to adjust the stylesheets (especially joins) accordingly, but that should not be that hard, because both serializations are only supporting 2 general types of content: list of items (array), and a single item.

A useful outcome of such architecture would be that the API would effectively serve as the Model (as in MVC), either internally or remotely. On the other hand, the XSLT stylesheets would also become decoupled, they could be used on the server-side as well as the client-side. They would use internal XSLT processor calls to retrieve the necessary XML documents in the first case, and remote HTTP calls to the API in the second.

Add a comment Comments (13)