Click here to Skip to main content
15,896,063 members
Articles / Web Development / ASP.NET

Using Internal Interfaces While Preserving Encapsulation

Rate me:
Please Sign up or sign in to vote.
4.80/5 (10 votes)
28 Oct 2009CPOL10 min read 41.3K   206   39  
Discusses use of interfaces to recover encapsulation where the internal keyword is used
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using CollegeCourseClient;
using DataAccessClient;

namespace CollegeCourseImpl.Impl1
{
    // Toy Implementation number 1
    //
    // This implementation is a straightforward implementation of the
    // interfaces
    internal class CollegeCourse : ICollegeCourse, IDataAccessService
    {

        #region ServiceImplementation

        // These are used for implementing the service interface
        private long auid = 0L;
        private DateTime? created = null;
        private DateTime? modified = null;
        private bool isDirty = false;

        public long AUID
        {
            get { return auid; }
            set { auid = value; }
        }

        public DateTime? Modified
        {
            get { return modified; }
            set { modified = value; }
        }

        public DateTime? Created
        {
            get { return created; }
            set { created = value; }
        }

        public bool IsDirty
        {
            get { return isDirty; }
            set { isDirty = value; }
        }

        #endregion

        #region BusinessImplementation

        // These are used for implementing the business interface
        internal string courseName;
        internal string courseNumber;
        internal string term;
        internal string courseDescription;
        internal string instructor;
        internal int creditHours = 0;
        internal List<string> enrolledStudents = new List<string>();

        public string CourseName
        {
            get { return courseName; }
            set
            {
                courseName = value;
                isDirty = true;
            }
        }

        public string CourseNumber
        {
            get { return courseNumber; }
            set
            {
                courseNumber = value;
                isDirty = true;
            }
        }

        public string Term
        {
            get { return term; }
            set
            {
                term = value;
                isDirty = true;
            }
        }

        public string Instructor
        {
            get { return instructor; }
            set
            {
                instructor = value;
                isDirty = true;
            }
        }

        public string CourseDescription
        {
            get { return courseDescription; }
            set
            {
                courseDescription = value;
                isDirty = true;
            }
        }

        public int CreditHours
        {
            get { return creditHours; }
            set
            {
                creditHours = value;
                isDirty = true;
            }
        }

        public string[] GetEnrolledStudents()
        {
            return enrolledStudents.ToArray();
        }

        public void EnrollStudent(string student)
        {
            if (enrolledStudents.Contains(student)) return;
            enrolledStudents.Add(student);
            isDirty = true;
        }

        public void UnEnrollStudent(string student)
        {
            if (!enrolledStudents.Contains(student)) return;
            enrolledStudents.Remove(student);
            isDirty = true;
        }
        #endregion

        #region ToyStringRepresentation

        // These are used for building the toy string representation

        private static readonly string MajorFieldSeparator = "--";
        private static readonly string MinorFieldSeparator = "++";

        public string ToStringRep()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(courseNumber!=null?courseNumber:"")
                .Append(MajorFieldSeparator);
            sb.Append(courseDescription != null ? courseDescription : "")
                .Append(MajorFieldSeparator);
            sb.Append(term != null ? term : "")
                .Append(MajorFieldSeparator);
            sb.Append(courseName != null ? courseName : "")
                .Append(MajorFieldSeparator);
            sb.Append(instructor != null ? instructor : "")
                .Append(MajorFieldSeparator);
            sb.Append(creditHours).Append(MajorFieldSeparator);
            if (enrolledStudents.Count > 0)
            {
                foreach (string student in enrolledStudents)
                {
                    sb.Append(student).Append(MinorFieldSeparator);
                }
                sb.Remove(sb.Length - MinorFieldSeparator.Length,
                    MinorFieldSeparator.Length);
            }
            else
            {
                sb.Remove(sb.Length - MajorFieldSeparator.Length,
                    MajorFieldSeparator.Length);
            }
            return sb.ToString();
        }

        public override string ToString()
        {
            return ToStringRep();
        }

        public void FromStringRep(string nsb)
        {
            string[] fields =
                nsb.Split(
                    new string[] { MajorFieldSeparator },
                                   StringSplitOptions.None);
            if (fields.Length < 6)
            {
                throw new Exception("Unable to parse object from string.");
            }
            courseNumber = fields[0];
            courseDescription = fields[1];
            term = fields[2];
            courseName = fields[3];
            instructor = fields[4];
            creditHours = int.Parse(fields[5]);
            enrolledStudents.Clear();
            if (fields.Length > 6)
            {
                string[] students =
                    fields[6].Split(new string[] { MinorFieldSeparator },
                                                  StringSplitOptions.None);
                enrolledStudents.AddRange(students);
            }
        }

        #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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions