Windows VistaVisual Studio .NET 2003Windows 2003.NET 1.1Visual Studio 2005Windows 2000Windows XP.NET 2.0C# 2.0IntermediateDevVisual StudioWindows.NETC#
How to create a User Defined Collection






1.60/5 (9 votes)
Oct 7, 2006
3 min read

51352

362
How to create a User Defined Collection
Introduction
A collection is a set of similar objects that can be accessed using only one reference. In this article i assume that you know about usage and benefits of using collections but if you need to know more about them you may find this article useful.Background
Need to using our own custom collection rather than using Objects provided by Microsoft .NET framework comes when you need to add more functionalities to your typed collection or to show a different kind of behavior without exposing new concepts. Certainly it depends on your need according to your development goals.Why Strongly Typed Collections
Despite of having a customized behavior in manipulating your business objects you may need to achieve these by implementing Strongly Typed CollectionsBusiness Logic Integrity
You can implement your business logic in your custom collections like checking for type of business object going to be stored before adding to collection or verifying properties of object to get desired results.Transaction Level Processing
Using your own collection you can define transactions at collection level to achieve integrity. For example of you have 100 Users in a collection and you wanted to update all Users data in database at once and in case of a problem you have Rollback option, you have to use your own Collection and implement all these checking's are there.Batch Process
Instead of calling processing operations for every business object separately you can make that function at collection level and support batch Process in that collection and have a more efficient design.Implementing a simple Collection
Here i am going to implement a very simple implementation of Strongly Typed Collection for my Business type calledUser
. In this implementation i have shown where you can customize your code more by adding some additional checking and methods. User class
For implementing any collection you need to have a business object. Here i have made a simple User class to demonstrate you usage of my collectionpublic class User
{
public User ( )
{
}
public User ( String userName , String password )
{
this.userName = userName;
this.password = password;
}
private String password;
public String Password
{
get { return password; }
set { password = value; }
}
private String userName;
public String UserName
{
get { return userName; }
set { userName = value; }
}
UserCollection class
I have implemented a very simple Collection class and i have given this class much functionality at once so you can access all the method in one object. I have used the CollectionBase class as a base class so objects in collection can be accessed with foreach loop. IEnumerator is there so you can get Enumeration of your collection. All required methods are implemented and i have done everything to make the code simple. For CollectionBase you need to implementAdd, Remove methods
public class UsersCollection : CollectionBase , IEnumerable , IEnumerator
{
private int index = -1;
public UsersCollection ( )
{
this.index = -1;
}
public void Add ( User user )
{
/*
* Example for applying business Logic
*
*/
if ( user != null )
{
throw new Exception ( "Your passed User Object is null" );
}
if ( user.Password.Length < 8 )
{
throw new Exception ( "User's password can not be less than 8 characters" );
}
/*
* Normal behaviour of a Collection
*/
this.List.Add ( user );
}
public void Remove ( User user )
{
this.List.Remove ( user );
}
public User this [ int index ]
{
get { return ( User ) this.List [ index ]; }
set { this.List [ index ] = value; }
}
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator ( )
{
return this;
}
#endregion
#region IEnumerator Members
public Object Current
{
get
{
return this.List [ index ];
}
}
public bool MoveNext ( )
{
this.index++;
return ( this.index < this.List.Count );
}
public void Reset ( )
{
this.index = -1;
}
#endregion
public void Save ( UsersCollection collection )
{
try
{
/*
* Insert all items of
*/
}
catch ( Exception e )
{
/*
* if you get an error in between Rollback
*/
throw e;
}
finally
{
}
}
}
Customization
To show to you that what kind of additions we can make in our Collection classes, i have put two things for you in this code. First is checking's like what is done inAdd
or new method called Save
that maybe used for transactions or batch processes.