Polymorphism index, exclude other documents

27 views
Skip to first unread message

Tony Aarts

unread,
Jul 8, 2014, 6:09:33 AM7/8/14
to rav...@googlegroups.com
I'm trying to use polymorphism for some entities, let's say I have:

[IsRoot]
Class Animal {
   Id
   Name
}

Class Dog: Animal {
   Id
   Name
   HasTale
}

Class Cat: Animal {
   Id
   Name
   DrinksMilk
}

For conventions I use:
                Conventions = new DocumentConvention()
                {
                    FindTypeTagName = type =>
                    {
                        var parents = Types.GetInheritanceTree(type);
                        foreach (var parent in parents)
                        {
                            if (Attributes.GetAttributes<IsRootAttribute>(parent, inheritAttributes: false).HasItems())
                            {
                                return DocumentConvention.DefaultTypeTagName(parent);
                            }
                        }
                        return DocumentConvention.DefaultTypeTagName(type);
                    }
                }

A dog will be called an "animal", a cat will also be an "animal", nothing wrong with that, because there is a scenario in which I want to include both cat and dog in one index.

Now I also want to create a specific index with only "cats" to get cats which are drinking milk. What would be the easiest way to accomplish this?

In my real scenario, I got a "Planning" object, which can be eigther "AbscencePlanning", "ProjectPlanning" etc. etc. The above solution will make it possible to just get all planned items (Planning has a DateFrom, DateTo etc). So I can check everywhere if there is a duplicate planning, because of the polymorphism and the ability to query on a generic type "Project" which is available in every single assembly.

A multimap won't solve this issue, because AbsencePlanning is located in another dll (assembly) which can't access ProjectPlanning (but can access Planning) and also cannot reach the ProjectPlanning index (except when I resolve it using string, not by type). So this Polymorphism looked like a serious solution to my problem. The only thing is, I want to create a specific index for only the ProjectPlanning, but I can't find a way to only load "ProjectPlanning" objects inside my index.

Example:
Map = planning =>
                from person in planning.OfType<ProjectPlanning>()

is not working. How to get the subset of Planning??

Oren Eini (Ayende Rahien)

unread,
Jul 8, 2014, 6:58:58 AM7/8/14
to ravendb
You are probably better off doing the reverse, having a multi map index for all the common stuff.

But at any rate, you can add another attribute for filtering, or just use the Raven-Clr-Type.



Oren Eini

CEO


Mobile: + 972-52-548-6969

Office:  + 972-4-622-7811

Fax:      + 972-153-4622-7811





--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages