Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Models vs. Modules (37signals.com)
35 points by brm on Feb 4, 2009 | hide | past | favorite | 8 comments


Reminds me of... Favour object composition over class inheritance, only we are talking about mixin inheritance instead of class inheritance.


Summary: Object oriented beats monkey patching.


Actually, the two are orthogonal. You can monkey-patch an aggregation into a model as easily as you can monkey-patch anything else in.

Also, modules aren't monkey-patching. :)

  http://en.wikipedia.org/wiki/Monkey_patch
Lastly, the modules referred to in the article were added statically, not dynamically. We don't use a lot of dynamic module inclusion (though we do it some).


in django i separate model components into different files and then mixin in the relevant fields and methods. for example, suppose i define models Blog and Post in blog.py, and Rating and RatingMixin (extends object not base model). I'd give Rating a method, say Initialize, that gets called when importing the whole models folder. Rating.Initialize() would then mixin all the fields and methods in RatingMixin to the specified models. In this case, it might be useful to give Blog and Post the method "average_method".

Is this good practice?

Also, what's the advantage of keep lots of data in a single row versus multiple tables? Does it effect performance that much (esp with caching)?


Simple question, why not have a db backed Avatar model instead?


It is db-backed. It's just that the fields exist on the people table, instead of in their own table.


Thanks, I never noticed model aggregation before. Looks really useful.


Also if you did it db backed in a separate table, you'd have to do database joins to retrieve avatars, which might have a performance impact.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: