Projection question (1:N relations)

Posts   
 
    
CowHills
User
Posts: 47
Joined: 14-Mar-2007
# Posted on: 20-Oct-2013 06:37:40   

I've playing around with projections and it is great. There one thing I'm wondering about projections.

Is it possible to project a 1:N relationship? Or is it that I don't fully understand projection and it should be always a 1:1 projection.

Let say I have the following situation:

  • Two entities: ParentEntity and a ChildEntity. There is a 1:N relationship between the two.
  • Two DTO's: ParentDTO and ChildDTO

public class ParentDTO { public string FamilyName {get;set} public ChildDTO[] Children { get; set;} }

Is it possible possible to execute one query (left join Parent and Child) and project in one cycle or do I have to execute two queries and merge the two collections afterwards?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Oct-2013 06:01:42   

CowHills wrote:

Is it possible possible to execute one query (left join Parent and Child) and project in one cycle or do I have to execute two queries and merge the two collections afterwards?

It's possible in both ways:

A) Projecting a hierarchical entity fetch (Parent plush PrefetchPath with Child) into custom classes. For this you can use EntityViews, also to create in-memory projections. more info here...

B) Projecting a DynamicList (one query) into custom classes. More info here....

Give it a try, and if you have troubles getting into this, please post more info (LLBLGen version, example code, etc) so we can help you with that.

David Elizondo | LLBLGen Support Team
CowHills
User
Posts: 47
Joined: 14-Mar-2007
# Posted on: 22-Oct-2013 10:10:43   

daelmo wrote:

It's possible in both ways:

A) Projecting a hierarchical entity fetch (Parent plush PrefetchPath with Child) into custom classes. For this you can use EntityViews, also to create in-memory projections. more info here...

B) Projecting a DynamicList (one query) into custom classes. More info here....

I've looked at both examples but both techniques show an example where both entities (Parent and Child) are flattened in one custom class. So if I have one parent entity with five children, this will result in five custom classes and not just one. Is this correct?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 22-Oct-2013 19:21:17   

Projecting a hierarchical entity fetch, the first example David mentioned, should satisfy your requirement if I understand your question correctly.

Could you please verify this or otherwise please elaborate in more details so we can better answer you?

CowHills
User
Posts: 47
Joined: 14-Mar-2007
# Posted on: 23-Feb-2014 08:24:13   

Walaa wrote:

Projecting a hierarchical entity fetch, the first example David mentioned, should satisfy your requirement if I understand your question correctly.

Could you please verify this or otherwise please elaborate in more details so we can better answer you?

The first example David mentioned will solve my problem but there is disadvantage to this approach. If I want to project a collection of entities with a 1:N relation (let's say CustomerEntities and OrderEntities) into a custom class, I must first get the EntityCollection including a Prefetch path. There is a enormous performance and memory penalty using entities. Using a plain projection is much more efficient and way faster. I'm only querying the data I want and I'm instantiating low-profile DTO-classes.

The general FetchProjection doesn't allow me to include a Pretfetch path.

The only thing I think of is to extend the FetchProjection method with the possibility of using a Prefetch path.

Could you confirm this or guide me to better solution?

Using: LLBL version 4.0 Adapter model, .NET 3.5 framework

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Feb-2014 07:12:13   

The concept of PrefetchPath is intrinsic to Entities (EntityCollection). So you cannot use that with custom projections (anonymous types).

David Elizondo | LLBLGen Support Team