views:

72

answers:

1

I have a DTO with 40+ properties. But in order to populate all properties I need to execute 4 separate queries. My first query is in charged of getting basic information. For each row returned I run 3 more queries based on the id's given from the main query (N+1 problem). I can set use eager loading but then I'm loading thousands of objects that I don't need.

Should I split my DTO and create a separate DTO for each query I run then link then tie them all together into a central DTO by id?

I was envisioning a final DTO like this.

public class FooDto
{
    public string Foo { get; set; }
    public string Bar { get; set; }
    public FirstDto FirstQueryResults { get; set; }
    public SecondDto SecondQueryResults { get; set; }
    public ThirdDto ThirdQueryResults { get; set; }
}

Is there a better way of solving this? I'm using Oracle and NHibernate doesn't support multi criterias. Note that I am joining most of my data. The problem comes when I need to query data with a complete new set of criteria.

+1  A: 

How about creating a VIEW that joins the data to give the 40 properties all in one go, and basing your DTO on that - whatever a DTO may be ;-)

Tony Andrews
Unfortunately, I can't create a view because the database is read-only. :(
Mike
That's OK, you (or perhaps rather, the DBA) can create a view in another schema that selects (reads) data from those tables.
Tony Andrews
No dice. We are not allowed to create anything on that database regardless of schema.
Mike
This is why ORMs like nhibernate get such a bad name: trying to join data from several tables without actually joining in SQL is crazy. Can't you just write the SQL you need and run that through nhibernate?
Tony Andrews
I don't follow "trying to join data from several tables without actually joining in SQL is crazy".
Mike
Well, you said that you need to execute 4 separate queries to obtain all the data for one DTO, which I assume is like an "entity" or "object". Presumably that's because the data is held in various tables. You could get it all in 1 query if you could perform a SQL join: "select a.xxx, b.xxx, ... from a join b...". That is a far better idea than running lots of tiny queries and joining the data together in the application - for a start it is 1 round trip to the database rather than 4. That's what I meant.
Tony Andrews
For the most part everything is joined. The problem comes when I need to query tables that use totally different criterias.
Mike