CSLA 1.5: Two Child Collections per Business Object

CSLA 1.5: Two Child Collections per Business Object

Old forum URL: forums.lhotka.net/forums/t/924.aspx


cjherasmus posted on Thursday, August 17, 2006

Hi everyone,

Is it possible to have two or more child collections per business object, e.g.

BO is Person

First child object collection is Vehicles

Second child object collection is Houses

In the Person object I would then declare:

private PersonVehicles _personVehicles = PersonVehicles.NewPersonVehicles();

private PersonHouses _personHouses = PersonHouses.NewPersonHouses();

And then have two properties declared:

public PersonVehicles Vehicles

{get {return _vehicles;}}

public PersonHouses Houses

{get {return _houses;}}

In the fetch:

_vehicles = PersonVehicles.GetPersonVehicles(dr);

_houses = PersonHouses.GetPersonHouses(dr);

etc....

In the end I want to display the Person properties on the form, with a list of all houses and a list of all vehicles, exactly the same way Rocky did with the ProjectTracker code in the 1.5 version of the book code.

Regards,

Henrik replied on Thursday, August 17, 2006

Hi cjherasmus

Yes you can have multiple child collections per business object. You own example is exactly how you want to do this.

If you are using a GetPerson sproc to retrieve your data, this will also have to retrieve the vehicles and houses related to the person, then your DataPortal_Fetch will look like this:

...
//Read person data from data reader
...

dr.NextResult()
_vehicles = PersonVehicles.GetPersonVehicles(dr);

dr.NextResult()
_hourse = PersonHouses.GetPersonHouses(dr);

/Henrik

RangerGuy replied on Thursday, August 17, 2006

How would you handle a collection of people?

If you had People.Person.PersonVehicles.GetPersonVehicles(dr)?

When you select from the person table you select all the related vechiles and houses. So each dataset would have data relating to multiple people.

Datareaders are forward only so once you read the last record for a PersonsVehicle the dataReader will automatically read the next row so you will be on a different persons list of vehicles I think.

Maybe for this instances it's better to use a dataset instead of a datareader?

 

cjherasmus replied on Thursday, August 17, 2006

Henrik,

I initially thought my problem must be solved somewhere above or beneath the "dr.NextResult" line. You've made it clear to me. Never thought of having two dr.NextResult lines.

Thanks

jwooley replied on Thursday, August 17, 2006

There's no problem with using multiple child collections. I have one BO that has about 20 child collections (yeah, I should probably refactor that one some day...) Make sure to add each one as part of your .IsDirty and .IsValid overloads to have the parent object reflect the full status if necessary.

Additionally, there's no problem with using datasets with data relations to populate the results. Another option would be to lazy load the children collections. One method I use is as follows (in pseudocode)

Select records for parent object collection based on a filter and fill it
Select records for child objects joined to the parent table based on the parent table's filter as above, including the parent's id column
   while reading results from the child query
        Find the parent object in the collection
            Add a new child to the selected collection by passing the datareader row (note, we do not recreate the collection each time and return that as would be typical of a dataportal call. The entire fetch is occuring on the server side and the tree will be returned in one call)

Note, I only use this scheme when loading the full tree is necessary. Hopefully this makes enough sense to not include some full code.

Jim Wooley
http://devauthority.com/blogs/jwooley

Copyright (c) Marimer LLC