Skip to content

Instantly share code, notes, and snippets.

@Deneas
Last active June 8, 2021 20:40
Show Gist options
  • Select an option

  • Save Deneas/d5b89f2047dd360b47de8436f49dc620 to your computer and use it in GitHub Desktop.

Select an option

Save Deneas/d5b89f2047dd360b47de8436f49dc620 to your computer and use it in GitHub Desktop.

Revisions

  1. Deneas revised this gist Jun 8, 2021. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions ScoreboardAfter.cs
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,7 @@ public void AddScore(Student student, int score)
    Scores.Add(new StudentScore(student, score));
    return;
    }
    // We cannot kill the mutation FirstOrDefault, because it actually is not valid.
    // The previous if statement ensures that the score exists, thus we will never get null.

    var existingScore = Scores.First(_ => _.Student.Equals(student));
    existingScore.AddScore(score);
    }
  2. Deneas created this gist Jun 6, 2021.
    24 changes: 24 additions & 0 deletions ScoreboardAfter.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // Licensed under the MIT License
    public class Scoreboard
    {
    public readonly List<StudentScore> Scores = new List<StudentScore>();
    // inspired by AddItem method in https://github.com/dotnet-architecture/eShopOnWeb/blob/master/src/ApplicationCore/Entities/BasketAggregate/Basket.cs
    public void AddScore(Student student, int score)
    {
    if (!Scores.Any(_ => _.Student.Equals(student)))
    {
    Scores.Add(new StudentScore(student, score));
    return;
    }
    // We cannot kill the mutation FirstOrDefault, because it actually is not valid.
    // The previous if statement ensures that the score exists, thus we will never get null.
    var existingScore = Scores.First(_ => _.Student.Equals(student));
    existingScore.AddScore(score);
    }
    public int GetScore(Student student)
    {
    return Scores.Where(_ => _.Student.Equals(student))
    .Select(_ => _.Score)
    .FirstOrDefault();
    }
    }
    22 changes: 22 additions & 0 deletions ScoreboardBefore.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // Licensed under the MIT License
    public class Scoreboard
    {
    public readonly List<StudentScore> Scores = new List<StudentScore>();
    // inspired by AddItem method in https://github.com/dotnet-architecture/eShopOnWeb/blob/master/src/ApplicationCore/Entities/BasketAggregate/Basket.cs
    public void AddScore(Student student, int score)
    {
    if (!Scores.Any(_ => _.Student.Equals(student)))
    {
    Scores.Add(new StudentScore(student, score));
    return;
    }
    var existingScore = Scores.FirstOrDefault(_ => _.Student.Equals(student));
    existingScore.AddScore(score);
    }
    public int GetScore(Student student)
    {
    return Scores.Where(_ => _.Student.Equals(student))
    .Select(_ => _.Score)
    .FirstOrDefault();
    }
    }
    28 changes: 28 additions & 0 deletions StudentScoringTest.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    // Licensed under the MIT License
    public class StudentScoringTest
    {
    [Fact]
    public void TwoScores_AreAddedUp()
    {
    var student = new Student(18, true);
    var firstScore = 5;
    var secondScore = 8;
    var expectedScore = 13;
    var scoreboard = new Scoreboard();

    scoreboard.AddScore(student, firstScore);
    scoreboard.AddScore(student, secondScore);
    var finalScore = scoreboard.GetScore(student);
    Assert.Equal(expectedScore, finalScore);
    }
    [Fact]
    public void NoScores_Zero()
    {
    var student = new Student(18, true);
    var expectedScore = 0;
    var scoreboard = new Scoreboard();

    var finalScore = scoreboard.GetScore(student);
    Assert.Equal(expectedScore, finalScore);
    }
    }