Core Data design patterns
Core Data design patterns
- Subject: Core Data design patterns
- From: Squ Aire <email@hidden>
- Date: Fri, 17 Jul 2009 16:26:28 +0000
- Importance: Normal
Let's say I have the usual Employee and Department entities.
Throughout my application I have to do fetching. I have simplified my code by making a helper class called CoreDataHelper, which has all kinds different helper methods, such as
+(NSArray *)employeesInDepartment:(NSManagedObject*)department
inManagedObjectContext:(NSManagedObjectContext *)moc;
And lots of others like it, as per my needs. As you can imagine, the list will grow with time as I need to do different types of fetching around my app, and as I add more entities.
The advantage of using this method is that I save myself a lot of "NSFetchRequest *fetchRequest = ..." type of code lines throughout my app. On the other hand, a possible drawback is that I might be breaking some design principles. If this is the case, can you tell me why this is bad?
In fact, I can think of another variation of my method. Namely, to not have class methods in CoreDataHelper, but rather instance methods and initialize the CoreDataHelper with the MOC. The advantage of this would be that I would not have to pass the MOC into each method call. The drawback is that I have to create a CoreDataHelper instance each time I want to use its methods.
A third variation would be to simply do either of the two variations above, but not pass in MOC anywhere and instead just use [[NSApp delegate] managedObjectContext] whenever I need it within CoreDataHelper. But I believe this third variation is plain bad as it relies on some special app delegate to be present, making a model object be depended on a controller object, breaking the MVC guidelines.
What do you guys think? How would you do this stuff?
A more general closing question: How do you make your code more organized? It's just that I've been typing these thousands of lines of code and all this time I've had this strange feeling that there is just something awful about my code, but without knowing what exactly is wrong. For instance, I've gotten to the point where lots of model/controller/view code, which should be separated somehow by MVC, are mixed together. Not completely mixed of course, I try to think of MVC and have some separation with respect to that, but there is still some mixup here and there. At least that's what I feel. The thing is just, I can't always see clearly how I would make the separation. Making me feel a bit bad. Any antidotes? Maybe reading some good design principles book will help? Or getting a degree in CS? How did you guys learn this stuff?
-.
_________________________________________________________________
Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy!
http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden