Latest Publications

Working with the Canvas element

One of the biggest problems I’ve been having with taking the KnitProject to the next level is drawing with JavaScript. This is where the Canvas element comes in.

If you’re an IE user you can leave right now, Unless you download some plugin, none of this will work. Frankly I don’t care about IE and don’t intent to support it. If and when IE descides to support the Canvas element (or you download the plugin) then you can come back and play with the big boys.

(more…)

Share

Refactoring DBObject, Part 4

The keyPath I talked about last time is just to useful to wait on. So I added it. This lets you easly get values from DBObjects within other DBObjects. So on the death date example from last time.
return (DateTime)((DBObject)valueForKey("user_info")).valueForKey("column_death_date");
is replaced with
return (DateTime)valueForKeyPath("user_info.column_death_date");
(more…)

Share

HashTable vs Dictionary<>

I’ve been using a Hashtable in my DBObject because, well, I’ve never seen any problems with it. But some people on the internet seem to hate the HashTable (something I’ve never understood.) So I decided to run my own test.

I ran two tests, one with a HashTable that uses a string as the Key vs a Dictionary<string, int> and another with the same HashTable vs Dictionary<string, object>. It seemed like the Dictionary with an object would be a better comparison because I need to store objects, not just ints.

Here is the first result:

hashtable times 1
(more…)

Share

Refactoring DBObject, Part 3

I think it’s time for a demonstration. In this example I show two ways you can use joined tables within the object. I also so why the KeyPath would be a useful addition to the object.

In User.BirthDate I’m joining two columns from the table users_info directly into the User object. There are problems with this. For example I have to change the table and index column. I’d also have to change the column names if I did and update (because birth_date and death_date don’t exist in the users table.) Over all the BirthDate method is sloppy and error prone.
The DeathDate creates a new DBObject and works off that. This is a cleaner apporach, I just have to remember to update when I update the User object. This also shows why KeyPath would be a nice addition. (DateTime)((DBObject)valueForKey("user_info")).valueForKey("column_death_date") isn’t nearly as nice as something like (DateTime)valueForKey("user_info.column_death_date")
(more…)

Share

Refactoring DBObject, Part 2

Note: I really shouldn’t call this refactoring because it’s an api change.

My SetProperty() and GetProperty() methods are really just my weak attempt to add KVC (Key Value Coding) to the DBObject. This is something built in to objective-c. So I want to rename these methods to reflect their true nature. (The word Property was confusing anyway.) It would also be nice if we could get the values via the object indexer. Although we don’t want to rely on this because the child class might wish to override it.

So SetProperty(string property, object value); will become setValueForKey(string key, object value);

and GetProperty(string property, object default); will now become valueforKey(string key);

and the overrideable valueForUndefinedKey(string key); which will be called when they key doesn’t exist.
(more…)

Share

Refactoring DBObject

DBObject has come a long way recently and there is a lot of duplicated code. So I think now is a good time to refactor.

Let’s start with the most obvious items. There are only 11 methods in the DBObject, 7 of them take string connection, 4 take string index_column, and another 4 take string table. These values should almost always be the same for the entire life of the object. It would make secence to define these somewhere and just use them instead of having to constantly pass them.

But where should we define them? I can see  3 options. Using properties on the object that the child can set. Using SetProperty and having a special key for these items. Or forcing the child to implement methods that return the correct value.

I’m not a big fan of the third option. It just seems like too much work for the developer using the object. It would be the most flexible option, but I’m not so sure that we need that kind of flexibility. I like the second option, except it provides the possibility that the child could accidentally changes these values. If we say the table name is stored as _table_name_ then there is always the possibility that the developer could try to put something else in that value. This would cause strange bugs in the object. The only thing I don’t like with the first option is that it doesn’t use the SetProperty system. You have to define these values via some other system rather than the one used for everything else.

I think for now, These  properties should use the current SetProperty system. This will keep things consistent.  So in addition to the column_ properties, there will also be _table_name_, _index_column_, and _connection_string_. We can have the methods verify these properties exist before running any method that uses them.
(more…)

Share

Checking if record/object already exists.

Last time I said it would be nice to know if the record already exists in the database. When you want a one-to-many relationship, you don’t want to accidentally add the same record more than once. If an object is made that has the exact same values as another record, we shouldn’t add it, but simply change the index to point to the correct record. Then again, you might want to allow such duplicates (like a history log.)

So today I created a new method for DBObject called doesExist. It simply returns true or false if the object already exists in the database. One thing you have to watch out for in MySQL is checking for NULL. You can’t simply do WHERE column=NULL It won’t work. You have to call it as WHERE column IS NULL
(more…)

Share

DBObject with Self Updating.

I’ve added self updating and Insert to the DBObject. I’ve also changed the naming of the columns it pulls from the database. Each column property now starts with “column_”.
(more…)

Share

Objective-C makes C# look funny.

I’ve been learning Objective-C (and Cocoa) in my spare time and I’ve recently been playing around with CoreData. It does a lot of the work for you and helps you build full featured applications quickly. And I was wondering where this kind of technology is when I do windows programming. Granted I’ve been focused on web stuff for the last few years, so maybe it exists. When I opened Visual Studio this morning I was greeted by a post of an undo/redo framework. CoreData gives you this functionality for free, you don’t have do do anything, it just works. (If you’re not using CoreData then Cocoa has a very easy to use framework for undo/redo.)

I just found this funny.

Share

39. Letters from inside the story.

Dear Heather,

I’m sorry. I’ve been cheating on you. I know it’s too late for apologies and I know there is nothing I can say to make you come back to me. You shouldn’t. You need to be with a great guy who can take care of you and be loyal. I’m not that guy. I love you more than anything in the world. I’d give up anything for you. But that doesn’t change the fact that I cheated. I just want you to know that I’m sorry and I never wanted you to find out.

I don’t know why I did it. Why does a guy give up the girl he loves for some cheap, dirty fling? When he knows he’s going to regret it. When it tears at his heart to look at your smiling face afterwords. And it does. Every time I cheated on you I woke up the next morning and just cried. Cried and prayed to god that you’d never find out, because I love you and I never wanted to lose you.

If it makes any difference to you, I left her. Actually I left her sitting alone in the middle of a field. I never want to see her again, and after what I did to her, she’ll never want to see me. She isn’t all that hot or anything. She’s kind of plain looking. Her hair is always pulled back and it stretches out her forehead. It’s just that she was there.

It was innocent at first. You never wanted to go to my friends parties. God knows I’d asked you. It’s like, if you would’ve been there, none of this would have happened in the first place. But no, you had to study or wake up early or something. This party was in this rundown house in the middle of downtown. I wouldn’t be surprised if it was condemned. We where all smoking pot and this girl sat down next to me. It was all innocent, I even told her about you! She told me about this guy she’d been dating since high-school. She said she’d never been with anyone else. He didn’t know she was here. Everything was happing all at once and we where sitting on this old couch that smelled like a dumpster, and she kissed me.

The next time I went to a party she was there again. I talked about you and she did it again. She kissed me. I tried to push her off. I was upset about last time. I told her and she pushed me down. She just pushed me the fuck down like I was some sort of rag doll and got on top of me. She started kissing my neck and pulling up my shirt. I tried to stop her, but she said if I resisted she’d scratch me so you’d know. See I didn’t have a choice, she basically raped me. I wouldn’t have done it otherwise.

Why didn’t I tell you? Who the fuck is going to believe me? My friends didn’t and they watched her do it! I just wanted it to be over and to be with you again. And we were, we were so happy together. Sure we had a couple of fights, but everyone does.

And then, I dunno. We fought about something or another as I was leaving for a party. I dunno, I was just pissed off. I didn’t care what happened. At the party I smoked like two bowls. I was just smoking and bitching and this fucking girl shows up again. She was upstairs the whole time I was down there and she must’ve heard me talking about you because she comes down and tells me to dump you. And I was like no, I love her. And she was just like then why’d you fuck me?

Fuck, I don’t know. I don’t know why I do half the stupid shit I do. And so we started fucking, like every weekend. It became, I dunno, almost normal, as crazy as that sounds. It’s like I had my perfect sweet beautiful girl all week long, and then friday night I had this nasty dirty whore. The kind of girl that goes ass to mouth while my friends cheer us on. It was exotic. And the whole time I knew I could just leave and come back to my perfect girl. The girl I wanted to spend the rest of my life with.

If I could do it all over again, I don’t know what I’d do. I hate myself for losing you. I really do love you. With all my heart I love you. I’d buy you the moon if it would make everything better. But I know it won’t. You’ll never come back to me and you shouldn’t. I wouldn’t if I were you. For whatever it’s worth, there’s a scar on my heart. Losing you was the biggest mistake I could’ve made, I’ll always love you.

©Chris Richards 2009
Share