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: Greg Titus <email@hidden>
- Date: Sun, 16 Sep 2007 09:18:37 -0700
On Sep 16, 2007, at 7:38 AM, Erik Buck wrote:
Consider for example a drawing program that uses Core Data to store
graphical elements to draw. I have a bunch of VT4Object (or
subclass) entities that store points, images, font identifiers,
strings, etc. I then have VT4ObjectReference entities.
VT4ObjectReference has a to-one relationship with VT4Object which
in turn has a to-many reciprocal relationship. The reason for
VT4ObjectReference is that one point object might be used in
several graphical objects. Several graphical objects might draw
the same image. Only one instance of each particular point or the
image exists in the data store, but multiple graphics may reference
the same point or image.
One thing I notice, is that your code doesn't look like it attempts
to do any of the point/image uniquing you mention here.
So, one particular kind of graphic is the VT4ImageGraphic whose job
is to draw an image with a size at a position. The image, size,
and position are all VT4ObjectReferences which relationships with
the actual point, size, and image entities.
Does anyone have suggestions for how to reduce the amount of hard
coding going on ?
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"
inManagedObjectContext:[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
_______________________________________________
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