Decendents..

Decendents..

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


rb157 posted on Monday, October 08, 2007

Forgive my lack of experience, I am completely self taught in c# and have recently picked up the CSLA to extend my knowledge. I think that I am coming to grips with it, but have reached a problem with descendents beyond children. I know that this has been discussed elsewhere, including in an old forum which I cannot find.

I am creating an application where I shall provide a list of Albums. Each Album has a list of songs and each song has a list of artists. Now lets assume that I have a large collection of albums, how do I use CSLA to populate all of the albums with songs and those songs with artists?

I have seen around these forums various suggsestions for solving this problem, along with a article by Rocky which may be found here:

http://www.lhotka.net/Article.aspx?id=8354fb43-f676-48dc-b6a8-692f7ba27bb0

Firstly this seems to guide against including a superset of records including album, song and artist info in each record. Whilst I fundamentally agree with this I wondered about the reasoning behind this, Rocky does state that the information will be replicated on each row, and therefore would be impractical. However in my case I only have the attributes album name, song name, artist name, therefore the amount of duplicated data would be pretty small therefore to me it would appear to be a reasonable approach, to avoid overloading my db with thousands of calls to return my list.

This however got me thinking:

If the duplicated data is low then this may be a reasonable approach. Lets say I have lots of other info in my list: the albums have album cover images and dates and a review. Then I am starting to get lots of data. Well surely then I can split into many results sets in one stored procedure but with each result having reference to its parent id. I.E.

select id, name, image from album;

select albumid, id, name from song;

select albumid, songid, id from artist;

I can then work my way through the three results sets to populate the business objects. This is however where things get complicated so I started to think about how to do this. I was thinking along the lines in the following post:

http://forums.lhotka.net/forums/permalink/3702/3721/ShowThread.aspx#3721

But I had a concern about the use of this[cageId] within the following code:

internal void GetCageAnimals(SafeDataReader dr)
{
    while (dr.Read())
       {
          Guid cageId = dr.GetGuid("CageId");
          Cage cage = this[cageId];
          if (cage != null)
             {
                cage.Animals.GetAnimal(dr);
             }
       }
}

Apologies for the long post, but does this work? My understanding is that Cages is a BusinessListBase<Cages,Cage> or ReadOnlyList<Cages,Cage> class which both inherit indirectly from Collection<T>. Within which the only index is the standard array int index. Therefore the cageId would need to be the int index in the cages object which could not be retrieved from the database. This would however work if BusinessListBase/ReadOnlyList (or an alternative) inherited from Dictionary<TKey,TValue> and TKey was always the database primary key id.

Does this make sense, i.e.  is the approach sound? If so should would the framework need to change to support this approach?

I am not sure how a dictionary works internally to provide the key/value link, however I suppose a foreach loop could be used to find the parent object to add a child to, or a dictionary could be maintained seperately in the Cages object. Would both of these lead to poor performance compared to cages being a dictionary?

Many Thanks,

Richard

 

 

 

rb157 replied on Tuesday, October 09, 2007

Just found and watched Rocky's DNRTV video on this subject. This seems to describe an approach similar to that thaqt I and Tom Cooly and I have described. However I have an issue regarding the method of finding the objects to add new children and grandchildren to. I am sure that the method within Tom's example will not work, and in Rocky's this is described in the FindById methods of the list objects by looping through the list to find the required object.

I tend to use Dictionary<TKey, TValue> objects wherever I am going to need to find a specific object in the collection for optimum performance as I am always concerned that looping through a collection will not perform sufficiently as the collection increases in size.

I may be wrong about performance of collection looping verses Dictionary access, but would appreciate comment on whether it would be worth considering implementation of Dictionary inherited object to replace or in addition to the Collection inheritance of the current ReadOnlyList objects currently within the framework.

 

Richard

Copyright (c) Marimer LLC