-
-
Save gshutler/1959266 to your computer and use it in GitHub Desktop.
Diffs
This file contains hidden or 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
public static class DiffGenerator | |
{ | |
public Diff<T> Generate<T>(IEnumerable<T> left, IEnumerable<T> right) | |
{ | |
var added = right.Except(left).ToList(); | |
var removed = left.Except(right).ToList(); | |
var leftList = left.ToList(); | |
var common = right.Where(item => leftList.Contains(item)).ToList(); | |
return new Diff(added, removed, common); | |
} | |
} | |
public sealed class Diff<T> | |
{ | |
public IEnumerable<T> Added { get; private set; } | |
public IEnumerable<T> Removed { get; private set; } | |
public IEnumerable<T> Common { get; private set; } | |
internal Diff(IEnumerable<T> added, IEnumerable<T> removed, IEnumerable<T> common) | |
{ | |
Added = added; | |
Removed = removed; | |
Common = common; | |
} | |
} | |
public class Something | |
{ | |
public void Update(string updatedName, IEnumerable<Guid> updatedCountries) | |
{ | |
var countriesDiff = DiffGenerator.Generate(this.countries, updatedCountries); | |
var regionNameChanged = updatedName != this.name; | |
this.Apply(new RegionUpdated(updatedName, regionNameChanged, countriesDiff)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment