MetaGuru
MetaGuru

Reputation: 43873

What would be a good pattern for designing generic json models to have identity properties added at run-time based on the configured document store?

currently the identity models are hard coded and the models inherit from them, however I'd rather be able to just write generic models without having to specify the identity properties like this (because I would need a different model definition for the same model, depending on the storage type)

PersonModel : CouchAdapterModel, IJsonModel {
     string Name { get; set; }
}

CouchAdapterModel : IJsonModel {
     string _id { get; set; }
     string _rev { get; set; }
}

thus PersonModel gains those two inherited properties, and will be in the serialized Json accordingly as needed for couch

but let's say that I don't want to use inheritance for this, rather I want the identity properties to be implied or added at runtime based on the chosen document store

then I can define the model generically, but then if I want to save it or read it from something other than CouchDB it will gain the identity properties accordingly

Can't think of a super clean way to do this though... one thought was have some kind of IJsonIdentity property required then it would be injected at runtime like

CouchDBIdentity : IJsonIdentity {
    string _id { get; set; }
    string _rev { get; set; }
}

or

MongoDBIdentity : IJsonIdentity {
    string whatever { get; set; }
}

and then at runtime when the model provider creates the model the dependency injection or something sets the identity property on the model...

the problem then becomes that I can no longer get the properties at the root level of the model, instead they are underneath the Identity property of that model, so standard JSON for the backend won't match up and people can't just use it as expected...

any idea would be greatly appreciated

Upvotes: 1

Views: 37

Answers (0)

Related Questions