Art
Art

Reputation: 771

GreenDao and entity inheritance

My task is to make disk cache on Android OS for my application (it is some sort of messenger). I'd like to store messages in database, but have met a problem of storing different types of messages (currently 5 types of messages each type have it's own fields and they all extends base class)

GreenDao documentation says:

Note: currently it’s impossible to have another entity as a super class (there are no polymorphic queries either)

I am planing to have entity which almost 1 to 1 to base class, except one column - raw binary or json data in which every child class can write anything it need.

My questions are:

  1. GreenDao is good solution in such case? Is there any solutions which allow not to worry about inheritance - and how much did they cost in terms of efficiency.
  2. How to "serialize" data to such field (what method I should override or where I should put my code which will do all necessary things
  3. How to give GreenDao correct constructor to "deserialize" Json or binary to correct class instance
  4. Should I use reflection - or just switch/case for finding correct constructor (only 5 types of constructors are possible) - is reflection how much will reflection "cost" in such case?

Upvotes: 3

Views: 2136

Answers (1)

AlexS
AlexS

Reputation: 5345

If you really need inheritance greendao is not the r I get choice, since it doesn't support it. But I think you can go without inheritance:

You can design an entity with a discriminator column (messagetype) and a binary or text column (data). Then you can use an abstract factory to create desired objects from data depending of the messagetype.

If the conversion is complex, I'd put it in a separate class, otherwise I'd put it as a method in the keep section.

Be aware that this design may slow you down, if you really have a lot of messages, since separate tables would reduce index sizes. Talking about indexes: if you want to access a message through some property of your data column later on, you are screwed since you can't put an index on it.

Upvotes: 3

Related Questions