ios - Decoupling Core Data -
most apps i've written use core data follow common pattern - model classes inherit nsmanagedobject
, created insertion nsmanagedobjectcontext
.
since i'm evaluating couple of different persistence options on new side project, i'd love find way separate out core data implementation rest of code. example, might instead use concept of datastore
, defined using protocol. requires implementations provide functionality. example getcars
, addcar
, , deletecar
. app shouldn't need know whether getcars
fetching core data, realm, local file, etc.
one of immediate problems i've run core data implementation of datastore
car
added entity data model, can't create car
object except via designated initializer, expects context insert into. won't fly, since whole point separate knowledge of nsmanagedobjectcontext
etc various view controllers , other classes.
i thinking using other name core data entities (e.g. cdcar
) , adding bunch of code mapping to/from cdcar
, car
. or perhaps making protocol car
general use in app, cdcar
conform. there more common approach this?
adding parallel entity adds lot of complexity, point it's not clear decoupling net benefit.
what i'd include factory methods on datastore
class create instances on demand. instead of non-datastore
code creating car
directly, it'd call new createcar
method on datastore
. datastore
whatever needs create valid instance , returns result. mean don't need addcar
since wouldn't creating instances outside of datastore
.
Comments
Post a Comment