Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to create a User Defined Collection

0.00/5 (No votes)
7 Oct 2006 2  
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 Collections

Business 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 called User . 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 collection
public 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 implement Add, 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 in Add or new method called Save that maybe used for transactions or batch processes.

Conclusion

Maybe in .Net Framework 2.0 we have a better replacement to these old styled collections with Generics but still i think there maybe some situation that these old style collections will be useful. In my next article i will play with Basic concepts of Generics. At the end I would like to thanks Mayank .C for giving me such a nice idea.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here