Last active
January 6, 2019 04:39
-
-
Save thaianhduc/068a967163955e13c87fbcbb8e1fface to your computer and use it in GitHub Desktop.
Improved version of TeacherCollection design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace DotConnect.LeakyAbstraction | |
{ | |
public interface IIndexedTeacherCollection | |
{ | |
int GetIndex(string teacherName); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace DotConnect.LeakyAbstraction | |
{ | |
public interface ITeacherCollection : IEnumerable<Teacher> | |
{ | |
ITeacherCollection WhereTeachMathematics(); | |
ITeacherCollection WhereExperienced(); | |
int Count{get;} | |
IIndexedTeacherCollection BuildIndex(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace DotConnect.LeakyAbstraction | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var school = new School | |
{ | |
Name = "Gotham City" | |
}; | |
school.Teachers.Add(new Teacher | |
{ | |
Name = "Batman", | |
Specialty = "Mathematics", | |
StartedOn = DateTime.Now.AddYears(-11), | |
IsStillAtWork = true | |
}); | |
school.Teachers.Add(new Teacher | |
{ | |
Name = "Joker", | |
Specialty = "Chemical", | |
StartedOn = DateTime.Now.AddYears(-6), | |
IsStillAtWork = false | |
}); | |
Console.WriteLine("Total teachers: {0}", school.TeacherCollection.Count); | |
Console.WriteLine("Mathematics teachers are: {0}", | |
string.Join("; ", school.TeacherCollection | |
.WhereTeachMathematics() | |
.Select(x => x.Name))); | |
Console.WriteLine("> 10 years teachers are: {0}", | |
string.Join("; ", school.TeacherCollection | |
.WhereExperienced() | |
.Select(x => x.Name))); | |
var batmanIndex = school.TeacherCollection.BuildIndex().GetIndex("Batman"); | |
Console.WriteLine($"Hey Batman is at {batmanIndex}"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace DotConnect.LeakyAbstraction | |
{ | |
public class TeacherCollection : ITeacherCollection, IIndexedTeacherCollection | |
{ | |
private readonly IList<Teacher> _teachers = new List<Teacher>(); | |
public TeacherCollection(IEnumerable<Teacher> teachers) | |
{ | |
if (teachers != null) | |
_teachers = teachers.Where(x => x.IsStillAtWork).ToList(); | |
} | |
public ITeacherCollection WhereTeachMathematics() | |
{ | |
return new TeacherCollection(_teachers.Where(x => x.Specialty == "Mathematics")); | |
} | |
public ITeacherCollection 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; | |
} | |
public IIndexedTeacherCollection BuildIndex() | |
{ | |
return new TeacherCollection(_teachers.OrderBy(x => x.StartedOn)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment