EOClassDescription

From EOFWiki
Jump to: navigation, search

Contents

In one sentence

EOClassDescription is a class that facilitates objects to adhere and usefully fulfill the EOEnterpriseObject protocol.

You don't use it (much)

EOClassDescription is a glue class that feeds the EOEnterpriseObject. Application may create subclasses of EOClassDescription to provide objects with EOEnterpriseObject functionality. In regular use, only the EOEnterpriseObject methods are used and EOClassDescription remains in the background.

How it's used

To find the EOClassDescription of an object use

[self classDescription];

you might be tempted to use as a shortcut

[EOClassDescription classDescriptionForClass:[self class]];

but there are objects, like EOGenericRecord, that do not register their EOClassDescriptions, so asking the object itself is preferable.

Usually someone registers the EOClassDescription on the object's behalf. That may have happened in the +initialize method of it's class:

+ (void) initialize
{
   [EOClassDescription registerClassDescription:[self myClassDescription]
                                       forClass:self];
}

That way an EOClassDescription becomes active in the EOEnterpriseObject protocol on the behalf of the class. The class itself need not implement -classDescription then.

All methods that are part of the EOEnterpriseObject protocol are already existant as a category on NSObject. Thus for example the method toOneRelationshipKeys is present pretty much like this

@implementation NSObject( EOEnterPriseObject)

- (NSArray *) toOneRelationshipKeys
{
   return( [[self eoClassDescription] toOneRelationshipKeys]);
}

A very simple class description and it's class

@interface FooClassDescription : EOClassDescription
{
   NSArray   *_attributeKeys;
   NSArray   *_toManyRelationshipKeys;
}
@end
@implementation FooClassDescription 

- (id) init
{
   [super init];

   _attributeKeys          = [NSMutableArray new];
   _toManyRelationshipKeys = [NSMutableArray new];

   [_attributeKeys addObject:@"name"];
   [_toManyRelationshipKeys addObject:@"bars"];


   return( self);
}


- (NSArray *) attributeKeys
{
   return( _attributeKeys);
}


- (NSArray *) toManyRelationshipKeys
{
   return( _toManyRelationshipKeys);
}


- (NSString *) entityName
{
   return( @"Foo");
}

@end
@interface Foo : NSObject
{
   NSString          *_name;
   NSMutableArray    *_bars;
}

- (void) setName:(NSString *) name;
- (NSString *) name;

- (void) addToBars:(id) bar;
- (void removeFromBars:(id) bar;
- (NSArray *) bars;


@end

@implementation Foo

+ (void) initialize
{
   [EOClassDescription registerClassDescription:[FooClassDescription new]
                                       forClass:self];
}

- (void) setName:(NSString *) name
{
   [self willChange];
   [_name autorelease];
   _name = [name copy];
}


- (NSString *) name
{
   return( _name);
}


- (void) addToBars:(id) bar
{
   [self willChange];
   [_bars addObject:bar];
}


- (void) removeFromBars:(id) bar
{
   [self willChange];
   [_bars removeObject:bar];
}


- (NSArray *) bars
{
   return( _bars);
}

@end
Personal tools