Attachments November 18, 2005
This post can also be subtitled "The Philosophy of Computer Science, Part 1.1". It's a new idea I'm playing with. It's not a new idea, really, it's derivative of a very cool concept in Computer Science called Aspect Oriented Programming. It's a pretty abstract concept. Basically, the typical example used is logging transactions on an object (I know, that description is very abstract also). So, say I have a bank account and I want to log my transactions, I can just flip a switch, sorta, and now my bank account is emailing me whenever I deposit or withdrawal money. But, what if now I want to deposit half of the money in a savings account when it's deposited in my checking account WITHOUT modifying ANY banking code?! Impossible, you say? They key here is that I don't want to go into the "deposit" function on my bank account object and enter stuff to deposit half of it into my savings. If you designed the system using an AOP container, this would be simple. What if I wanted to log AND deposit half into savings on a deposit into my checking account? I would just write a class that "intercepts" a call to deposit money into my checking account and takes half and puts it into my savings account, then continues the call to deposit the money into my checking account with half of the original value. Of course, if I was dishonest, I'd just deposit half into savings and then the original amount into checking!! But, that's not honest :)
So, AOP is very flexible. What I'm developing is a twist, though. It's for objects on my website, and it's mostly to do with data, rather than "processes" (like, before I deposit my check, take half, put it in savings, then put the other half into checking). So, to be more in line with data, and to be sorta AOP, what would it have to do?
Pretend I have a News object (I do, you're reading it), and that news object, right now, has a title, a date, text, the user who wrote it, and the categories it belongs in. What if I wanted to add a file to correspond with the news that I could upload and it would automatically show itself with a little paperclip (signifying an "file" attachment), and the filename next to it, with a link to download it. I don't want to program the news so that every news item could potentially have a file attached to it. You'll see later that by attachment, I don't only mean "File" attachment, like you send with an email. What if I want to allow other objects to have that same type of attachment, i.e. a file uploaded and shown under that item? I'm not about to change my whole, very well designed, table and object structure. I'm way too lazy for that.
Another attachment possibility for News would be a "History" attachment, so that whenever I update a news post, it puts an entry into a history table, and I can view that news' history. Another one would be a "View Count" attachment, so that whenever that news item is viewed, I increment a field for that news item, and that value shows under the news in a label that reads "Read 4 times". What if I wanted to put security on a certain news item? I could make a "Security" attachment that ties into my current security system and checks to see if the person viewing the website should be allowed to see that news.
The main thing here is that I don't know all of the possibilities for the future. That's where Object Oriented programming and Aspect Oriented Programming form a perfect marriage. I can program a new attachment and add it into the system, then any object can use that attachment. One downside of this design right now is that for every news item I enter, I will have to select, each time, what attachments I want. For some things, it just makes sense. Like, I could turn "Comments" into an attachment, but for most news items I will want to allow comments, so I would be adding that to every news item. The upside, in contrast, is that some objects were not designed to be commentable. I could make Content commentable this way (click "Your Site Rules!" for a Content example).
One thing I could do is have a global attachment for objects. News could have a global attachment of Comments, then a local attachment could be a file attachment for a certain news item.
One pretty big downside of this system is this... it's website rewrite time!! Yup, the whole website will have to be redesigned. The data, however, can pretty much stay how it is. That's good, because I decided on this design because I didn't want to change the data structure! I'd hate to lose anything I wrote, I love reading my own stuff ;-)
I can hardly stay awake right now though. Til tomorrow.