Last active
December 24, 2016 10:38
-
-
Save Hinidu/e6c4b4e4546f86fd9bec6dec16b45c43 to your computer and use it in GitHub Desktop.
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; | |
using System.Linq; | |
using NUnit.Framework; | |
namespace DynamicData.Tests.External | |
{ | |
class Student | |
{ | |
public int Id { get; } | |
public string Name { get;} | |
public Student(int id, string name) | |
{ | |
Id = id; | |
Name = name; | |
} | |
} | |
class Class | |
{ | |
public int Id { get; private set; } | |
public string Name { get; private set; } | |
public IList<int> StudentIds { get; private set; } | |
public Class(int id, string name, IList<int> studentIds) | |
{ | |
Id = id; | |
Name = name; | |
StudentIds = studentIds; | |
} | |
} | |
class Grade | |
{ | |
public int StudentId { get; } | |
public int ClassId { get; } | |
public int Value { get;} | |
public Grade(int studentId, int classId, int value) | |
{ | |
StudentId = studentId; | |
ClassId = classId; | |
Value = value; | |
} | |
} | |
class StudentSummary | |
{ | |
public Class[] Classes { get; } | |
public Student Student { get; } | |
public int[] Grades { get; private set; } | |
public StudentSummary(Student student, IEnumerable<Class> classes, int[] grades) | |
{ | |
Student = student; | |
Classes = classes.ToArray(); | |
Grades = grades; | |
} | |
} | |
[TestFixture] | |
public class StudentTest | |
{ | |
[Test] | |
public void Test() | |
{ | |
var students = new SourceCache<Student, int>(student => student.Id); | |
var classes = new SourceCache<Class, int>(@class => @class.Id); | |
var grades = new SourceList<Grade>(); | |
var alice = new Student(1, "Alice"); | |
students.AddOrUpdate(alice); | |
var bob = new Student(2, "Bob"); | |
students.AddOrUpdate(bob); | |
var chloe = new Student(3, "Chloe"); | |
students.AddOrUpdate(chloe); | |
var math = new Class(1, "Math", new List<int> { alice.Id }); | |
classes.AddOrUpdate(math); | |
var biology = new Class(2, "Biology", new List<int> { alice.Id, bob.Id }); | |
classes.AddOrUpdate(biology); | |
var algorithms = new Class(3, "Algorithms", new List<int> { bob.Id }); | |
classes.AddOrUpdate(algorithms); | |
grades.Add(new Grade(alice.Id, math.Id, 5)); | |
grades.Add(new Grade(bob.Id, biology.Id, 4)); | |
grades.Add(new Grade(bob.Id, algorithms.Id, 2)); | |
var studentsByClass = classes.Connect() | |
.TransformMany(@class => @class.StudentIds.Select(studentId => (studentId: studentId, @class: @class)), x => x); | |
var studentsSummary = students.Connect() | |
.LeftJoinMany( | |
studentsByClass, | |
swc => swc.studentId, | |
(studentId, student, grouping) => new StudentSummary( | |
student, grouping.Items.Select(swc => swc.@class), new int[0])) | |
.AsObservableCache(); | |
CollectionAssert.IsEmpty(studentsSummary.Lookup(chloe.Id).Value.Classes); | |
CollectionAssert.AreEquivalent(new[] { math, biology }, studentsSummary.Lookup(alice.Id).Value.Classes); | |
algorithms.StudentIds.Add(alice.Id); | |
classes.AddOrUpdate(algorithms); | |
CollectionAssert.AreEquivalent(new[] { math, biology, algorithms }, studentsSummary.Lookup(alice.Id).Value.Classes); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment