Re: How to avoid excessive hard coding when programatically inserting Core Data entity ?
Re: How to avoid excessive hard coding when programatically inserting Core Data entity ?
- Subject: Re: How to avoid excessive hard coding when programatically inserting Core Data entity ?
- From: Erik Buck <email@hidden>
- Date: Sun, 16 Sep 2007 16:25:06 -0400
On Sep 16, 2007, at 12:18 PM, Greg Titus wrote:
I'd go a step farther than Shawn's suggestion of factory methods
for the VT4Objects, and have a VT4Graphic superclass of
VT4ImageGraphic (which you probably already have), which builds
both the objects and the object references. VT4Graphic would have a
user defaults-style API with methods like:
- (void)setPoint:(NSPoint)aPoint withName:(NSString *)name;
- (void)setSize:(NSSize)aSize withName:(NSString *)name;
- (void)setImageData:(NSData *)data withName:(NSString *)name;
Example implementation:
- (void)_setObject:(VT4Object *)object withName:(NSString *)name;
{
VT4ObjectReference *positionReference =
[NSEntityDescription
insertNewObjectForEntityForName:@"VT4ObjectReference"
inManagedObjectContext:[self managedObjectContext]];
[positionReference setObject:object];
[positionReference setName:name];
[positionReference setOwner:self];
}
- (void)setPoint:(NSPoint)aPoint withName:(NSString *)name;
{
VT4PointVector *newPoint = [NSEntityDescription
insertNewObjectForEntityForName:@"VT4PointVector"
i
nManagedObjectContext:[self managedObjectContext]];
[newPoint setX:[NSNumber numberWithFloat:aPoint.x]];
[newPoint setY:[NSNumber numberWithFloat:aPoint.y]];
[self _setObject:newPoint withName:name];
}
Then your utility method is reduced to:
+ (void)insertWithImage:(NSImage *)anImage atPoint:(NSPoint)aPoint
inManagedObjectContext:(NSManagedObjectContext *)aManagedObjectContext
{
if(nil != anImage)
{
NSAssert(nil != aManagedObjectContext, @"Invalid managed
object context");
// Create an entity to draw the image with the size at the
position
VT4ImageGraphic *newImageGraphic = [NSEntityDescription
insertNewObjectForEntityForName:@"VT4ImageGraphic"
inManagedObjectContext:aManagedObjectContext];
[newImageGraphic setPoint:aPoint withName:@"position"];
[newImageGraphic setSize:[anImage size] withName:@"size"];
[newImageGraphic setImageData:[anImage TIFFRepresentation]
withName:@"image"];
}
}
Hope this helps,
- Greg
I think this is a glorious idea. I wasn't thinking along the lines
of the preferences API, but that is actually apropos. I particularly
like the technique for hiding entity names from objects that don't
really need to know them. It was the specific entity names and
having to know the names of their attributes that I was calling "hard
coding." It seems to me that a VT4ImageGraphic shouldn't necessarily
have to know the deatials about a VT4PointVector's attributes.
Thanks.
_______________________________________________
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