473,769 Members | 7,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.CollectionB ase
{
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
14 1709
"Arne Vajhøj" <ar**@vajhoej.d kwrote
Chris Mullins wrote:
>"Joanna Carter [TeamB]" wrote
>>"Chris Mullins" <cm******@yahoo .comwrote:
OB************* @TK2MSFTNGP05.p hx.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.Collecti ons.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.Collecti ons.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.d kwrote
>Chris Mullins wrote:
>>"Joanna Carter [TeamB]" wrote
"Chris Mullins" <cm******@yahoo .comwrote:
OB************* @TK2MSFTNGP05.p hx.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.Collecti ons.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.Collecti ons.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.Collecti ons.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.d kwrote:
Chris Mullins wrote:
>>
The classes they recommend be returned, in the
System.Collect ions.ObjectMode l 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.Collecti ons.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.d kwrote:
>Chris Mullins wrote:
>>The classes they recommend be returned, in the
System.Collec tions.ObjectMod el 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.Collecti ons.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.d ka écrit dans le message de news:
XsXWg.20907$2g4 .9052@dukeread0 9...

| http://pluralsight.com/blogs/craig/a.../21/15770.aspx
|
http://www.gotdotnet.com/team/fxcop/...ericLists.html
|
| indicates that it is because System.Collecti ons.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 GenericIntegerC ollection:
CollectionBase, IEnumerable<int >
{
public int Add(int value)
{
return InnerList.Add(v alue);
}

// Other method overrides using Int32.

public new IEnumerator<int GetEnumerator()
{
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
1639
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); }} In printCollection, I have to limit the bound of the argument, when is a generic of Collection with some classes inherites from A. Does anyone know
4
1420
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 called Animals. I'd like to have a class: Collection<Animals>
5
2921
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 an "AppleBasket" which is a class that contains a collection of apples. So, some code: class Fruit{ }
3
2606
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 generics i understand that one doesnt have to used these custom collections. So i want to move this 1.1 project to 2.0. I have been reading about generics but i geuss i dont really get hpw to implement them. For example i have 2 classees in the...
7
2098
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 like some suggestions. I am unable to use abstract classes as my code must not effect any current inheritance chains so I am using interfaces. So I have something like this: interface IBase { Collection<IBaseItemItems{get;set;}
7
3257
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 System.Collections.Generic namespace. Literature I have read on this ties generics in with collections, hence articulate their examples as such. That's fine, I understand what is being said. My question is more towards the application and implementation...
7
5748
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 iterate through the collection and use "foreach" on the collection. public class Cars : System.Collections.ObjectModel.Collection<Car{ } So, could someone explain to me why you would want to create a GENERIC custom collection class.
2
138
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. Here is an example of the old way: static void Main(string args) { MonitorCenter monitor = new MonitorCenter(); monitor.SetMonitor(new ExceptionMonitor());
20
1850
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
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.