Solved

Object-oriented Design & Analysis - Website User - Object Modelling

Posted on 2014-12-10
3
141 Views
Last Modified: 2014-12-14
Hi there

I'm quite new to OOP & am doing my first major project in this area. I've been reading quite a bit & watching loads of tutorials. However I cannot find a "best-pratice" or design pattern for a fairly common scenario. This may be a newbie question, but I hope some of you may have practical experience with this.

The use cases are complted and I have identified the various actors of the system. The website will have the following actors:
- Anonymous visitor (can make a payment or donation without becoming a member)
- EventMember (can create & modify events)
- StandardMember (can sign up for events)
- Administrator

Further there will be some external system "actors" like an analytics system.

Let's say I created a Person superclass and then 4 subclasses for the types of user. There would hardly be anything in common. For the administrator only the name may be stored. The member will need to set all the mandatory fields. The anonymous visitor could be a person or a bot. A visitor may become a member. The various users may share some common methods like search.

Should I create seperate classes for let's say visitor, member (with 2 member sub-types) and administrator. Also would it make sense to create separate classes for the external systems?

How would you model this common actor scenario?

Any help is much appreciated.
Cheers
0
Comment
Question by:riffrack
  • 2
3 Comments
 
LVL 32

Assisted Solution

by:it_saige
it_saige earned 500 total points
ID: 40492094
OOP fundamentally follows the design that an object *is* something.  In your scheme, the Person would be the base class and all others would inherit from the base implementing their own properties and methods.  To put a face on this (so to speak).

Play around with this -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace EE_Q28578061
{
	[Serializable]
	class Person : INotifyPropertyChanged
	{
		#region INotifyPropertyChanged Members
		[NonSerialized]
		private PropertyChangedEventHandler _propertyChanged;
		public event PropertyChangedEventHandler PropertyChanged
		{
			add { _propertyChanged += value; }
			remove { _propertyChanged -= value; }
		}

		protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			PropertyChangedEventHandler handler = _propertyChanged;
			if (handler != null)
				handler(sender, e);
		}
		#endregion

		#region Private Fields
		private Guid fID;
		private string fName;
		private string fFirst;
		private string fLast;
		private string fEmail;
		private bool fCanPromote = false;
		private bool fIsAuthenticated = false;
		#endregion

		#region Public Properties
		public Guid ID
		{
			get { return fID; }
			set 
			{
				if (!value.Equals(fID))
				{
					fID = value;
					OnPropertyChanged(this, new PropertyChangedEventArgs("ID"));
				}
			}
		}

		public string Name 
		{
			get { return fName; }
			set
			{
				if (!value.Equals(fName))
					fName = value;
			}
		}

		public string First
		{
			get { return fFirst; }
			set
			{
				if (!value.Equals(fFirst))
					fFirst = value;
			}
		}

		public string Last
		{
			get { return fLast; }
			set
			{
				if (!value.Equals(fLast))
					fLast = value;
			}
		}

		public string Email
		{
			get { return fEmail; }
			set
			{
				if (!value.Equals(fEmail))
					fEmail = value;
			}
		}

		public bool CanPromote
		{
			get
			{
				return fCanPromote;
			}
			set
			{
				if (!value.Equals(fCanPromote))
					fCanPromote = value;
			}
		}

		public bool IsAuthenticated
		{
			get { return fIsAuthenticated; }
			set 
			{
				if (!value.Equals(fIsAuthenticated))
				{
					fIsAuthenticated = value;
					OnPropertyChanged(this, new PropertyChangedEventArgs("IsAuthenticated"));
				}
			}
		}
		#endregion

		#region Constructors
		public Person() { ;}

		public Person(Person person)
		{
			fID = person.ID;
			fName = person.Name;
			fFirst = person.First;
			fLast = person.Last;
			fEmail = person.Email;
			fIsAuthenticated = person.IsAuthenticated;
		}
		#endregion

		#region Public Virtual (Overridable) Methods
		public virtual T Promote<T>() where T : Person, new()
		{
			return Activator.CreateInstance(typeof(T), this) as T;
		}

		public override string ToString()
		{
			return string.Format("{0} [{1} {2}]", Name, First, Last);
		}
		#endregion
	}

	[Serializable]
	class AnonymousVisitor : Person
	{
		// Implement your own properties or methods for an anonymous user.
		private bool fCanAuthenticate;

		public bool CanAuthenticate
		{
			get { return fCanAuthenticate; }
			set { fCanAuthenticate = value; }
		}

		public AnonymousVisitor() { ;}

		public AnonymousVisitor(Person person) : base(person) { ;}
	}

	[Serializable]
	class EventMember : Person
	{
		// Implement your own properties or methods for an event member.
		public string Middle { get; set; }

		public EventMember() { ;}

		public EventMember(Person person) : base(person) { ;}

		public override string ToString()
		{
			return string.Format("{0} [{1} {2} {3}]", Name, First, Middle, Last);
		}
	}

	[Serializable]
	class StandardMember : Person
	{
		public string NickName { get; set; }

		public StandardMember() { ;}

		public StandardMember(Person person) : base(person) { ;}

		public override string ToString()
		{
			return string.Format("{0} [{1} {2} a.k.a {3}]", Name, First, Last, NickName);
		}
	}

	[Serializable]
	class Administrator : Person
	{
		public Administrator() { ;}

		public Administrator(Person person) : base(person) { ;}
	}

	[Serializable]
	class Actor : Person
	{
		public Actor() { ;}

		public Actor(Person person) : base(person) { ;}
	}

	class Program
	{
		private static AnonymousVisitor visitor = new AnonymousVisitor() { ID = Guid.NewGuid(), First = "Bruce", Last = "Banner", Email = "hulk@marvel.com", Name = "Hulk", CanAuthenticate = true, CanPromote = true };
		private static EventMember @event = new EventMember() { ID = Guid.NewGuid(), First = "John", Middle = "Quincy", Name = "Adams", Email = "jqa@us-presidents.org" };
		private static StandardMember standard = null;
		private static Administrator administrator = null;
		private static Actor actor = new Actor();

		static void Main(string[] args)
		{
			visitor.PropertyChanged += OnPropertyChanged;
			@event.PropertyChanged += OnPropertyChanged;

			Console.WriteLine("Let's work with the visitor first");
			Console.WriteLine("Visitor - {0} is authenticated? {1}", visitor, visitor.IsAuthenticated);
			Console.WriteLine("Standard Member - {0}", standard == null ? "No standard user authenticated" : string.Format("{0} is authenticated? {1}", standard, standard.IsAuthenticated));
			Console.WriteLine("Mock visitor authentication...");
			visitor.IsAuthenticated = true;
			Console.WriteLine("Standard Member - {0}", standard == null ? "No standard user authenticated" : string.Format("{0} is authenticated? {1}", standard, standard.IsAuthenticated));
			Console.WriteLine("We should now be able to work off of the standard user...");

			Console.WriteLine();
			Console.WriteLine("Now let's work with the event");
			Console.WriteLine("Event - {0} is authenticated? {1}", @event.ToString(), @event.IsAuthenticated);
			Console.WriteLine("Administrator - {0}", administrator == null ? "No administrator authenticated" : string.Format("{0} is authenticated? {1}", administrator, administrator.IsAuthenticated));
			Console.WriteLine("Mock event authentication...");
			@event.IsAuthenticated = true;
			Console.WriteLine("Administrator - {0}", administrator == null ? "No administrator authenticated" : string.Format("{0} is authenticated? {1}", administrator, administrator.IsAuthenticated));
			Console.WriteLine("Event is not an administrator, but let's now make it possible...");
			@event.IsAuthenticated = false;
			@event.CanPromote = true;
			@event.IsAuthenticated = true;
			Console.WriteLine("Administrator - {0}", administrator == null ? "No administrator authenticated" : string.Format("{0} is authenticated? {1}", administrator, administrator.IsAuthenticated));
			Console.WriteLine("We should now be able to work off of the administrator...");
			Console.ReadLine();
		}

		private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (sender is AnonymousVisitor)
			{
				AnonymousVisitor visitor = sender as AnonymousVisitor;
				if (e.PropertyName == "IsAuthenticated")
				{
					Console.WriteLine("Property IsAuthenticated has changed for {0}", visitor);
					if (visitor.IsAuthenticated && visitor.CanPromote)
						standard = visitor.Promote<StandardMember>();
					Console.WriteLine("Visitor - {0}; has been promoted to a standard user - {1}", visitor, standard != null);
				}
			}

			if (sender is EventMember)
			{
				EventMember @event = sender as EventMember;
				if (e.PropertyName == "IsAuthenticated")
				{
					Console.WriteLine("Property IsAuthenticated has changed for {0}", @event);
					if (@event.IsAuthenticated && @event.CanPromote)
						administrator = @event.Promote<Administrator>();
					Console.WriteLine("Event - {0}; has been promoted to an Administrator - {1}", @event, administrator != null);
				}
			}
		}
	}
}

Open in new window


Produces the following output -Capture.JPG
-saige-
0
 

Author Comment

by:riffrack
ID: 40493271
Hi saige

Thank you very much for your suggestion. The inheritance model you suggest is exactly what I was considering, however the AnonymousVisitor does not really share any properties with the member. We don't know the name, first, last, email or anything of the AnonymousVisitor. And it could be a bot, hence not a person. What would be adavantages of inheriting the person class?

riffrack
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 40494047
First.  You have the stipulation that an anonymous visitor can become an Authenticated user.  You also state that Anonymous users can make payments/donations without needing to Authenticate.  Ultimately, if having a few extra properties is really a problem, you can hide the base properties in the child class by explicitly defining them.  Here is an example implementation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace EE_Q28578061
{
	static class Globals
	{
		private static readonly List<Person> fUsers = new List<Person>();
		public static List<Person> Users { get { return fUsers; } }
	}

	static class HashHelper
	{
		public static int GetHashCode<T1, T2>(T1 arg1, T2 arg2)
		{
			return GetHashCode(new object[] { arg1, arg2 });
		}

		public static int GetHashCode<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3)
		{
			return GetHashCode(new object[] { arg1, arg2, arg3 });
		}

		public static int GetHashCode<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
		{
			return GetHashCode(new object[] { arg1, arg2, arg3, arg4 });
		}

		public static int GetHashCode(params object[] args)
		{
			unchecked
			{
				int hash = 0;
				foreach (var item in args)
					hash = 31 * hash + ((item != null) ? item.GetHashCode() : 0);
				return hash;
			}
		}
	}

	[Serializable]
	class Person : INotifyPropertyChanged
	{
		#region INotifyPropertyChanged Members
		[NonSerialized]
		private PropertyChangedEventHandler _propertyChanged;
		public event PropertyChangedEventHandler PropertyChanged
		{
			add { _propertyChanged += value; }
			remove { _propertyChanged -= value; }
		}

		protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			PropertyChangedEventHandler handler = _propertyChanged;
			if (handler != null)
				handler(sender, e);
		}
		#endregion

		#region Private Fields
		private Guid fID;
		private string fName;
		private string fFirst;
		private string fLast;
		private string fEmail;
		private bool fCanPromote = false;
		private bool fIsAuthenticated = false;
		#endregion

		#region Public Properties
		public Guid ID
		{
			get { return fID; }
			set 
			{
				if (!value.Equals(fID))
				{
					fID = value;
					OnPropertyChanged(this, new PropertyChangedEventArgs("ID"));
				}
			}
		}

		public string Name 
		{
			get { return fName; }
			set
			{
				if (!value.Equals(fName))
					fName = value;
			}
		}

		public string First
		{
			get { return fFirst; }
			set
			{
				if (!value.Equals(fFirst))
					fFirst = value;
			}
		}

		public string Last
		{
			get { return fLast; }
			set
			{
				if (!value.Equals(fLast))
					fLast = value;
			}
		}

		public string Email
		{
			get { return fEmail; }
			set
			{
				if (!value.Equals(fEmail))
					fEmail = value;
			}
		}

		public bool CanPromote
		{
			get
			{
				return fCanPromote;
			}
			set
			{
				if (!value.Equals(fCanPromote))
					fCanPromote = value;
			}
		}

		public bool IsAuthenticated
		{
			get { return fIsAuthenticated; }
			set 
			{
				if (!value.Equals(fIsAuthenticated))
				{
					fIsAuthenticated = value;
					OnPropertyChanged(this, new PropertyChangedEventArgs("IsAuthenticated"));
				}
			}
		}
		#endregion

		#region Constructors
		public Person() { ;}

		public Person(Person person)
		{
			fID = person.ID;
			fName = person.Name;
			fFirst = person.First;
			fLast = person.Last;
			fEmail = person.Email;
			fIsAuthenticated = person.IsAuthenticated;
		}
		#endregion

		#region Public Virtual (Overridable) Methods
		public virtual T Promote<T>() where T : Person, new()
		{
			return Activator.CreateInstance(typeof(T), this) as T;
		}

		public override string ToString()
		{
			return string.Format("{0} [{1} {2}]", Name, First, Last);
		}
		#endregion

		#region Overriden Methods
		public override bool Equals(object obj)
		{
			if (obj == null || (obj.GetType() != GetType()))
				return false;

			return this == (obj as Person);
		}

		public bool Equals(Person person)
		{
			if (object.ReferenceEquals(person, null))
				return false;

			return this == person;
		}

		public override int GetHashCode()
		{
			return HashHelper.GetHashCode(ID, Name, First, Last, Email, CanPromote, IsAuthenticated);
		}
		#endregion

		#region Operator Overloads
		public static bool operator ==(Person lhs, Person rhs)
		{
			if (object.ReferenceEquals(lhs, rhs))
				return true;

			if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null))
				return false;

			return (lhs.ID != null && rhs.ID != null ? lhs.ID.Equals(rhs.ID) : false) &&
				(lhs.Name != null && rhs.Name != null ? lhs.Name.Equals(rhs.Name) : false) &&
				(lhs.First != null && rhs.First != null ? lhs.First.Equals(rhs.First) : false) &&
				(lhs.Last != null && rhs.Last != null ? lhs.Last.Equals(rhs.Last) : false) &&
				(lhs.Email != null && rhs.Email != null ? lhs.Email.Equals(rhs.Email) : false) &&
				lhs.CanPromote.Equals(rhs.CanPromote) &&
				lhs.IsAuthenticated.Equals(rhs.IsAuthenticated);
		}

		public static bool operator !=(Person lhs, Person rhs)
		{
			return !(lhs == rhs);
		}
		#endregion
	}

	[Serializable]
	class AnonymousVisitor : Person
	{
		// Implement your own properties or methods for an anonymous user.
		private bool fCanAuthenticate;

		public bool CanAuthenticate
		{
			get { return fCanAuthenticate; }
			set { fCanAuthenticate = value; }
		}

		public string Name { get { return base.Name; } }
		public string First { get { return base.First; } }
		public string Last { get { return base.Last; } }
		private string Email { get { return base.Email; } }

		public AnonymousVisitor() 
		{
			base.Name = string.Format("Anonymous{0}", Globals.Users.Count());
			base.First = "Anonymous";
			base.Last = "Visitor";
		}

		public AnonymousVisitor(Person person) : base(person) 
		{
			base.Name = string.Format("Anonymous{0}", Globals.Users.Count());
			base.First = "Anonymous";
			base.Last = "Visitor";
		}

		#region Overriden Methods
		public override bool Equals(object obj)
		{
			if (obj == null || (obj.GetType() != GetType()))
				return false;

			return this == (obj as AnonymousVisitor);
		}

		public bool Equals(AnonymousVisitor person)
		{
			if (object.ReferenceEquals(person, null))
				return false;

			return this == person;
		}

		public override int GetHashCode()
		{
			return HashHelper.GetHashCode(base.GetHashCode(), CanAuthenticate);
		}
		#endregion

		#region Operator Overloads
		public static bool operator ==(AnonymousVisitor lhs, AnonymousVisitor rhs)
		{
			if (object.ReferenceEquals(lhs, rhs))
				return true;

			if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null))
				return false;

			return (Person)lhs == (Person)rhs && lhs.CanAuthenticate.Equals(rhs.CanAuthenticate);
		}

		public static bool operator !=(AnonymousVisitor lhs, AnonymousVisitor rhs)
		{
			return !(lhs == rhs);
		}
		#endregion
	}

	[Serializable]
	class EventMember : Person
	{
		// Implement your own properties or methods for an event member.
		public string Middle { get; set; }

		public EventMember() { ;}

		public EventMember(Person person) : base(person) { ;}

		public override string ToString()
		{
			return string.Format("{0} [{1} {2} {3}]", Name, First, Middle, Last);
		}

		#region Overriden Methods
		public override bool Equals(object obj)
		{
			if (obj == null || (obj.GetType() != GetType()))
				return false;

			return this == (obj as EventMember);
		}

		public bool Equals(EventMember person)
		{
			if (object.ReferenceEquals(person, null))
				return false;

			return this == person;
		}

		public override int GetHashCode()
		{
			return HashHelper.GetHashCode(base.GetHashCode(), Middle);
		}
		#endregion

		#region Operator Overloads
		public static bool operator ==(EventMember lhs, EventMember rhs)
		{
			if (object.ReferenceEquals(lhs, rhs))
				return true;

			if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null))
				return false;

			return (Person)lhs == (Person)rhs &&
				(lhs.Middle != null && rhs.Middle != null ? lhs.Middle.Equals(rhs.Middle) : false);
		}

		public static bool operator !=(EventMember lhs, EventMember rhs)
		{
			return !(lhs == rhs);
		}
		#endregion
	}

	[Serializable]
	class StandardMember : Person
	{
		public string NickName { get; set; }

		public StandardMember() { ;}

		public StandardMember(Person person) : base(person) { ;}

		public override string ToString()
		{
			return string.Format("{0} [{1} {2} a.k.a {3}]", Name, First, Last, NickName);
		}

		#region Overriden Methods
		public override bool Equals(object obj)
		{
			if (obj == null || (obj.GetType() != GetType()))
				return false;

			return this == (obj as StandardMember);
		}

		public bool Equals(StandardMember person)
		{
			if (object.ReferenceEquals(person, null))
				return false;

			return this == person;
		}

		public override int GetHashCode()
		{
			return HashHelper.GetHashCode(base.GetHashCode(), NickName);
		}
		#endregion

		#region Operator Overloads
		public static bool operator ==(StandardMember lhs, StandardMember rhs)
		{
			if (object.ReferenceEquals(lhs, rhs))
				return true;

			if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null))
				return false;

			return (Person)lhs == (Person)rhs &&
				(lhs.NickName != null && rhs.NickName != null ? lhs.NickName.Equals(rhs.NickName) : false);
		}

		public static bool operator !=(StandardMember lhs, StandardMember rhs)
		{
			return !(lhs == rhs);
		}
		#endregion
	}

	[Serializable]
	class Administrator : Person
	{
		public Administrator() { ;}

		public Administrator(Person person) : base(person) { ;}

		#region Overriden Methods
		public override bool Equals(object obj)
		{
			if (obj == null || (obj.GetType() != GetType()))
				return false;

			return this == (obj as Administrator);
		}

		public bool Equals(Administrator person)
		{
			if (object.ReferenceEquals(person, null))
				return false;

			return this == person;
		}

		public override int GetHashCode()
		{
			return HashHelper.GetHashCode(base.GetHashCode());
		}
		#endregion

		#region Operator Overloads
		public static bool operator ==(Administrator lhs, Administrator rhs)
		{
			if (object.ReferenceEquals(lhs, rhs))
				return true;

			if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null))
				return false;

			return (Person)lhs == (Person)rhs;
		}

		public static bool operator !=(Administrator lhs, Administrator rhs)
		{
			return !(lhs == rhs);
		}
		#endregion
	}

	[Serializable]
	class Actor : Person
	{
		public Actor() { ;}

		public Actor(Person person) : base(person) { ;}

		#region Overriden Methods
		public override bool Equals(object obj)
		{
			if (obj == null || (obj.GetType() != GetType()))
				return false;

			return this == (obj as Actor);
		}

		public bool Equals(Actor person)
		{
			if (object.ReferenceEquals(person, null))
				return false;

			return this == person;
		}

		public override int GetHashCode()
		{
			return HashHelper.GetHashCode(base.GetHashCode());
		}
		#endregion

		#region Operator Overloads
		public static bool operator ==(Actor lhs, Actor rhs)
		{
			if (object.ReferenceEquals(lhs, rhs))
				return true;

			if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null))
				return false;

			return (Person)lhs == (Person)rhs;
		}

		public static bool operator !=(Actor lhs, Actor rhs)
		{
			return !(lhs == rhs);
		}
		#endregion
	}

	class Program
	{
		static void Main(string[] args)
		{
			AddUsers();

			Console.WriteLine("Let's work with the anonymous user first");
			AnonymousVisitor visitor = (from user in Globals.Users where user.GetType() == typeof(AnonymousVisitor) select user).FirstOrDefault() as AnonymousVisitor;
			Console.WriteLine("Anonymous user - {0} is authenticated? {1}", visitor, visitor.IsAuthenticated);
			Console.WriteLine("Mock visitor authentication...");
			visitor.IsAuthenticated = true;
			Console.WriteLine("Anonymous user should now be a standard user...  Let's check, shall we.");
			foreach (StandardMember user in Globals.Users.Where(u => u.GetType() == typeof(StandardMember)))
			{
				Console.WriteLine("Standard user - {0}", user);
				user.Name = "HulkSmash";
				user.First = "Bruce";
				user.Last = "Banner";
				user.Email = "hulksmash@marvel.com";
				user.NickName = "Hulk";
				Console.WriteLine("Standard user - {0}", user);
			}

			Console.WriteLine();
			Console.WriteLine("Now let's work with the event");
			EventMember @event = (from user in Globals.Users where user.GetType() == typeof(EventMember) select user).FirstOrDefault() as EventMember;
			Console.WriteLine("Event member - {0} is authenticated? {1}", @event, @event.IsAuthenticated);
			Console.WriteLine("Mock event member authentication...");
			@event.IsAuthenticated = true;
			@event.IsAuthenticated = false;
			@event.CanPromote = true;
			@event.IsAuthenticated = true;
			Console.WriteLine("Event member should now be an Administrator...  Let's check, shall we.");
			foreach (Administrator user in Globals.Users.Where(u => u.GetType() == typeof(Administrator)))
				Console.WriteLine("Administrator - {0}", user);
			Console.ReadLine();
		}

		private static void AddUsers()
		{
			Person visitor = new AnonymousVisitor() { ID = Guid.NewGuid(), CanAuthenticate = true, CanPromote = true };
			visitor.PropertyChanged += OnPropertyChanged;
			Globals.Users.Add(visitor);

			Person @event = new EventMember() { ID = Guid.NewGuid(), Name = "SixthPrez", First = "John", Middle = "Quincy", Last = "Adams", Email = "jqa@us-presidents.org" };
			@event.PropertyChanged += OnPropertyChanged;
			Globals.Users.Add(@event);
		}

		private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (sender is AnonymousVisitor)
			{
				Person visitor = sender as AnonymousVisitor;
				if (visitor != null)
				{
					if (e.PropertyName == "IsAuthenticated")
					{
						Console.WriteLine("Property IsAuthenticated has changed for {0}", visitor);
						if (visitor.IsAuthenticated && visitor.CanPromote)
						{
							if (Globals.Users.Contains(visitor))
								Globals.Users[Globals.Users.IndexOf(visitor)] = visitor.Promote<StandardMember>();
							visitor = visitor.Promote<StandardMember>();
						}
						Console.WriteLine("Visitor - {0}; has been promoted to a standard user - {1}", visitor, visitor.GetType() == typeof(StandardMember));
					}
				}
			}

			if (sender is EventMember)
			{
				Person @event = sender as EventMember;
				if (@event != null)
				{
					if (e.PropertyName == "IsAuthenticated")
					{
						Console.WriteLine("Property IsAuthenticated has changed for {0}", @event);
						if (@event.IsAuthenticated && @event.CanPromote)
						{
							if (Globals.Users.Contains(@event))
								Globals.Users[Globals.Users.IndexOf(@event)] = @event.Promote<Administrator>();
							@event = @event.Promote<Administrator>();
						}
						Console.WriteLine("Event - {0}; has been promoted to an Administrator - {1}", @event, @event.GetType() == typeof(Administrator));
					}
				}
			}
		}
	}
}

Open in new window


Produces the following output -Capture.JPGUsing a base class in this manner allows for more flexibility when you need to move objects between various types.

The obvious benefits are being able to easily change the classification of one person object to the next by way of the promote method.  You could then set up an inheritance tree (so to speak) by way of attributes that allow for you to stipulate who can become what.

Another benefit that I allude to in my most recent implementation is the ability to blur the lines (so to speak) between an AnonymousVisitor vs a StandardMember vs an EventMember; etc. etc..  If you look, I created a single static list made up of Person objects.  I can then promote (and potentially demote) these objects without the need to move the person object from one list to another.

-saige-
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Finite Automata 10 40
Trouble with decrypting PGP encrypted file using PHP & GnuPG 4 391
WCAG audit tools 1 52
base64 decode encode 12 100
What is Waterfall Model? Waterfall model is the classic Software Development Life Cycle method practiced in software development process. As the name "waterfall" describes, this development is flowing downwards steadily like waterfall, i.e., procee…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now