C
cwineman
Hello,
I'm hoping to do something using Generics, but I'm not sure it's possible.
Let's say I want to have a bunch of business objects and a data access class
cooresponding to each business object. For each business object, I would
have something like below:
public class Apple
{
//apple stuff
}
public class AppleDAL
{
public void UpdateApple( int appleID, Apple apple );
public Apple GetApple( int appleID );
public AppleList ListAllApples();
}
Currently, the AppleList collection class is a subclass of CollectionBase.
Everytime I add a new business object, I need to write a new DAL class and a
new CollectionBase subclass to act as the collection for that business
object:
/// <summary>
/// Type-safe collection for Apple objects
/// </summary>
[Serializable]
public class AppleList : System.Collections.CollectionBase
{
public AppleList {}
public Apple this[ int index ]
{
get{ return( Apple List[index] ); }
set{ List[index] = value; }
}
public int Add( Apple item ) { return( List.Add( item ) ); }
public int IndexOf( Apple item ) { return( List.IndexOf( item ) ); }
public void Insert( int index, Apple item ) { List.Insert( index,
item ); }
public void Remove( Apple item ) { List.Remove( item ); }
}
I am wondering if there is a way to use Generics to avoid writing the
CollectionBase subclass for every new business object. I'm not sure how I
would create a concrete type-safe collection for something new. So for an
new business object, orange:
public class Orange
{
//orange stuff
}
public class OrangeDAL
{
public void UpdateOrange( int orangeID, Orange orange );
public Orange GetOrange( int orangeID );
public OrangeList ListAllOranges();
}
Can I create an OrangeList class using Generics? Something like?
//this doesn't work
public class OrangeList <Orange>{}
-Corey
I'm hoping to do something using Generics, but I'm not sure it's possible.
Let's say I want to have a bunch of business objects and a data access class
cooresponding to each business object. For each business object, I would
have something like below:
public class Apple
{
//apple stuff
}
public class AppleDAL
{
public void UpdateApple( int appleID, Apple apple );
public Apple GetApple( int appleID );
public AppleList ListAllApples();
}
Currently, the AppleList collection class is a subclass of CollectionBase.
Everytime I add a new business object, I need to write a new DAL class and a
new CollectionBase subclass to act as the collection for that business
object:
/// <summary>
/// Type-safe collection for Apple objects
/// </summary>
[Serializable]
public class AppleList : System.Collections.CollectionBase
{
public AppleList {}
public Apple this[ int index ]
{
get{ return( Apple List[index] ); }
set{ List[index] = value; }
}
public int Add( Apple item ) { return( List.Add( item ) ); }
public int IndexOf( Apple item ) { return( List.IndexOf( item ) ); }
public void Insert( int index, Apple item ) { List.Insert( index,
item ); }
public void Remove( Apple item ) { List.Remove( item ); }
}
I am wondering if there is a way to use Generics to avoid writing the
CollectionBase subclass for every new business object. I'm not sure how I
would create a concrete type-safe collection for something new. So for an
new business object, orange:
public class Orange
{
//orange stuff
}
public class OrangeDAL
{
public void UpdateOrange( int orangeID, Orange orange );
public Orange GetOrange( int orangeID );
public OrangeList ListAllOranges();
}
Can I create an OrangeList class using Generics? Something like?
//this doesn't work
public class OrangeList <Orange>{}
-Corey