Skip to content

Instantly share code, notes, and snippets.

@thaianhduc
Last active January 6, 2019 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thaianhduc/b5a3a05d37affce4c540c8bc837b1fa0 to your computer and use it in GitHub Desktop.
Save thaianhduc/b5a3a05d37affce4c540c8bc837b1fa0 to your computer and use it in GitHub Desktop.
A teacher collection which wraps the logic of operations on teachers. To support getting the index, the GetIndex method is introduced and the collection is ordered by the creation date by default on the constructor. This demonstrates a design issue.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace DotConnect.LeakyAbstraction
{
public class TeacherCollection : IEnumerable<Teacher>
{
private readonly IList<Teacher> _teachers = new List<Teacher>();
public TeacherCollection(IEnumerable<Teacher> teachers)
{
if (teachers != null)
_teachers = teachers.Where(x => x.IsStillAtWork)
.OrderBy(x => x.StartedOn)
.ToList();
}
public TeacherCollection WhereTeachMathematics()
{
return new TeacherCollection(_teachers.Where(x => x.Specialty == "Mathematics"));
}
public TeacherCollection WhereExperienced()
{
return new TeacherCollection(_teachers.Where(x => DateTime.Now >= x.StartedOn.AddYears(10)));
}
public IEnumerator<Teacher> GetEnumerator()
{
return _teachers.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _teachers.GetEnumerator();
}
public int Count
{
get { return _teachers.Count; }
}
public int GetIndex(string teacherName)
{
for(var index = 0; index < _teachers.Count; index ++)
{
if(_teachers[index].Name == teacherName)
{
return index;
}
}
return -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment