Geeks With Blogs

News Ashraful Alam Joy

Create Your Badge

Ashraful Alam is a Software Architect, who has 8 years of professional experience in Software Development industry. This Bangladeshi national is involved with project management and development of several US based software projects from his country. Already he has managed and developed several software projects, which are being used by several users of different countries, such as USA, Canada, Australia, and Bangladesh. While developing and managing a team, he contains and maintains a set of well defined engineering practices developed by him and other online developer communities.

Due to his willingness to give effort to improve and share better software development practices, Ashraf has been awarded as “Most Valuable Professional” (MVP) in ASP.NET category by Microsoft since year 2007 multiple times, which is a rare honor and prestigious reorganization among the developers around the world.

Check his portfolio to know more about him and his works.


.NETTER Characters... Every part of your life is best, if you can know yourself and thus create your life like an artist!

Generics is a new feature in .NET 2.0 which allows us to create a data structure without committing to a specific data type. In the early age of .net 2.0 (i.e. .net 1.x), while using custom entity model in our application data architecture, we had to create a separate class regarding the collection for each of custom entity.

The custom entity
public class SystemUser
{
    public string _Name, _Password;

    public SystemUser(string name, string password)
    {
        _Name = name;
        _Password = password;
    }

    public string Password
    {
        get { return _Password; }
        set { _Password = value; }
    }

    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

The old age custom entity collection class

For a custom entity "SystemUser" we had to create a separate collection class as below:

public class SystemUserCollection: CollectionBase
{
    public SystemUser this[ int index ]
    {
        get{return ( (SystemUser) List[index]);}
        set{List[index]=value;}
    }
    
    public int Add(SystemUser value)
    {
        return (List.Add(value));
    }
    
    public int IndexOf(SystemUser value)
    {
        return (List.IndexOf(value));
    }
    
    public void Insert(int index, SystemUser value)
    {
        List.Insert(index, value);
    }
    
    public void Remove(SystemUser value)
    {
        List.Remove(value);    
    }
    
    public bool Contains(SystemUser value)
    {
        return (List.Contains(value));    
    }
  
}
Custom entity collection using .net 2.0 generics

The generics in .net 2.0 made developers life extremely easy, we can create a common collection class for all custom business entity with respect to common collection based functionality.

/// <summary>
/// This is the helper collection class, that holds 
generic type data.
/// This type will be used to manipulate generic class.
/// </summary>
/// <typeparam name="GenericType"></typeparam>
public class CustomCollection<GenericType> : CollectionBase
{
        /// <summary>
        /// Adds an object to the collection.
        /// </summary>
        /// <param name="GenericObject"></param>
        public void Add(GenericType item)
        {
            InnerList.Add(item);
        }

        /// <summary>
        /// Removes an object from the collection.
        /// </summary>
        /// <param name="index"></param>
        public void Remove(int index)
        {
            InnerList.RemoveAt(index);
        }

        /// <summary>
        /// Gets and sets the appropriate object 
in the specified index.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public GenericType this[int index]
        {
            get { return ((GenericType)List[index]); }
            set { List[index] = value; }
        }

}//end of GenericCollection<GenericType>

Using generic collection

Generic collection classes can be used in the following way:

public static CustomCollection<SystemUser> 
GetSampleGenericCollection()
{
    SystemUser user1 = new SystemUser("ashraf", "12345");
    SystemUser user2 = new SystemUser("ehsan", "12345");
    SystemUser user3 = new SystemUser("tuhin", "12345");
    SystemUser user4 = new SystemUser("rabbi", "12345");

    CustomCollection<SystemUser> mySystemUserCollection 
= new CustomCollection<SystemUser>();

    mySystemUserCollection.Add(user1);
    mySystemUserCollection.Add(user2);
    mySystemUserCollection.Add(user3);
    mySystemUserCollection.Add(user4);

    return mySystemUserCollection;

}

Download code:

Technorati Tags: ,,,,
Posted on Monday, February 18, 2008 5:12 PM Implementation , Database , .NET , Software Development | Back to top


Comments on this post: Using .NET 2.0 Generics for Custom Entity Collection

# re: Using .NET 2.0 Generics for Custom Entity Collection
Requesting Gravatar...
Thank you for your schemes of programming such complex programs with the help of internet.
Left by coursework writing on Jan 10, 2011 2:38 AM

Your comment:
 (will show your gravatar)


Copyright © Ashraf Alam | Powered by: GeeksWithBlogs.net