Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a system where data is stored in a persistent store and read by a server application. Some of this data is only ever seen by the server, but some of it is passed through unaltered to clients.

So, there is a big temptation to persist data - whether whole rows/documents or individual fields/sub-documents - in the exact form that the client can use (eg. JSON), as this removes various layers of boilerplate, whether in the form of procedural SQL, an ORM, or any proxy structure which exists just to hold the values before having to re-encode them into a client-suitable form. This form can usually be used on the server too, though business logic may have to live outside of the object,

On the other hand, this approach ends up leaking implementation details everywhere. 9 times out of 10 I'm happy just to read a JSON structure out of the DB and send it to the client, but 1 in every 10 times I have to know the details of that implicit structure (and be able to refactor access to it if the stored data ever changes). And this makes me think that maybe I should be pulling this data into separate business objects, so that business logic doesn't have to change when the data schema does. (Though you could argue this just moves the problem rather than solves it.)

There is a complicating factor in that our data schema is constantly changing rapidly, to the point where we dropped our previous ORM/RDBMS system in favour of MongoDB and an implicit schema which was much easier to work with. So far I've not decided whether the rapid schema changes make me wish for separate business objects (so that server-side calculations need less refactoring, since all changes are restricted to the persistence layer) or for no separate business objects (because every change to the schema requires the business objects to change to stay in sync, even if the new sub-object or field is never used on the server except to pass verbatim to a client).

So my question is whether it is sensible to store objects in the form they are usually going to be used, or if it's better to copy them into intermediate business objects to insulate both sides from each other (even when that isn't strictly necessary)? And I'd like to hear from anybody else who has had experience of a similar situation, perhaps choosing to persist XML or JSON instead of having an explicit schema which has to be assembled into a client format each time.

share|improve this question

1 Answer 1

Your question should be whether the data will only ever be presented in one way. What happens when in 6 months your customers decide that they want their screens changed around? Or they want another application that uses some of the data that you're storing as JSON? What if you need to search on it in a new way? Need to tie the data into some reports?

Storing your data in it's presentation format makes all of this more difficult. These are some of the reasons abstracting the data layer away from the presentation layer is generally considered a good idea.

However, if you KNOW that the UI is not going to change for a very long time, and you KNOW that your JSON strings will be essentially atomic, and you can write a consistent process that will simply serve your JSON and not need changing even when the JSON does, you could consider it. I still wouldn't recommend it, though.

share|improve this answer
    
I like your answer except for the last paragraph. I don't think you should ever store data in it's presentation format. –  briddums Jul 10 '12 at 14:54
    
Well... it's not really stored in the presentation format, since nothing is showing the JSON directly. It's more of a data exchange format. The client software is responsible for presentation, and as such different clients could display the data differently. –  Kylotan Jul 10 '12 at 16:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.