Managing inverse relationships without CoreData

Posted by Nathaniel Martin on Stack Overflow See other posts from Stack Overflow or by Nathaniel Martin
Published on 2009-10-05T18:17:09Z Indexed on 2010/06/13 21:02 UTC
Read the original article Hit count: 262

This is a question for Objective-J/Cappuccino, but I added the cocoa tag since the frameworks are so similar.

One of the downsides of Cappuccino is that CoreData hasn't been ported over yet, so you have to make all your model objects manually.

In CoreData, your inverse relationships get managed automatically for you... if you add an object to a to-many relationship in another object, you can traverse the graph in both directions.

Without CoreData, is there any clean way to setup those inverse relationships automatically?

For a more concrete example, let's take the typical Department and Employees example. To use rails terminology, a Department object has-many Employees, and an Employee belongs-to a Department.

So our Department model has an NSMutableSet (or CPMutableSet ) "employees" that contains a set of Employees, and our Employee model has a variable "department" that points back to the Department model that owns it.

Is there an easy way to make it so that, when I add a new Employee model into the set, the inverse relationship (employee.department) automatically gets set? Or the reverse: If I set the department model of an employee, then it automatically gets added to that department's employee set?

Right know I'm making an object, "ValidatedModel" that all my models subclass, which adds a few methods that setup the inverse relationships, using KVO. But I'm afraid that I'm doing a lot of pointless work, and that there's already an easier way to do this.

Can someone put my concerns to rest?

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about object-oriented-design