SubType Collections

Posts   
 
    
morrisb
User
Posts: 13
Joined: 24-Jun-2008
# Posted on: 28-Mar-2011 16:24:19   

Hi - I'm using v3.1 (February 7th, 2011) against MS SQL Server and the Self-Servicing model.

I've got relations as follows: A is related to B (1:n). B is an abstract type and is inherited by C.

On the A Entity I have a collection of B - is there a simple way to have an EntityView of C Entities?

Is this something I can do in the designer? Or do I need to filter the B collection in code (and if so can I up-cast my B's into a collection of C's)?

Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Mar-2011 17:00:37   

You can use Type Filtering, please check Filtering on entity type

And the fetch is Polymorphic, i.e. when fetching the collection of Bs, the collection will be filled with subTypes entities. So you won't need a cast.

morrisb
User
Posts: 13
Joined: 24-Jun-2008
# Posted on: 29-Mar-2011 09:00:48   

Won't that give me a filtered collection of B's though?

I don't want to have to cast each member. I'm really after a collection of type C.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 29-Mar-2011 09:14:23   

The fetch is polymorphic. and so it won't give you a list of B's.

The following is copied from the Docs:

If Order is in an inheritance hierarchy, the fetch is polymorphic. This means that if the customer entity, in this case customer "CHOPS", has references to instances of different derived types of Order, every instance in customer.Orders is of the type it represents, which effectively means that not every instance in Orders is of the same type.

morrisb
User
Posts: 13
Joined: 24-Jun-2008
# Posted on: 30-Mar-2011 10:57:33   

In my app I have a DocumentEntity (B) that is inherited by EmailEntity (C), so based on the DelegatePredicate example in the docs I'm trying to do this in my A class:

private EntityView<EmailEntity> Emails
{
    get
    {
        PredicateExpression filter = new PredicateExpression(new DelegatePredicate(
            delegate(IEntityCore toExamine)
            {
                return typeof(EmailEntity).IsAssignableFrom(toExamine.GetType());
            }));

        return new EntityView<EmailEntity>(this.Documents, filter);
    }
}

Except this won't compile because the constructor for the EntityView requires an EmailEntity collection.

I suspect I'm either going to have to manually create an EmailEntity collection (and so miss out on changes in the base collection) or I'll have to provide a DocumentEntity collection and cast each item in my client - neither is desirable.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-Mar-2011 17:23:12   

You should filter the collection into an EntityView of the superType and then project its contents to an EntityCollection/EntityView of the subType.

Please check the following projecttion example: Projection to EntityCollection