I'm trying to work out how to inject resources into NSManagedObject subclass instances, but can't find a reasonable way.
Simply put, I have an object which represents a service that behaviour methods of an Entity require. This service needs to be available at runtime.
In a Plain-Old-Objective-C-Object, I'd simply pass this object as a constructor argument, or set it via a property after construction. Similarly to how many objects require and use a delegate.
However, awakeFromInsert and awakeFromFetch obviously take no parameters, nor can I find anywhere to hook into the NSManagedObjectContext to configure NSManagedObjects post initialisation.
Does anyone have a solution for this?
As a entirely contrived example:
@interface ProductEntity : NSManagedObject
@property (nonatomic, retain) NSNumber *unitPrice;
@property (nonatomic, retain) MyTaxCalculatorService *taxCalculatorService;
- (void)grossPriceForUnits:(NSUInteger)units;
@end
@implementation ProductEntity
@dynamic unitPrice;
@synthesize taxCalculatorService
- (void)grossPriceForUnits:(NSUInteger)units
{
return [self.taxCalculatorService grossAmountForUnitPrice:self.unitPrice quantity:units];
}
@end
Ignoring whether this is the best way to calculate gross prices (it's a contrived example), how would I get the taxCalculatorService instance into the ProductEntity? I can't override init, and I can't find anywhere I could consistently call [entity setTaxCalculatorService:service].
Thoughts?