Click here to Skip to main content
15,888,521 members
Articles / Programming Languages / C#

Coding in Tiers - Part II

Rate me:
Please Sign up or sign in to vote.
4.67/5 (19 votes)
24 Sep 20037 min read 82.4K   1.8K   83  
Reuse code in Windows and Web forms
using System;
using System.Collections;

using Dao;
using Services;
using Utility;

namespace Lib
{
	#region -- AccountManager --
	/// <summary>
	/// A <code>class</code> that provides account registration and authentication
	/// services.
	/// </summary>
	/// <remarks>
	/// This class resides in the Business tier and provides implementation of
	/// the account related services offered by the application. This class is
	/// used by the UI via the services offered through the Service tier.
	/// </remarks>
	public sealed class AccountManager : IRegistrationService, IAuthenticationService
	{
		#region -- Constructors --
		/// <summary>
		/// Constructor used by the Service tier
		/// </summary>
		internal AccountManager() {}
		#endregion

		#region -- Methods --
		/// <summary>
		/// Register a new user into the system
		/// </summary>
		/// <remarks>
		/// The Username and Password on the account should not be <code>null</code>
		/// or empty faliure on which the corresponding errors will be reported.
		/// </remarks>
		/// <param name="registration">The registration information</param>
		/// <exception cref="ValidationException">If the Username and Password on the account is
		/// <code>null</code> or empty</exception>
		public void Authenticate(IAccount acct)
		{
			// Place holder for error information
			Hashtable errors = new Hashtable(2);

			// Check if username is valid
			if(!ValidationHelper.ValidateRequired(acct.Username))
			{
				// Store the error message
				errors["Username"] = "Username is required.";
			}

			// Check if password is valid
			if(!ValidationHelper.ValidateRequired(acct.Password))
			{
				// Store the error message
				errors["Password"] = "Password is required.";
			}

			// Additional checks to verify if user exists and passwords match etc.

			if(errors.Count > 0)
			{
				// Throw exception for errors to be reported
				throw new ValidationException("Validation Error.", errors);
			}
		}

		/// <summary>
		/// Register a new user into the system
		/// </summary>
		/// <remarks>
		/// The Username and Password on the account should not be <code>null</code>
		/// or empty faliure on which the corresponding errors will be reported. The
		/// username on the registration information is checked for duplication. If
		/// the username does not currently exist in the database, the account is
		/// created and registration is successful. If the username already exists,
		/// an error message is reported back to the caller.
		/// </remarks>
		/// <param name="registration">The registration information</param>
		/// <exception cref="ValidationException">If an error occurs during processing.</exception>
		public void Register(IRegistration registration)
		{
			// Place holder for error information
			Hashtable errors = new Hashtable(3);

			// Check if username is valid
			if(!ValidationHelper.ValidateRequired(registration.Username))
			{
				// Store the error message
				errors["Username"] = "Username is required.";
			}

			// Check if password is valid
			if(!ValidationHelper.ValidateRequired(registration.Password))
			{
				// Store the error message
				errors["Password"] = "Password is required.";
			}

			// Create the Data Access Object
			RegistrationDAO dao = new RegistrationDAO();

			// Check if an account exists for the username supplied
			if(dao.AccountExists(registration.Username))
			{
				// Store the error message
				errors["Username"] = "An account with the username entered already exists.";
			}

			// Check if there are any errors
			if(errors.Count > 0)
			{
				// Throw exception for errors to be reported
				throw new ValidationException("Validation Error.", errors);
			}

			// Register the user
			dao.Register(registration);
		}
		#endregion
	}
	#endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Web Developer
United States United States
Over 15 years of experience in designing and architecting high availability/scalability financial application software using OO technologies such as C++/Java/C#.

Comments and Discussions