Have prefetched aggregates?

Posts   
 
    
kdekok
User
Posts: 26
Joined: 07-Apr-2008
# Posted on: 25-Jan-2011 13:00:20   

Assume the following 2 tables:

Customers - 1:n - Orders

In a datagridview i want to display a list with seleted fields of the Customer, and a field indicating the number of Orders related to that customer.

This number of Orders is determined in the View by: calling Orders.Count on the item object (being a type of Customer, from my Model.Customers)

The moment this line is rendered, LLBGen performs a SELECT * FROM Orders where CustomerID=x on the database.

Offcourse i can add a prefetchpath to the Customer collection, but that retrieves too much data.

Is there a way to add a prefetch for an aggregate only? something like:

MyPrefetchPath.Add(new FieldExpression(OrderFields.Somefield, Count))

Or are there better ways to do this in general.

(I don't want to use a fieldresultset, because i need a strongly typed entitycollection, and i don't want to run a GetDbCount() on each item in the View, because i think that would break the 'separation of concerns' principle.)

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 25-Jan-2011 14:06:31   

The easiest way to do this is to use a TypedView, TypedList or DynamicList to fetch this information. Is there a particular reason you need a strongly typed entitiy collection if you are only displaying items in a grid ? Unless you NEED to modify every entity in collection there is a lot of overhead, it is easier to fetch a summary view of the data, and then fetch individual entities if you need to edit them.

Matt

kdekok
User
Posts: 26
Joined: 07-Apr-2008
# Posted on: 25-Jan-2011 14:17:17   

MTrinder wrote:

The easiest way to do this is to use a TypedView, TypedList or DynamicList to fetch this information. Is there a particular reason you need a strongly typed entitiy collection if you are only displaying items in a grid ? Unless you NEED to modify every entity in collection there is a lot of overhead, it is easier to fetch a summary view of the data, and then fetch individual entities if you need to edit them.

Matt

Matt, thanks for your reply.

You're right, i don't think i need the full Entity here. It was because my View is strongly typed, which would give the advantage of Intellisense to the View-designer. (ASP.NET MVC)

Is a TypedView much more lightweight compared to the Entity? The disadvantage in my opinion is that i have to create all those views in the designer, or use dynamic lists but i would be mapping all the fields constantly...

Kevin.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 25-Jan-2011 16:12:35   

TypedViews are a LOT more lightweight, they are essentially little more than a very thin wrapper around a DataTable. TypedViews are still strongly typed though (the clue is in the name simple_smile ) so you should still get intellisense etc in the designer.

Agreed there is a bit of overhead in maintaining them, but usually worth it, I reckon.

Matt

kdekok
User
Posts: 26
Joined: 07-Apr-2008
# Posted on: 25-Jan-2011 16:54:03   

Yes, i think i have to start using typedview or typedlists for this.

However the designer and/or coding overhead was even a little more than i expected, see my other post on this: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=19340

Is it true that TypedViews can only be made from SQL Views or can i create them inside the Designer from scratch, like with TypedLists?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 25-Jan-2011 17:12:11   

TypedViews can only be created from SQL views, correct. They can't be reverse engineered from the designer to the database - that only works from tables (assuming you are using V3)

Matt