Does it violate the DRY principle to use an MVC server-side framework and a client-side MVC framework

When using a MVC pattern for server side code (in my case django), the model definition is defined once in the model component. When using a client side MVC based library(in my case backbone) the model definition or some subset is redefined. If I were to make a change to my server side model definition, let’s say add a field to a model, then I would have to make a similar model change to my client side model definition to include that new field.

Is this a violation of the DRY principle? Should server side model definitions auto-generate client side javascript based model definitions or is this an acceptable violation of the DRY principle?

Since you posted this under the backbone.js tag, let’s get the obvious answer out first: You’re not violating the DRY principle, because you don’t have to define the fields for Backbone.Model. Your model, at its simplest, can look like this:

var FooModel = Backbone.Model.extend({});

If today your service returns {foo:1}, and tomorrow {foo:1, bar:2}, Backbone will quite happily use the new property.

Let’s assume you were using a framework that required you to define your client model’s properties, the question becomes more debatable…

<opinion>

The best you could do would be to codegen the client-side model properties from the server models. If you wanted to stretch it, you could possibly even generate simple validation code, but that’s about it. Is it worth it? With the additional complexity added by code generation tools, I would say that unless you have hundreds of model types, the answer is a resounding no.

Consider what happens when you change a server model, in each of the following cases:

  • You add a property to the model
  • You remove a property from the model
  • You change a property of the model

Whether your client-side model is automatically generated or not, each of the changes will most likely require you to change some code on the client. Removing and changing a property are breaking changes: your application will most likely break or fail in unpredictable ways if it expects a certain property to be there, and it’s not. Addition is more harmless, but unless you do some client-side code changes, a new property will not be very useful. You’ll have to write code that uses the new property.

Now, if you’re making changes to the client-side code anyway, the price of having to change the model definition is absolutely tiny. Any amount of quasi-theoretical DRY compliance can’t make it worth your while.

I would go even one step further, and actually try to avoid directly mapping your server-side model to your client-side model, because it creates a tight coupling between them. There are numerous occasions when you may need to change the model in either end without changing the other, but if the models are expected to be directly equivalent, the change will propagate unnecessary change in its counterpart. Because in ORM-backed, “Active Model” type patterns the model is a participant in both persistence and business logic, there will be plenty of changes like this.

Instead you should write/generate Data Transfer Objects (DTO) for your server-side models. The DTO should contain a minimal necessary subset of the server model’s properties, which the client needs. The DTO defines the interface between the server and the client. If the client model needs to change, you can change the DTO. If the server model changes, you can change the DTO.

</opinion>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật