Re: Core Data Abstractions
Re: Core Data Abstractions
- Subject: Re: Core Data Abstractions
- From: mmalcolm crawford <email@hidden>
- Date: Mon, 30 May 2005 11:02:08 -0700
On May 30, 2005, at 9:00 AM, Vincent Coetzee wrote:
This is a problem that occurs often when using O/R mapping
frameworks or even object databases. In the ODBMS world we have a
pattern that we use to handle this, we call it the "manager
pattern". We create a single object called for example a
ProjectFactory or a ProjectManager, we then hide all of the code in
this object and we use a series of methods to gain access to the
objects we require such as
array = [ProjectFactory allProjects]
we may even have more specific methods for retrieving only the
names (often required for GUIs)
arrayOfStrings = [ProjectFactory allProjectNames];
The analogue here would be to create another entity to which all the
Projects are related. You then "fetch" the Projects simply by
following the relationship.
[An aside here:
On 30 May 2005, at 5:51 PM, Evan DiBiase wrote:
An issue that's come up, however, is that I seem to be sprinkling a
lot of fetch requests and new entity insertions around my code.
Why are you writing the fetch requests? Are you doing this instead
of simply traversing a relationship? See:
<http://developer.apple.com/documentation/Cocoa/Conceptual/
CoreData/Articles/cdFAQ.html#//apple_ref/doc/uid/TP40001802-242985>
How are you creating the new instances of your entities? The
NSEntityDescription method makes it a lot easier:
NSManagedObject *newEmployee = [NSEntityDescription
insertNewObjectForEntityForName:@"Project"
inManagedObjectContext:context];
See:
<http://developer.apple.com/documentation/Cocoa/Conceptual/
CoreData/Articles/cdCreateMOs.html>
Finally, of course, using Cocoa Bindings will also cut out a lot of
code, if that's an option.
]
Alternatively, you can remove a lot of repetitive code by creating
and using fetch request templates:
NSError *error = nil;
NSFetchRequest *fr = [[self managedObjectModel]
fetchRequestTemplateForName:@"AllProjects"];
NSArray *projects = [[self managedObjectContext]
executeFetchRequest:fr error:&error];
mmalc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden