Before I begin, I should probably mention that this sort of thing should probably have been implemented using the Core Data framework, but that is out of the question now.
We have data objects that are retrieved from a web service, and I’m trying to build a hierarchy for them, but I’m struggling.
I figured my base class should be called DataEntity, and defines an abstract property called EntityId and implements the hash method based on the entityId.
Any object retrieved from the web service can be implemented in one of two ways: By passing an NSDictionary to an entity, and letting it use that as its data source, or by passing the dictionary to an entity and letting it initialise its own data members from the dictionary. I have called these two types of entities DictionaryBackedDataEntity and ConcretedDataEntity. So my hierarchy would then be:
DataEntity -> DictionaryBackedDataEntity
-> ConcreteDataEntity
Then, consumers of this package/framework would only really care about the data members that are available to it, so I have created a protocol for each data type retrievable from the interface. So, for example, I could have a and an protocol, and these would have the data members that the DataEntity should expose.
So then, my hierarchy would look like this:
DataEntity -> DictionaryBackedDataEntity -> DictionaryBackedPerson <Person>
-> DictionaryBackedAnimal <Animal>
-> ConcreteDataEntity -> ConcretePerson <Person>
-> ConcreteAnimal <Animal>
Now, to be honest, I’m not sure whether I should be doing the above, or something like the below:
DataEntity -> PersonEntity -> DictionaryBackedPerson
-> ConcretePerson
-> AnimalEntity -> DictionaryBackedAnimal
-> ConcreteAnimal
In the above, PersonEntity and AnimalEntity would be abstract classes, and so instances would either be DictionaryBacked or Concrete instances.
Does anyone have any experience or recommendations on how I should approach this? I seem to be going around in circles and can’t settle on a decision…
Regards,
N
Why not just have a single class
DataEntitythat stores it’s property values in a dictionary? I think you probably don’t need (and probably shouldn’t require) to differentiate betweenConcreteDataEntityandDictionaryBackedDataEntity.As a start…
You can save yourself some typing by using some of the more dynamic features of Obj-C here…
Look here: https://github.com/davedelong/Demos/tree/master/DynamicStorage