Skip to content

Instantly share code, notes, and snippets.

@gshutler
Forked from DominicFinn/Diff Generator
Created March 2, 2012 15:49
Show Gist options
  • Save gshutler/1959266 to your computer and use it in GitHub Desktop.
Save gshutler/1959266 to your computer and use it in GitHub Desktop.
Diffs
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