"Inheritance" with MongoRecord

135 views
Skip to first unread message

Łukasz Kuczera

unread,
Nov 4, 2011, 8:58:03 AM11/4/11
to Lift
I'm new to mongo record, hence the question about design.
I have a model where there are different "entities" namely: files, text posts, images etc. All those can be shared in different ways, is it better to create type hierarchy for sharing ie. 

GenericShare -> ShareWithA
GenericShare -> shareWithB
GenericShare -> ShareWithC

or include the "shareType" field in single mongo record class. 

My concern is about querying that data later. For example getting all shares for file (A,B or C) getting all ShareWithA for different entieties (ShareA for file, images and text posts).

Bests,
Lukasz

Lukasz Kuczera

unread,
Nov 4, 2011, 9:14:55 AM11/4/11
to Lift
Something like this:

trait GenericShareModel [OwnerType <: MongoRecord[OwnerType]] extends
UserId[OwnerType] {
self: OwnerType =>
// A, B, C
object system extends StringField(this.asInstanceOf[OwnerType], "")
object shareId extends StringField(this.asInstanceOf[OwnerType], "")
object date extends DateField(this.asInstanceOf[OwnerType])
object url extends StringField(this.asInstanceOf[OwnerType], "")

Indrajit Raychaudhuri

unread,
Nov 4, 2011, 12:32:38 PM11/4/11
to lif...@googlegroups.com

How about something along these lines:

trait GenericShare[ShareT <: GenericShare[ShareT]] extends MongoRecord[ShareT] with ObjectIdPk[ShareT] {

self: ShareT =>

def shareId: StringField[ShareT] // deferred, to be implemented in subtype

def entity: EnumField[ShareT, Share.type] // object Share extends Enumeration { type Share = Value; ... }

object date extends DateField(self) // not deferred to subtype (type annotation not necessary since self is of type BsonRecord[_] via MongoRecord[_]

// ...
// ...
}

trait ShareA[AT <: ShareA[AT]] extends GenericShare[AT] {
self: AT =>

object shareId extends StringField(self, 20) // implemented here (again type annotation not necessary)

def meta ... // the usual MongoMetaRecord etc.
// ...
}

Is this what you are looking for?

- Indrajit

> --
> Lift, the simply functional web framework: http://liftweb.net
> Code: http://github.com/lift
> Discussion: http://groups.google.com/group/liftweb
> Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

Łukasz Kuczera

unread,
Nov 7, 2011, 2:20:51 PM11/7/11
to lif...@googlegroups.com
Thank you Indrajit, this takes me there in cleaner way but I need it to be dynamic because I don't know the exact A, B, C's during runtime hence the "system" field. All I want to say at this point is: 

1. give me all shared with A 
2. give me where I shared X (file, post, etc.)

2 is quite simple, 1 is more interesting regarding speed and code. For now I can live with querying every thingy in the system because they are fixed in numbers (files, photos, links), more concerned about the speed, but with indexes this should be quite fast shouldn't it ?
Reply all
Reply to author
Forward
0 new messages