How to avoid excessive hard coding when programatically inserting Core Data entity ?
How to avoid excessive hard coding when programatically inserting Core Data entity ?
- Subject: How to avoid excessive hard coding when programatically inserting Core Data entity ?
- From: Erik Buck <email@hidden>
- Date: Sun, 16 Sep 2007 10:38:43 -0400
There are many times that no code is required to use Core Data.
Unfortunately, as far as I have ever determined, there are cases
where code is needed, and it is icky hard coded stuff too.
I have been using Core Data for quite a while now, and I have just
persevered through the icky parts. I am hopeful that better patterns
exist though.
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.
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.
Now, when the user drags and drops an image into the drawing
application, the VT4ImageGraphic
+insertWithImage:atPoint:inManagedObjectContext: is called and the
VT4ImageGraphic as well as all of the related entities have to be
created and inserted into a managed object context. See the code
below. Does anyone have suggestions for how to reduce the amount of
hard coding going on ?
@implementation VT4ImageGraphic (VT4Utility)
+ (void)insertWithImage:(NSImage *)anImage atPoint:(NSPoint)aPoint
inManagedObjectContext:(NSManagedObjectContext *)aManagedObjectContext
{
if(nil != anImage)
{
NSAssert(nil != aManagedObjectContext, @"Invalid managed
object context");
// Create an entity to store the position of the image
VT4PointVector *newPoint = [NSEntityDescription
insertNewObjectForEntityForName:@"VT4PointVector"
inManagedObjectContext:aManagedObjectContext];
[newPoint setX:[NSNumber numberWithFloat:aPoint.x]];
[newPoint setY:[NSNumber numberWithFloat:aPoint.y]];
// Create an entity to store the desired size of the image
VT4Vector *newSize = [NSEntityDescription
insertNewObjectForEntityForName:@"VT4Vector"
inManagedObjectContext:aManagedObjectContext];
[newSize setX:[NSNumber numberWithFloat:[anImage size].width]];
[newSize setY:[NSNumber numberWithFloat:[anImage size].height]];
// Create an entity to store the image data
VT4Image *newImage = [NSEntityDescription
insertNewObjectForEntityForName:@"VT4Image"
inManagedObjectContext:aManagedObjectContext];
[newImage setImageData:[anImage TIFFRepresentation]];
// Create an entity to draw the image with the size at the
position
VT4ImageGraphic *newImageGraphic = [NSEntityDescription
insertNewObjectForEntityForName:@"VT4ImageGraphic"
inManagedObjectContext:aManagedObjectContext];
// Create an entities to reference the image data, position,
and size entities
// and establish the relationships between the entities
VT4ObjectReference *imageReference = [NSEntityDescription
insertNewObjectForEntityForName:@"VT4ObjectReference"
inManagedObjectContext:aManagedObjectContext];
[imageReference setObject:newImage];
[imageReference setName:@"image"];
[imageReference setOwner:newImageGraphic];
VT4ObjectReference *positionReference = [NSEntityDescription
insertNewObjectForEntityForName:@"VT4ObjectReference"
inManagedObjectContext:aManagedObjectContext];
[positionReference setObject:newPoint];
[positionReference setName:@"position"];
[positionReference setOwner:newImageGraphic];
VT4ObjectReference *sizeReference = [NSEntityDescription
insertNewObjectForEntityForName:@"VT4ObjectReference"
inManagedObjectContext:aManagedObjectContext];
[sizeReference setObject:newSize];
[sizeReference setName:@"size"];
[sizeReference setOwner:newImageGraphic];
}
}
@end
_______________________________________________
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