Returning a domain object that has a treeview like structure..

Mar 3, 2008 at 12:26 PM
I am trying to return a list of categories, each category can have one or more items associated.
e.g.
--root category
----category 1
------data item 1 for category 1
------data item 2 for category 1
----category 2
------data item 1 for category 2

problem is repository class iterates reader for me and for this scenario, I need to iterate the reader in order to return an object like the following...

class CategoryTree : Category
{
public IList<CategoryContent>
}

Is the problem with the domain object - i.e. should i return a list of categories and in another call return the list of categorycontents and marry the two up in the presenter?
...or is there someway of getting the repositoryfactory class to call a class that i write to replace the Repository class?

Regards,
Rik
Developer
Mar 18, 2008 at 8:53 PM
Support for this construct is built-in to the Oracle code generation, though some manual work is involved because collection properties on Business Entities are not processed recursively to discover the properties of the children.

The code should look something like that (note the use of an anonymous delegate at the bottom of the code sample):

public List<Job> Construct(IDataReader reader)
{
// Create a list of the parents first
List<Job> JobList = new List<Job>();

while (reader.Read())
{
Job Job = new Job();

int IdIndex = reader.GetOrdinal("JOBID");
if (!reader.IsDBNull(IdIndex)) Job.Id = reader.GetString(IdIndex);

int TitleIndex = reader.GetOrdinal("JOBTITLE");
if (!reader.IsDBNull(TitleIndex)) Job.Title = reader.GetString(TitleIndex);

// code removed for brevity

JobList.Add(Job);
} // while

// Process the second cursor
reader.NextResult();

while (reader.Read())
{
// create child object and populate with data from cursor
Employee childObject = new Employee();

int lastNameIndex = reader.GetOrdinal("LAST_NAME");
if (!reader.IsDBNull(lastNameIndex)) childObject.LastName = reader.GetString(lastNameIndex);

// code removed for brevity


// Add the child object to the parent object's List<ChildObject> property
// using the MatchingParentObject delegate method (e.g. matching the "OrderDetails"
// entity to the "Order" entity).
Int32 keyIndex = reader.GetOrdinal("ID");
Int32 key = reader.GetInt32(keyIndex);
JobList.Find(delegate(Job itemToMatch) { return itemToMatch.Id == key.ToString(); }).EmployeesInJob.Add(childObject);
}

return JobList;
}


r_beacroft wrote:
I am trying to return a list of categories, each category can have one or more items associated.
e.g.
--root category
----category 1
------data item 1 for category 1
------data item 2 for category 1
----category 2
------data item 1 for category 2

problem is repository class iterates reader for me and for this scenario, I need to iterate the reader in order to return an object like the following...

class CategoryTree : Category
{
public IList<CategoryContent>
}

Is the problem with the domain object - i.e. should i return a list of categories and in another call return the list of categorycontents and marry the two up in the presenter?
...or is there someway of getting the repositoryfactory class to call a class that i write to replace the Repository class?

Regards,
Rik