Custom Collections
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngarchitecture' list.


Daniel Wilson
Hello Everyone,
I wanted to ask everyone a quick question about how do you go about
creating a custom collection, say, UserCollection. Do you create a child
class, UserCollection that inherits from a class, perhaps ArrayList OR do
you create a class, UserCollection that implements the needed interfaces.
(i.e. UserCollection: IEnumerable, IList).
I started out doing the latter of the two and implementing say
CollectionBase if needed because I didn't want a change in ArrayList (by
someone other than me) to propagate to all of my custom collections. (This
may not be a big deal, what do you think). What considerations do you take
into account when choosing between the two or in taking a hybrid approach?

One final question, Is there any way for me to hide (disallow access
to) a method in a base class from users of my child class? For example, if
I derive from ArrayList when I create UserCollection how can I "hide" the
RemoveAt function?

Thanks,
Daniel Wilson

-----Original Message-----
From: Brian Bilbro [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 09, 2002 8:48 AM
To: aspngarchitecture
Subject: [aspngarchitecture] Re: Business Object design for WebForm and
WinForm...

You can use the following code:

if(System.Web.HttpContext.Current!=null)
{
// Here you can use
// System.Web.HttpContext.Current.Cache
// or
// System.Web.HttpContext.Current.Application
}
else
{
// Your not in an ASP.NET environment
// and you'll have to create your own
// caching mechanism
}

HTHs,
Brian

----- Original Message -----
From: Mark <mailto:Click here to reveal e-mail address> Feinholz
To: aspngarchitecture <mailto:Click here to reveal e-mail address>
Sent: 07/09/2002 2:20 AM
Subject: [aspngarchitecture] Business Object design for WebForm and
WinForm...

I've got business objects (custom objects, collections of objects,
hierarchies, etc) that I want to use for both web apps and windows apps.
They use a helper object that takes care of talking to the sql server
database (sets up parameter lists, builds commands, etc). This helper
object (SqlPersist) is instanciated by the Update method at the top of the
object tree - it will contain the open SqlConnection and SqlTransaction
objects and it will get passed on down through the hierarchy as Update
methods are run on all of the dirty objects.

I would like to be able to cache the arrays of parameters that the helper
object builds for each update request in a Hashtable whose key is the Stored
Procedures name so that if there is 50 objects in one of the collections in
the hierarchy, each object's Update method doesn't have to rebuild the exact
same array of SqlParameters - but instead can just look it up and update
it's Value.

So, my question, does anyone know how I can write code that can detect
whether or not the client application for these business objects that are
using the SqlPersist helper object is an asp.net application or not? I
would like to use asp.net's Cache if it is a Web App and use an internally
managed cache if it is not.

Thanks, Mark.

| [aspngarchitecture] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives
Reply to this message...
 
    
Arief Budimartoyo
You may have to make at least 2 class, 1 is an item class, and second is a
list class ...
item class will have any properties you want to publish for read only or
able to set properties.
list class will have any methods you can customize like Add, Clear, RemoveAt
or whatever that will manipulate a Collection class inside ... You can use
HashTable, DataTable, or ArrayList ...
some Properties also can make here like Count, Total, etc.. and for
implementation of your collection you must make ICollection properties to
get Values of collection , and also Item type Properties to get [i]
collection ...
the code are something like this :

public class MyItem {
private string _id;
private string _name;
public MyItem(string id,string name) {
this._id=id;
this._name=name;
}
public string ID {
get {return _id;}
}
public string Name {
get {return _name;}
}
}

public class MyList {
private HashTable myColl = new HashTable();

public int Count {
get {myColl.Count;
}
}
public ICollection Values {
get {return myColl.Values;}
}
public MyItem this[int key] {
get {return (MyItem) myColl[key];}
}
public void Add(MyItem value) {
myColl.Add(value.key,value)
}
public void RemoveItembyKey(object key) {
myColl.Remove(key);
}
public void Clear() {
myColl.Clear();
}
}

so you can hide some method from collection class used ...

hth.
Arief Budimartoyo

-----Original Message-----
From: Daniel Wilson [mailto:Click here to reveal e-mail address]
Sent: Wednesday, July 10, 2002 12:56 AM
To: aspngarchitecture
Subject: [aspngarchitecture] Custom Collections

Hello Everyone,
I wanted to ask everyone a quick question about how do you go about
creating a custom collection, say, UserCollection. Do you create a child
class, UserCollection that inherits from a class, perhaps ArrayList OR do
you create a class, UserCollection that implements the needed interfaces.
(i.e. UserCollection: IEnumerable, IList).
I started out doing the latter of the two and implementing say
CollectionBase if needed because I didn't want a change in ArrayList (by
someone other than me) to propagate to all of my custom collections. (This
may not be a big deal, what do you think). What considerations do you take
into account when choosing between the two or in taking a hybrid approach?

One final question, Is there any way for me to hide (disallow access
to) a method in a base class from users of my child class? For example, if
I derive from ArrayList when I create UserCollection how can I "hide" the
RemoveAt function?

Thanks,
Daniel Wilson

-----Original Message-----
From: Brian Bilbro [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 09, 2002 8:48 AM
To: aspngarchitecture
Subject: [aspngarchitecture] Re: Business Object design for WebForm and
WinForm...

You can use the following code:

if(System.Web.HttpContext.Current!=null)
{
// Here you can use
// System.Web.HttpContext.Current.Cache
// or
// System.Web.HttpContext.Current.Application
}
else
{
// Your not in an ASP.NET environment
// and you'll have to create your own
// caching mechanism
}

HTHs,
Brian

----- Original Message -----
From: Mark <mailto:Click here to reveal e-mail address> Feinholz
To: aspngarchitecture <mailto:Click here to reveal e-mail address>
Sent: 07/09/2002 2:20 AM
Subject: [aspngarchitecture] Business Object design for WebForm and
WinForm...

I've got business objects (custom objects, collections of objects,
hierarchies, etc) that I want to use for both web apps and windows apps.
They use a helper object that takes care of talking to the sql server
database (sets up parameter lists, builds commands, etc). This helper
object (SqlPersist) is instanciated by the Update method at the top of the
object tree - it will contain the open SqlConnection and SqlTransaction
objects and it will get passed on down through the hierarchy as Update
methods are run on all of the dirty objects.

I would like to be able to cache the arrays of parameters that the helper
object builds for each update request in a Hashtable whose key is the Stored
Procedures name so that if there is 50 objects in one of the collections in
the hierarchy, each object's Update method doesn't have to rebuild the exact
same array of SqlParameters - but instead can just look it up and update
it's Value.

So, my question, does anyone know how I can write code that can detect
whether or not the client application for these business objects that are
using the SqlPersist helper object is an asp.net application or not? I
would like to use asp.net's Cache if it is a Web App and use an internally
managed cache if it is not.

Thanks, Mark.

| [aspngarchitecture] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngarchitecture] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives
Reply to this message...
 
    
Daniel Wilson
Thank you, Arief, very much for your reply.
In your example you are implementing an internal data structure and
allowing access to particular functionality by only implementing the
functions of that data structure you want the user to use, which is a
possible way. However you didn't derive your collection class from
HashTable and "hide" specific methods. Is the example you gave- implement
the desired functionality by implementing an internal data structure and
implementing the desired functions - the only way to "hide" some
functionality. There isn't a way to do this by using inheritance?
Also I think the way I worded of my question isn't very clear. Second
Attempt:
Basically I've seen two different methods for implementing custom
"collections", Users, for a custom business object, User. One is to inherit
from a data structure Class - ArrayList, HashTable,Queue, Stack, Collection
Base - the other is to implement a data structure internally as in Arief's
example and perhaps have the custom "collection" implement certain
interfaces. My question is which one, if either, you, personally, use when
implementing your own custom "collections" and why? This is meant to be a
generally poll of subscribers of this list.

Thanks again,
Daniel O. Wilson

----Original Message-----
From: Arief Budimartoyo [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 09, 2002 11:11 PM
To: aspngarchitecture
Subject: [aspngarchitecture] RE: Custom Collections

You may have to make at least 2 class, 1 is an item class, and second is a
list class ...
item class will have any properties you want to publish for read only or
able to set properties.
list class will have any methods you can customize like Add, Clear, RemoveAt
or whatever that will manipulate a Collection class inside ... You can use
HashTable, DataTable, or ArrayList ...
some Properties also can make here like Count, Total, etc.. and for
implementation of your collection you must make ICollection properties to
get Values of collection , and also Item type Properties to get [i]
collection ...
the code are something like this :

public class MyItem {
private string _id;
private string _name;
public MyItem(string id,string name) {
this._id=id;
this._name=name;
}
public string ID {
get {return _id;}
}
public string Name {
get {return _name;}
}
}

public class MyList {
private HashTable myColl = new HashTable();

public int Count {
get {myColl.Count;
}
}
public ICollection Values {
get {return myColl.Values;}
}
public MyItem this[int key] {
get {return (MyItem) myColl[key];}
}
public void Add(MyItem value) {
myColl.Add(value.key,value)
}
public void RemoveItembyKey(object key) {
myColl.Remove(key);
}
public void Clear() {
myColl.Clear();
}
}

so you can hide some method from collection class used ...

hth.
Arief Budimartoyo

-----Original Message-----
From: Daniel Wilson [mailto:Click here to reveal e-mail address]
Sent: Wednesday, July 10, 2002 12:56 AM
To: aspngarchitecture
Subject: [aspngarchitecture] Custom Collections

Hello Everyone,
I wanted to ask everyone a quick question about how do you go about
creating a custom collection, say, UserCollection. Do you create a child
class, UserCollection that inherits from a class, perhaps ArrayList OR do
you create a class, UserCollection that implements the needed interfaces.
(i.e. UserCollection: IEnumerable, IList).
I started out doing the latter of the two and implementing say
CollectionBase if needed because I didn't want a change in ArrayList (by
someone other than me) to propagate to all of my custom collections. (This
may not be a big deal, what do you think). What considerations do you take
into account when choosing between the two or in taking a hybrid approach?

One final question, Is there any way for me to hide (disallow access
to) a method in a base class from users of my child class? For example, if
I derive from ArrayList when I create UserCollection how can I "hide" the
RemoveAt function?

Thanks,
Daniel Wilson

-----Original Message-----
From: Brian Bilbro [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 09, 2002 8:48 AM
To: aspngarchitecture
Subject: [aspngarchitecture] Re: Business Object design for WebForm and
WinForm...

You can use the following code:

if(System.Web.HttpContext.Current!=null)
{
// Here you can use
// System.Web.HttpContext.Current.Cache
// or
// System.Web.HttpContext.Current.Application
}
else
{
// Your not in an ASP.NET environment
// and you'll have to create your own
// caching mechanism
}

HTHs,
Brian

----- Original Message -----
From: Mark <mailto:Click here to reveal e-mail address> Feinholz
To: aspngarchitecture <mailto:Click here to reveal e-mail address>
Sent: 07/09/2002 2:20 AM
Subject: [aspngarchitecture] Business Object design for WebForm and
WinForm...

I've got business objects (custom objects, collections of objects,
hierarchies, etc) that I want to use for both web apps and windows apps.
They use a helper object that takes care of talking to the sql server
database (sets up parameter lists, builds commands, etc). This helper
object (SqlPersist) is instanciated by the Update method at the top of the
object tree - it will contain the open SqlConnection and SqlTransaction
objects and it will get passed on down through the hierarchy as Update
methods are run on all of the dirty objects.

I would like to be able to cache the arrays of parameters that the helper
object builds for each update request in a Hashtable whose key is the Stored
Procedures name so that if there is 50 objects in one of the collections in
the hierarchy, each object's Update method doesn't have to rebuild the exact
same array of SqlParameters - but instead can just look it up and update
it's Value.

So, my question, does anyone know how I can write code that can detect
whether or not the client application for these business objects that are
using the SqlPersist helper object is an asp.net application or not? I
would like to use asp.net's Cache if it is a Web App and use an internally
managed cache if it is not.

Thanks, Mark.

| [aspngarchitecture] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngarchitecture] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngarchitecture] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives
Reply to this message...
 
    
Mark Feinholz
My object collections inherit from CollectionBase and I use the OnInsert
and OnRemove events/methods to make validate and possibly reject adds
and removes. A client application using my object collections for
example cannot remove objects from the collection that have changes that
need to be persisted to the db and they can only insert objects into a
collection that pass a few rules.

-----Original Message-----
From: Daniel Wilson [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 09, 2002 10:56 AM
To: aspngarchitecture
Subject: [aspngarchitecture] Custom Collections

Hello Everyone,
I wanted to ask everyone a quick question about how do you go about
creating a custom collection, say, UserCollection. Do you create a
child class, UserCollection that inherits from a class, perhaps
ArrayList OR do you create a class, UserCollection that implements the
needed interfaces. (i.e. UserCollection: IEnumerable, IList).
I started out doing the latter of the two and implementing say
CollectionBase if needed because I didn't want a change in ArrayList (by
someone other than me) to propagate to all of my custom collections.
(This may not be a big deal, what do you think). What considerations do
you take into account when choosing between the two or in taking a
hybrid approach?

One final question, Is there any way for me to hide (disallow
access to) a method in a base class from users of my child class? For
example, if I derive from ArrayList when I create UserCollection how can
I "hide" the RemoveAt function?

Thanks,
Daniel Wilson

-----Original Message-----
From: Brian Bilbro [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 09, 2002 8:48 AM
To: aspngarchitecture
Subject: [aspngarchitecture] Re: Business Object design for WebForm and
WinForm...

You can use the following code:

if(System.Web.HttpContext.Current!=null)
{
// Here you can use
// System.Web.HttpContext.Current.Cache
// or
// System.Web.HttpContext.Current.Application
}
else
{
// Your not in an ASP.NET environment
// and you'll have to create your own
// caching mechanism
}

HTHs,
Brian

----- Original Message -----
From: Mark <mailto:Click here to reveal e-mail address> Feinholz
To: aspngarchitecture <mailto:Click here to reveal e-mail address>
Sent: 07/09/2002 2:20 AM
Subject: [aspngarchitecture] Business Object design for WebForm and
WinForm...

I've got business objects (custom objects, collections of objects,
hierarchies, etc) that I want to use for both web apps and windows apps.
They use a helper object that takes care of talking to the sql server
database (sets up parameter lists, builds commands, etc). This helper
object (SqlPersist) is instanciated by the Update method at the top of
the object tree - it will contain the open SqlConnection and
SqlTransaction objects and it will get passed on down through the
hierarchy as Update methods are run on all of the dirty objects.

I would like to be able to cache the arrays of parameters that the
helper object builds for each update request in a Hashtable whose key is
the Stored Procedures name so that if there is 50 objects in one of the
collections in the hierarchy, each object's Update method doesn't have
to rebuild the exact same array of SqlParameters - but instead can just
look it up and update it's Value.

So, my question, does anyone know how I can write code that can detect
whether or not the client application for these business objects that
are using the SqlPersist helper object is an asp.net application or not?
I would like to use asp.net's Cache if it is a Web App and use an
internally managed cache if it is not.

Thanks, Mark.

| [aspngarchitecture] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngarchitecture] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives
Reply to this message...
 
    
Brian Espey
if you inherit from a class, then there isn't a way to
hide the public members of the base class from the
client. if you need to do that, then you probably
shouldn't be inheriting from the class, because of the
"is-a" rule. x is-not-a y if x can't do something
that y can. you could override the virtual methods in
your subclass to do a no-op or throw an exception, but
users would still be able to call the method, and
would get unexpected results. so if you need to hide
a bunch of methods, you should use an alternate
approach (like Arief's)

Brian

--- Daniel Wilson <Click here to reveal e-mail address> wrote:
[Original message clipped]


__________________________________________________
Do You Yahoo!?
Sign up for SBC Yahoo! Dial - First Month Free
http://sbc.yahoo.com

Reply to this message...
 
    
Chris Anderson
Doesn't the C# "new" modifer(*) allow that to be done?

(*) or the VB "shadows" modifier

Merak

[Original message clipped]

Reply to this message...
 
    
Brian Espey
not in the sense that I understood him to mean from
his question. the client would still see the method
declared as new/shadows on the derived class, but his
goal was to make it so the client can't see the
function at all (at least that's what I thought).

Brian

--- Chris Anderson <Click here to reveal e-mail address> wrote:
[Original message clipped]

__________________________________________________
Do You Yahoo!?
Sign up for SBC Yahoo! Dial - First Month Free
http://sbc.yahoo.com

Reply to this message...
 
    
Russ McClelland
Agreed, but in C++ you could do this (I believe) by modifying the base
class specification:

Class Derived: private Base
{
}

Allows Derived to inherit all behavior, but changes all inherited
methods to private. There are a few times when I think this would be
helpful, although aggregation would be acceptable solution. Still,
would be a nice shortcut so that you wouldn't have to write a bunch of
forwarding code to send things to the aggregate.

Curiously, they do allow you to hide all inherited methods using the
"new" keyword at the class level:

new Derived : Base
{
}

-----Original Message-----
From: Brian Espey [mailto:Click here to reveal e-mail address]=20
Sent: Wednesday, July 10, 2002 5:56 PM
To: aspngarchitecture
Subject: [aspngarchitecture] RE: Custom Collections

if you inherit from a class, then there isn't a way to
hide the public members of the base class from the
client. if you need to do that, then you probably
shouldn't be inheriting from the class, because of the
"is-a" rule. x is-not-a y if x can't do something
that y can. you could override the virtual methods in
your subclass to do a no-op or throw an exception, but
users would still be able to call the method, and
would get unexpected results. so if you need to hide
a bunch of methods, you should use an alternate
approach (like Arief's)

Brian

--- Daniel Wilson <Click here to reveal e-mail address> wrote:
[Original message clipped]


__________________________________________________
Do You Yahoo!?
Sign up for SBC Yahoo! Dial - First Month Free http://sbc.yahoo.com

| [aspngarchitecture] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.asplists.com/asplists/aspngarchitecture.asp =3D JOIN/QUIT=20
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
    
Daniel Wilson
Brian you are correct, I was looking for a way to hide methods from the
client not create similiarly named methods with different parameters. The
reason I was looking for a way to do this is for the exact reason Russ
stated - so I wouldn't have to write a lot of forwarding code
To my base class. Thanks also for the C++ example, neat.

Thanks,
-Daniel Wilson

-----Original Message-----
From: Russ McClelland [mailto:Click here to reveal e-mail address]
Sent: Wednesday, July 10, 2002 9:23 PM
To: aspngarchitecture
Subject: [aspngarchitecture] RE: Custom Collections

Agreed, but in C++ you could do this (I believe) by modifying the base class
specification:

Class Derived: private Base
{
}

Allows Derived to inherit all behavior, but changes all inherited methods to
private. There are a few times when I think this would be helpful, although
aggregation would be acceptable solution. Still, would be a nice shortcut
so that you wouldn't have to write a bunch of forwarding code to send things
to the aggregate.

Curiously, they do allow you to hide all inherited methods using the "new"
keyword at the class level:

new Derived : Base
{
}

-----Original Message-----
From: Brian Espey [mailto:Click here to reveal e-mail address]
Sent: Wednesday, July 10, 2002 5:56 PM
To: aspngarchitecture
Subject: [aspngarchitecture] RE: Custom Collections

if you inherit from a class, then there isn't a way to
hide the public members of the base class from the
client. if you need to do that, then you probably
shouldn't be inheriting from the class, because of the
"is-a" rule. x is-not-a y if x can't do something
that y can. you could override the virtual methods in
your subclass to do a no-op or throw an exception, but
users would still be able to call the method, and
would get unexpected results. so if you need to hide
a bunch of methods, you should use an alternate
approach (like Arief's)

Brian

--- Daniel Wilson <Click here to reveal e-mail address> wrote:
[Original message clipped]


__________________________________________________
Do You Yahoo!?
Sign up for SBC Yahoo! Dial - First Month Free http://sbc.yahoo.com

| [aspngarchitecture] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngarchitecture] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
mail
Great!!!!!!! i was looking for this!!!!!!!!. When you inherit i call it
a "Is a relation" this would be a "Look like relation" were you inherit
all code but not letting anyone know it. Tell me whyyyyyyyyyyyy i
dosen't work in C#

-----Original Message-----
From: "Russ McClelland" <Click here to reveal e-mail address>
To: "aspngarchitecture" <Click here to reveal e-mail address>
Date: Wed, 10 Jul 2002 20:22:46 -0500
Subject: [aspngarchitecture] RE: Custom Collections

[Original message clipped]

Reply to this message...
 
    
Andy Smith
why? because c# isn't c++, it just looks like it.

there are helper tools available that make creating =
containing-relationships easy.
I suggest you get one of those.

__
Andy Smith
Keyboard Jockey #3a7-2.78.1

[Original message clipped]

Reply to this message...
 
 
System.Collections.ArrayList
System.Collections.CollectionBase
System.Collections.ICollection
System.Collections.IEnumerable
System.Collections.IList
System.Data.DataTable
System.Data.SqlClient.SqlConnection
System.Data.SqlClient.SqlTransaction
System.Web.HttpContext




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com