.Net and Active Directory - An OO solution to authority structure - Part 4

Now that we have a means of instantiating a User and Group object using their DirectoryEntry, lets create a means to associate them together.  We will build off this step by attaching these Collections to our Owner object so as to represent the groups they are associated with (GroupCollection), and the people that report to our Owner (UserCollection).  So without further ado, lets create our UserCollection.

using System;
using System.Collections;
 
namespace ActiveDirectory_Demo1.GlobalObjects.AuthorityObjects
{
    /// <summary>
    /// Summary description for UserCollection.
    /// </summary>
    public class UserCollection : CollectionBase
    {
        /// <summary>
        /// Enumerator for this collection.
        /// </summary>
        public virtual User this[int index] 
        {
            get 
            {
                return (User) this.List[index];
            }
            set 
            {
                this.List[index] = value;
            }
        }
 
        /// <summary>
        /// Returns the index (position) of an item in the collection.
        /// </summary>
        /// <param name="item">Item to be searched.</param>
        /// <returns>Returns the index(position) of the item.</returns>
        public virtual int IndexOf(User item) 
        {
            return this.List.IndexOf(item);
        }
    
        /// <summary>
        /// This method appends an item to the collection.
        /// </summary>
        /// <param name="item">Item to be appended to the collection.</param>
        /// <returns>Returns an integer indicating if the item was added.</returns>
        public virtual int Add(User item) 
        {
            if(!this.List.Contains(item))
            {this.List.Add(item);}
            return this.List.Count-1;
        }
    }
}

If you have done any Object Oriented Programming before this is old hat.  We are creating a custom object, based on the CollectionBase object.  While this is by no means all the ways we can manipulate a Collection, the methods shown are all we need for this exercise.  We need:

  • A method to return the User object at a particular position in our Collection
  • A method to get the current User's ordinal position in the Collection
  • And finally a way to add User objects to our UserCollection

So, lets create a nearly identical Collection object for Groups:

using System;
using System.Collections;
 
namespace ActiveDirectory_Demo1.GlobalObjects.AuthorityObjects
{
    /// <summary>
    /// Summary description for GroupCollection.
    /// </summary>
    public class GroupCollection : CollectionBase
    {
        /// <summary>
        /// Enumerator for this collection.
        /// </summary>
        public virtual Group this[int index] 
        {
            get 
            {
                return (Group) this.List[index];
            }
            set 
            {
                this.List[index] = value;
            }
        }
 
        /// <summary>
        /// Returns the index (position) of an item in the collection.
        /// </summary>
        /// <param name="item">Item to be searched.</param>
        /// <returns>Returns the index(position) of the item.</returns>
        public virtual int IndexOf(Group item) 
        {
            return this.List.IndexOf(item);
        }
    
        /// <summary>
        /// This method appends an item to the collection.
        /// </summary>
        /// <param name="item">Item to be appended to the collection.</param>
        /// <returns>Returns an integer indicating if the item was added.</returns>
        public virtual int Add(Group item) 
        {
            if(!this.List.Contains(item))
            {this.List.Add(item);}
            return this.List.Count-1;
        }
 
        public virtual bool Contains(string groupname)
        {
            bool rtn = false;
            foreach(Group group in this)
            {
                if(group.GroupName == groupname)
                {
                    rtn = true;break;
                }
            }
            return rtn;
        }
    }
}

Wow, pretty much the same thing, except when you ask for a member of this Collection it will return a Group object rather than a User object.

In the next post we will attach these newly formed Collections onto our Owner object to represent the Users and Groups they are directly associated with.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: AaronZalewski
Posted on: 1/22/2008 at 1:34 PM
Tags: , ,
Categories: Active Directory
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Comments are closed