473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

generics collection question

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

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Aug 4 '06 #1
14 1673
Heck yes, Corey! It's quite simple:

using System.Collections.ObjectModel;

public class AppleList : Collection<Apple{}

That's all you need. It already has the methods that you had to wire up
before, like Add, Remove, IndexOf, etc.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.

"cwineman" <me@home.comwrote in message
news:11**************@sp6iad.superfeed.net...
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
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption
=----

Aug 4 '06 #2
Sure, define a base DataCollection and DataRecord object. Then create
your base DAL entity class and collection and then you can create
custom classes on top of those (I say this because I'm guessing your
DAL is probably auto-generated).

Something like this for the DAL:

public abstract class OrangeCollectionBase<T: DataCollection<Twhere
T : OrangeBase, new()
{
}

Then your derived collection class would look like this:

public class Oranges : OrangeCollectionBase<Orange>
{
}

cwineman wrote:
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

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Aug 4 '06 #3
Excellent.

I was fairly certain that it could be easily done, but I couldn't figure out
the syntax and I wasn't able to find an example showing what I wanted.

Thanks, guys.

"Kevin Spencer" <uc*@ftc.govwrote in message
news:eQ******************@TK2MSFTNGP04.phx.gbl...
Heck yes, Corey! It's quite simple:

using System.Collections.ObjectModel;

public class AppleList : Collection<Apple{}

That's all you need. It already has the methods that you had to wire up
before, like Add, Remove, IndexOf, etc.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Aug 4 '06 #4
cwineman wrote:
I was fairly certain that it could be easily done, but I couldn't figure out
the syntax and I wasn't able to find an example showing what I wanted.
If you don't want to write your own class, you can just use the built
in List<class:

public List<OrangeListAllOranges();

Aug 4 '06 #5
<wf****@gmail.coma écrit dans le message de news:
11**********************@p79g2000cwp.googlegroups. com...

| Sure, define a base DataCollection and DataRecord object. Then create
| your base DAL entity class and collection and then you can create
| custom classes on top of those (I say this because I'm guessing your
| DAL is probably auto-generated).

This really isn't necessary, you can use List<Tor Collection<Tor any
other generic class without any further derivation. A DAL should be quite
capable of producing or iterating through such lists using reflection.

public static class DAL
{
public static List<TRetrieveList<T>()
{
...
}

...
}

void test()
{
List<CustomercustList = DAL.RetrieveList<Customer>();

...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Oct 10 '06 #6
"Joanna Carter [TeamB]" <jo****@not.for.spamwrote in message
This really isn't necessary, you can use List<Tor Collection<Tor any
other generic class without any further derivation. A DAL should be quite
capable of producing or iterating through such lists using reflection.
public static class DAL
{
public static List<TRetrieveList<T>()
{
...
}
Just to be nitpicky, that code wouldn't pass FxCop validation (or Team
System Code Analysis).

For reason's I'm not quite clear on, you're not supposed to return a
List<T>. They recommend using the Generic classes found in the
System.Collections.ObjectModel namespace instead.

You can find more on this here:
http://msdn2.microsoft.com/en-us/library/ms182142.aspx
--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Oct 10 '06 #7
"Chris Mullins" <cm******@yahoo.coma écrit dans le message de news:
OB*************@TK2MSFTNGP05.phx.gbl...

| Just to be nitpicky, that code wouldn't pass FxCop validation (or Team
| System Code Analysis).
|
| For reason's I'm not quite clear on, you're not supposed to return a
| List<T>. They recommend using the Generic classes found in the
| System.Collections.ObjectModel namespace instead.

Hmm, Do you know if IList<Tis equally frowned upon ?

| You can find more on this here:
| http://msdn2.microsoft.com/en-us/library/ms182142.aspx

Interesting but, as you say, the reasons really aren't all that clear. What
If I want to return a type that cannot be inherited, is that really so wrong
?

I personally use my own ReadOnlyList<Tclass because ReadOnlyCollection<T>
raises exceptions on all the IList<Timplementing methods. To my mind, that
is a poor design choice; what is the point of supplying an interface that
provides most of its functionality by raising exceptions ? You could hardly
call that "implementation" :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Oct 10 '06 #8
"Joanna Carter [TeamB]" wrote
"Chris Mullins" <cm******@yahoo.coma écrit dans le message de news:
OB*************@TK2MSFTNGP05.phx.gbl...

| Just to be nitpicky, that code wouldn't pass FxCop validation (or Team
| System Code Analysis).
|
| For reason's I'm not quite clear on, you're not supposed to return a
| List<T>. They recommend using the Generic classes found in the
| System.Collections.ObjectModel namespace instead.

Hmm, Do you know if IList<Tis equally frowned upon ?
Code Analysis (via Team System) is perfectly happy with IList<T>. Seems to
be if one fails, they both should fail.
>
| You can find more on this here:
| http://msdn2.microsoft.com/en-us/library/ms182142.aspx

Interesting but, as you say, the reasons really aren't all that clear.
What
If I want to return a type that cannot be inherited, is that really so
wrong
?
I agree. I recognized the issue in your code because I do it so often
myself. We've been trying to be diligant about using FxCop more, and this is
one of the things that keeps biting me.
I personally use my own ReadOnlyList<Tclass because
ReadOnlyCollection<T>
raises exceptions on all the IList<Timplementing methods. To my mind,
that
is a poor design choice; what is the point of supplying an interface that
provides most of its functionality by raising exceptions ? You could
hardly
call that "implementation" :-)
I agree here as well.

I would much rather have a true RealyOnly Interface. I want compile-time
errors, not run-time errors.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Oct 10 '06 #9
Chris Mullins wrote:
"Joanna Carter [TeamB]" wrote
>"Chris Mullins" <cm******@yahoo.coma écrit dans le message de news:
OB*************@TK2MSFTNGP05.phx.gbl...
| For reason's I'm not quite clear on, you're not supposed to return a
| List<T>. They recommend using the Generic classes found in the
| System.Collections.ObjectModel namespace instead.

Hmm, Do you know if IList<Tis equally frowned upon ?

Code Analysis (via Team System) is perfectly happy with IList<T>. Seems to
be if one fails, they both should fail.
Maybe it has nothing to do with generics and it just wants
to enforce the good habit of returning interfaces instead
of concrete classes.

Arne
Oct 10 '06 #10
"Arne Vajhøj" <ar**@vajhoej.dkwrote
Chris Mullins wrote:
>"Joanna Carter [TeamB]" wrote
>>"Chris Mullins" <cm******@yahoo.comwrote:
OB*************@TK2MSFTNGP05.phx.gbl...
| For reason's I'm not quite clear on, you're not supposed to return a
| List<T>. They recommend using the Generic classes found in the
| System.Collections.ObjectModel namespace instead.

Hmm, Do you know if IList<Tis equally frowned upon ?

Code Analysis (via Team System) is perfectly happy with IList<T>. Seems
to be if one fails, they both should fail.

Maybe it has nothing to do with generics and it just wants
to enforce the good habit of returning interfaces instead
of concrete classes.
If only it were that simple.

The classes they recommend be returned, in the
System.Collections.ObjectModel namespace, are not interfaces. They're
standard classes.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Oct 10 '06 #11
Chris Mullins wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote
>Chris Mullins wrote:
>>"Joanna Carter [TeamB]" wrote
"Chris Mullins" <cm******@yahoo.comwrote:
OB*************@TK2MSFTNGP05.phx.gbl...
| For reason's I'm not quite clear on, you're not supposed to return a
| List<T>. They recommend using the Generic classes found in the
| System.Collections.ObjectModel namespace instead.

Hmm, Do you know if IList<Tis equally frowned upon ?
Code Analysis (via Team System) is perfectly happy with IList<T>. Seems
to be if one fails, they both should fail.
Maybe it has nothing to do with generics and it just wants
to enforce the good habit of returning interfaces instead
of concrete classes.

If only it were that simple.

The classes they recommend be returned, in the
System.Collections.ObjectModel namespace, are not interfaces. They're
standard classes.
http://pluralsight.com/blogs/craig/a.../21/15770.aspx
http://www.gotdotnet.com/team/fxcop/...ericLists.html

indicates that it is because System.Collections.ObjectModel.Collection
is intended to be extended while List is not.

No - it is not that obvious to me.

Arne
Oct 11 '06 #12
"Arne Vajhøj" <ar**@vajhoej.dkwrote:
Chris Mullins wrote:
>>
The classes they recommend be returned, in the
System.Collections.ObjectModel namespace, are not interfaces. They're
standard classes.

http://pluralsight.com/blogs/craig/a.../21/15770.aspx
http://www.gotdotnet.com/team/fxcop/...ericLists.html

indicates that it is because System.Collections.ObjectModel.Collection
is intended to be extended while List is not.
That's the answer I saw as well - but it sure seems like a pretty weak
answer to me. I still don't understand their motivation, as there are a
number of cases where an List<Tis EXACTLY what I would want to return.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Oct 11 '06 #13
Chris Mullins wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote:
>Chris Mullins wrote:
>>The classes they recommend be returned, in the
System.Collections.ObjectModel namespace, are not interfaces. They're
standard classes.
http://pluralsight.com/blogs/craig/a.../21/15770.aspx
http://www.gotdotnet.com/team/fxcop/...ericLists.html

indicates that it is because System.Collections.ObjectModel.Collection
is intended to be extended while List is not.

That's the answer I saw as well - but it sure seems like a pretty weak
answer to me. I still don't understand their motivation, as there are a
number of cases where an List<Tis EXACTLY what I would want to return.
Me too.

But ...

Arne
Oct 11 '06 #14
"Arne Vajhøj" <ar**@vajhoej.dka écrit dans le message de news:
XsXWg.20907$2g4.9052@dukeread09...

| http://pluralsight.com/blogs/craig/a.../21/15770.aspx
|
http://www.gotdotnet.com/team/fxcop/...ericLists.html
|
| indicates that it is because System.Collections.ObjectModel.Collection
| is intended to be extended while List is not.

To quote a comment on the first blog :
Tomas, probably because they want you to derive from Collection<T>
to expose your strongly-typed collection classes in public APIs rather
than using List<T>. A
I thought the purpose of generic types was that you didn't have to derive
from them to provide strongly typed collections ! ? Huh ?
To quote the example in the first blog :

// This class satisfies the rule.
public class GenericIntegerCollection:
CollectionBase, IEnumerable<int>
{
public int Add(int value)
{
return InnerList.Add(value);
}

// Other method overrides using Int32.

public new IEnumerator<intGetEnumerator()
{
foreach (int data in InnerList)
{
yield return data;
}
}
}

If I were designing a generic collection class, why would I use
CollectionBase to derive from ? This forces me to hold all my items in the
InnerList, which is an ArrayList !!! Surely the point of a generic
collection is that the items are stored as their native type, not
cast/boxed/unboxed on the way into/out of the storage ?

Methinks this train of thought has the smell of a .NET 1.1 programmer who
doesn't understand generics, enforcing their ideas on fxCop.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Oct 11 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: noname | last post by:
I'm learning C# generics recenly. How do i do to write the similar Java function below in C#? void printCollection(Collection<? extends A> c) { for (Object e: c) { System.out.println(e); }} ...
4
by: Chuck Cobb | last post by:
I have a question regarding generics: Suppose I want to create some generic collection classes: Collection<Cats> c; Collection<Dogs> d; and both Cats and Dogs are inherited from a base class...
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
3
by: Showjumper | last post by:
Back in asp.net 1.1 i made custom collection classes per Karl Seguin's article On the Way to Mastering ASP.NET: Introducing Custom Entity Classes to take advantage of strongly typed data. Now with...
7
by: JCauble | last post by:
I have a question about using Generics with Interfaces and some of there inheritance issues / problems. If this is not possible what I describe below I will have to go a different route and would...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
7
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
Can someone explain a few things about collections to me. In C# 2 generics, you can create a collection class by inheriting from System.Collections.ObjectModel.Collection. Using this you can...
2
by: =?Utf-8?B?QnJhdmVzQ2hhcm0=?= | last post by:
I am trying to convert a class I have to generics and I can't seem to find any possible why to implement it. I'm beginning to think I'm doing something I shouldn't or I hit generics limitation. ...
20
by: -- | last post by:
Imagine I have a class TypeX and a class TypeY that inherts TypeX. public class typeX { .... } public class typeY : typeX { ....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.