« PSIG 106 | NSToolbar | Main | Beautiful Code and Beautiful Software »
PSIG 107 | Xmo'd + mogenerator: Seamless Core Data code generation
Saturday, October 13, 2007
(What is PSIG? | PSIG 107 announcement)
Show and Tell
- Practical Ruby Gems. Tom: "Eh it's a library book."
- Gorman tells us of people coming to the Genius Bar because they've renamed their home folders and thus lost sight of their accounts.
- We plug Silicon Prairie Social and I lament RubyCocoa syntax, mentioning named arguments in Ruby's future.
- Kevin tells me that Python has named arguments already, but pyObjC still uses the underscore syntax because of C conversion complications.
- Paul: Good articles in More Java Gems.
- 20 Greatest Unsolved Problems. Says Bradley: "I don't think the author is that talented as a writer. The chapter on protein folding wasn't nearly as interesting as I thought it would be." We ask if he wrote the author to complain. "I didn't write him. I could kick his butt though."
- The Chinese Language: Fact and Fantasy, Elementarz Polish primer. Bradley pointed out something really cool about the Polish number system that I forget.
- Programming Erlang, The Definitive Antler Reference.
- Wolf's going to the OpenGL Bootcamp. "I'm not going to let you keep feeding me OpenGL lies that I've been too ignorant to counteract."
- Chasing the Rising Sun. "Is it about Japan beating us in technology?" No, it's actually about the song about the house.
- Dan demos the iAlertU MacBook alarm to everyone's delight. "Impressed a guy at Panera with it."
- How to shoot yourself in the foot in any programming language

Xmo'd + mogenerator: Seamless Core Data code generation
Mogenerator refresher (from PSIG 98):
Object relational mappers integrate object-oriented software with relational databases. They allow you to work with objects instead of raw SQL statements. Classes map to tables, and rows map to instances. There are a few different ways to implement this:
- Class first (e.g. Java Beans + XDoclet): Embed relational table and column names into the source to write SQL for you. Wolf believes this is the worst philosophy -- you can reference the data source, but there's not enough information to derive the schema from embedded metadata.
- Schema first (e.g. Active Record, code gen): Link classes from the schema. Unfortunately, DDL (Data Definition Language) is a harsh and primitive landscape incapable of supporting sane metadata and you end up needing hacks. With Active Record, "you must name things the way god intended them to be named -- who is of course DHH." With code gen it means manually hacking mapping files.
- Model first (e.g. EOF, Core Data): Wolf likes this philosophy best -- you start with a declarative base, but you have infinite room for sane metadata and you're not tied to any specific data source. Because it's just data, there's no need to write a parser or interpreter. You can generate both DDL and code from a single source. In dynamic languages, custom classes are optional.
Core Data can generate wrappers for all of your attributes as NSManagedObject classes or as custom subclasses of NSManagedObject. Custom subclasses are great for holding your business logic and helping with type safety. The problem is that it's kind of a tedious task to get Xcode to generate your subclasses and it usually involves a lot of painful merging due to the fact that each file contains both generated and custom code.
You can use the lovely Generation Gap pattern to help with the merging problem: put machine-generated and custom code in separate files and make your custom code subclass the generated code.
This is where mogenerator comes in -- it's a command-line tool that automates everything and makes use of this pattern. mogenerator owns the machine's files which are subclassed off of NSManagedObject, and subclassed files off of that are owned by you and untouched by the machine. When you modify your data model and invoke mogenerator, all your generated code will be updated but your custom code won't change.
Xmo'd
Xmo'd makes your life even easier by integrating mogenerator into Xcode. Wolf achieved this with the use of undocumented, reverse-engineered plugin API. Xmo'd overrides the data model document method to automatically run mogenerator for you. When you save a data model document in Xcode, the Xmo'd override fires off an AppleScript which calls mogenerator. This keeps your generated code continuously synced up with your data model. Xmo'd also adds the menu item "Autocustomize Entity Classes." Currently it works on Xcode 2; Xcode 3 support is coming soon.
11:55 PM

Comments
Is the piggyback robot the wave of the future? I've heard a year or so back that a nurse robot has been invented - it can lift heavy patients with all the tenderness of a slightly animated cadaver.
Posted by Qwerty the cucumber at October 21, 2007 6:04 PM
The way he smiled at that moment was--it was as if all the candle flames in the room at that moment grew suddenly thinner, like slits in the dark. He put his hand--it was a healthy hand--on my shoulder.
"You know how they say that a human can be fully functional with only half a brain?"
He paused--and I remembered the proverb: "240 Is The Color Of My Name"--to breathe, and then finished.
"They were only half right."
------------
"When I get bad grades, my worldly appetite drops considerably."
"Do not wink at me. Do not pucker your lips at me. You may smile at me. But do not laugh at me."
Posted by Weien at October 25, 2007 11:20 AM