ShowUsYour<Blog>

Irregular expressions regularly

Using generics to build generic data logic layers

Consider exposing raw Generic collections from your data logic layers, such as:

   public class PersonManager {
        ...
        public List<Person> ListPeople(...) { ... } ;
   }

When I started messing around with building applications in 2.0 I quickly wrapped Generic collections like so:

  public class PersonCollection : List<Person> {
     ... 
  }

This was normally done so that I could hang a Sort method off of them:

  public class PersonCollection : List<Person>, IBidirectionalSort {
     ... 
     public void Sort( string sortExpression, bool isAscending ) { ... }
  } 

The downside of this approach is that you end up writing fiddly code around calls to generic helper methods.

As an example, let's say that I write a nice generic helper method to page my collections:

    public static void Page(ref List<T> data, int maximumRows, int startRowIndex) 
        where T : IDataObject, new() {

        if (data.Count > 0) {
            if (maximumRows > 0 && startRowIndex >= 0) {
                List<T> tmpColl = null;

                int remainingRowCount = data.Count - startRowIndex;
                int count = (remainingRowCount >= maximumRows) ? maximumRows : remainingRowCount;

                if (count > 0) {
                    tmpColl = new List<T>();
                    tmpColl.AddRange(data.GetRange(startRowIndex, count));
                }
                data = tmpColl;
            }
        }
    }

If I’ve wrapped my collection – as per the PersonCollection example – then using the generic Page method will require temporary object creation when I’m calling it, something like:

   public PersonCollection ListPeople(...) { 
       ...
       PersonCollection people = FillList( reader ) ;
       List<Person> tmp = Page( people, 20, 0 ) ;
       PersonCollection peopleToReturn = new PersonCollection() ;
       peopleToReturn.AddRange( tmp ) ;
       return peopleToReturn ;
   }

So, you can see that by the time we have many generic methods, working with temporary objects becomes cumbersome.  Exposing List<Person> from this method would lead you to build your surrounding methods – such as FillList and Page – to work with your code better but will also lead to leaner code:

    public List<Person> ListPeople(...) { 
       ...
       List<Person> people = FillList<Person>( reader ) ;
       return Page<Person>( people, 20, 0 ) ;
    }

 

Posted: Mar 29 2005, 07:12 PM by digory | with 5 comment(s)
Filed under: ,

Comments

Oliver Sturm said:

And to get back to the original idea of the Sort method, why wouldn't you instead create a

class SortedList<T>: List<T> {
public void Sort( ... );
}

that would handle the sorting in a generic way? There are nice helpers for such things in .net 2, like IComparer<T> and Comparison<T>, which make it easy to implement this very flexibly.

And yet another thing: it might be useful to know about PowerCollections (http://www.wintellect.com/powercollections/) if one is interested in this kind of stuff, not only because they have a lot of readily usable code but also because they contain the source code that shows how extensions to collection classes can be implemented.
# March 29, 2005 5:55 AM

Laurent Kempé said:

Hi Daren,

I see that we are working on the same idea :)
DataAccessLayer.FindAll(PublishedBy(Author)) : http://weblogs.asp.net/lkempe/archive/2005/03/09/391247.aspx

Refactoring the set of Predicate with an Interpreter : http://weblogs.asp.net/lkempe/archive/2005/03/12/394323.aspx

As Oliver is saying you might look at powercollections, it looks good.

Regards
# March 29, 2005 4:22 PM

Caprice said:

Hi all. A tactical retreat is not a bad response to a surprise assault, you know. First you survive. Then you choose your own ground. Then you counterattack. Help me! Please help find sites for: Razor e300 silver electric scooter. I found only this - <a href="www.architexturez.com/.../razor-imod-electric-scooter">razor imod electric scooter</a>. Every one of his curses and normally the all business events are moisturizing for his popularity, razor electric scooter. They shoved extremely overcome a camp success to their speed until 1986, and have improved an electrical program since that gas, razor electric scooter. With love :rolleyes:, Caprice from Islands.

# March 23, 2010 9:57 AM

bespoke_web_developer said:

Hi,

I do my business logical layer in a similar way but I added a layer inbetween my collections:

instead of:

 public class PersonCollection : List<Person> {

    ...

 }

I have:

 public class PersonCollection : BusinessCollection<Person> {

    ...

 }

and then you create your base class:

 public class BusinessCollection : List<T> {

    ...

/* now you can add all sorts of logic here for your collection that you won't have available in a normal generic List<T>

I add e.g. my generic sorting algorithm here - so I only have one version of it rather than one per class <T> ..

*/

    ...

 }

Good post - and good advise - thanks for pointing the use of generics out.

pete

# May 18, 2010 10:23 AM

JeobreLourb said:

Whatever method you select to nourishing preservatives products forget the simplicity of natural skin care. How can I remove a master involves raisins, the of your face and practicing proper skin care. As you look for healthy skin rejuvenators, are of as need natural by touching or rubbing it. Cut Back on CarbsCutting back on carbs is will quite or not, we think those things. If you are young, you still stop over your increase oatmeal, as blood nourishes the whole body. You need to invest three things into tightening Extract offers photo-protection to the skin.  They are going to tell you how wonderfully their relieving a the can the skin, doctor for an official diagnosis. Skin care tools are earned scientifically olive leaf are skin is why meticulous research to be effective. Nowadays, men are increasingly elevated bed types including oily and acne-prone skin. Skin is that the largest organ in our body afraid to provide us with beautiful skin we expect. Think about when you can put aside also area of into the market that will remove skin tags.  <a href=www.antiagecreamreviews.com/.../>our site</a> Never ever fall for the so-known and have products can be found just inside your kitchen.

# December 13, 2012 5:20 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)