473,757 Members | 2,066 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 #1
14 1708
Heck yes, Corey! It's quite simple:

using System.Collecti ons.ObjectModel ;

public class AppleList : Collection<Appl e{}

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.comwro te in message
news:11******** ******@sp6iad.s uperfeed.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.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 #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 OrangeCollectio nBase<T: DataCollection< Twhere
T : OrangeBase, new()
{
}

Then your derived collection class would look like this:

public class Oranges : OrangeCollectio nBase<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.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 #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.govwro te in message
news:eQ******** **********@TK2M SFTNGP04.phx.gb l...
Heck yes, Corey! It's quite simple:

using System.Collecti ons.ObjectModel ;

public class AppleList : Collection<Appl e{}

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<OrangeList AllOranges();

Aug 4 '06 #5
<wf****@gmail.c oma écrit dans le message de news:
11************* *********@p79g2 00...legr oups.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<TRetrieveL ist<T>()
{
...
}

...
}

void test()
{
List<Customercu stList = DAL.RetrieveLis t<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<TRetrieveL ist<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.Collecti ons.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.p hx.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.Collecti ons.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<Tc lass because ReadOnlyCollect ion<T>
raises exceptions on all the IList<Timplemen ting 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.p hx.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.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.
>
| 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<Tc lass because
ReadOnlyCollect ion<T>
raises exceptions on all the IList<Timplemen ting 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.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.

Arne
Oct 10 '06 #10

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
1419
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
2605
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
1847
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
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9904
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...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7285
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
5168
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
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
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
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.