Thinking about Models
Posted: Fri Apr 18, 2008 2:32 am
One thing I've been thinking about this week is Models. At first that doesn't sound very interesting, you know you have a thing and a few methods to deal with that thing, insert(), delete(), findby(). But once you think about having more flexibility and more functionality then that it can be quite challenging.
I'm superficially familiar with some of the design patterns like Datamapper, Active record, etc to know it can get quite complex.
The problem I have had with designing my model layers is that at first you write down what pieces of data you have. Then you define their relationships. You start coding a few db classes. But then at some point in time you realize the relationships between the different kinds of data should be changed. Now you can throw all you models in the garbage. There should be better ways.
The thing that got me thinking about it was the thread about onion2k's idea about a photo gallery and how to deal with data. Do you place images (things) in groups/categories/sets/etc etc? As soon as you think traditionally ( a photo goes in an album, each album can have many tags or can be in certain categories, etc) you limit your models to be in a certain way. But as onion2k said, if you let go of that idea and only deal with tags, you might get much more flexibility.
Also take email for example. If you look at gmail, there's basically only Labels. An email arriving is labeled Inbox. An email sent is labeled Sent. You can give as many labels as you want to any email. You can create new labels. You are free to use as many labels as you want on all messages.
And that could work for more situations. Like a blog for example. Traditionally you have Posts, Comments, Categories, Tags and Authors. With certain fields. And certain (limited) relationships. But some people would like to have other sorts of posts, or use posts for other content-type (video, podcasts, etc). Maybe people would like to use other relationships between the different kinds of data. Now what if you just start with a single piece of data, and use only labels. If you label it "Post", it can be used in the traditional sense as a blog post. But you also Label it with "Henry" as the author. And as "PHP" as some sort of category. So you have a lot of flexibility, but without having a specific set of tables and relationships between those tables, which are hard or impossible to change.
So, is that something more people use? Are there certain problems with an approach like this?
I'm superficially familiar with some of the design patterns like Datamapper, Active record, etc to know it can get quite complex.
The problem I have had with designing my model layers is that at first you write down what pieces of data you have. Then you define their relationships. You start coding a few db classes. But then at some point in time you realize the relationships between the different kinds of data should be changed. Now you can throw all you models in the garbage. There should be better ways.
The thing that got me thinking about it was the thread about onion2k's idea about a photo gallery and how to deal with data. Do you place images (things) in groups/categories/sets/etc etc? As soon as you think traditionally ( a photo goes in an album, each album can have many tags or can be in certain categories, etc) you limit your models to be in a certain way. But as onion2k said, if you let go of that idea and only deal with tags, you might get much more flexibility.
Also take email for example. If you look at gmail, there's basically only Labels. An email arriving is labeled Inbox. An email sent is labeled Sent. You can give as many labels as you want to any email. You can create new labels. You are free to use as many labels as you want on all messages.
And that could work for more situations. Like a blog for example. Traditionally you have Posts, Comments, Categories, Tags and Authors. With certain fields. And certain (limited) relationships. But some people would like to have other sorts of posts, or use posts for other content-type (video, podcasts, etc). Maybe people would like to use other relationships between the different kinds of data. Now what if you just start with a single piece of data, and use only labels. If you label it "Post", it can be used in the traditional sense as a blog post. But you also Label it with "Henry" as the author. And as "PHP" as some sort of category. So you have a lot of flexibility, but without having a specific set of tables and relationships between those tables, which are hard or impossible to change.
So, is that something more people use? Are there certain problems with an approach like this?