Tell me more ×
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.

This is a question I've to-and-fro'd with for a while, and searched for and found nothing on: what're the accepted practices surrounding duplicating domain models in Javascript for a web application, when using a framework like Backbone or Knockout?

Given a web application of a non-trivial size with a set of domain models on the server side, should we duplicate these models in the web application (see the example at the bottom)? Or should we use the dynamic nature to load these models from the server?

To my mind, the arguments for duplicating the models are in easing validation of fields, ensuring that fields that expected to be present are in fact present etc. My approach is to treat the client-side code like an almost separate application, doing trivial things itself and only relying on the server for data and complex operations (which require data the client-side doesn't have). I think treating the client-side code like this is akin to separation between entities from an ORM and the models used with the view in the UI layer: they may have the same fields and relate to the same domain concept, but they're distinct things.

On the other hand, it seems to me that duplicating these models on the server side is a clear violation of DRY and likely to lead to differing results on the client- and server-side (where one piece gets updated but the other doesn't). To avoid this violation of DRY we can simply use Javascripts dynamism to get the field names and data from the server as and when they're neeed.

So: are there any accepted guidelines around when (and when not) to repeat yourself in these situations? Or this a purely subjective thing, based on the project and developer(s)?

Example

Server-side model

class M 
{
    int A
    DateTime B
    int C
    int D = (A*C)
    double SomeComplexCalculation = ServiceLayer.Call();
}

Client-side model

function M(){
    this.A = ko.observable();
    this.B = ko.observable();
    this.C = ko.observable();
    this.D = function() { return A() * C(); }
    this.SomeComplexCalculation = ko.observalbe();
    return this;
}l
M.GetComplexValue = function(){
    this.SomeComplexCalculation(Ajax.CallBackToServer());
};

I realise this question is quite similar to this one, but I think this is more about almost wholly untying the web application from the server, where that question is about doing this only in the case of complex calculation.

share|improve this question
 
Is your question more about how you manage object instances on the JS side for decoupled components, or more about the communication between the client and server? Also, regarding M.getComplexValue(), you may want to look into the "Promise" pattern as a way to minimize callback-hell while allowing all operations to be (potentially) asynchronous. –  Darien Sep 19 at 0:05
 
It's more about whether to use defined objects in JS (as in the example) or an anonymous object with dynamically populated fields –  AndyBursh Sep 19 at 7:41

1 Answer

I think good design patterns and a framework like Breeze can help you out. Don't repeat the domain model on the client again, leave it on the server but use Breeze to pull the model to the client.

Here is a great example of using breezejs with the repository and unit of work patterns.

https://github.com/yagopv/DurandalAuth

The domain model is kept on the server and breeze reads the metadata and creates the entities locally from the server. I think this is a great solution to your problem. You get local entity framework type access locally via JS and you can keep your model on the server. I think it strikes a good balance of the issues you brought up in your question.

share|improve this answer
 
how does this answer the question asked? As it stands, this reads more like a spamvertisement. –  gnat Nov 7 at 21:39
1  
It is a pretty direct answer. The question is to repeat the domain model on the client again. My answer is no leave it on the server but user breeze to pull the model to the client. I am not affiliated with breeze or durandal. I just find the frameworks useful. –  user1564034 Nov 7 at 21:52

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.