Skip to content

UPDATE Snapshot

sdrapkin edited this page May 1, 2018 · 8 revisions

You can create a snapshot of any .NET object, and compare/diff multiple snapshots. Snapshots capture values of all public properties. Snapshot comparison produces a Predicate<string> which returns either true (a property name has different snapshot values) or false (a property name has matching snapshot values).

The diff'ed snapshot predicate will also be reference-equal to Snapshot.NoDifference predicate when both snapshots are identical (matching property names with matching values).

The diff'ed snapshot predicate can be passed into QB.Update(...Predicate<string> propFilter = null) to update only those properties which have been changed between snapshots and for which propFilter returns true.

var p = new Person()
{
    Id = new Guid("cf9fad7a-9775-28b9-7693-11e6ea3b1484"),
    Name = "John",
    BirthDate = new DateTime(1975,03,17)
};
p.Dump(); // shows nicely in LinqPad - comment out otherwise
// [Person]
//  Name      : "John"
//  Id        :  cf9fad7a-9775-28b9-7693-11e6ea3b1484
//  BirthDate :  1975-03-17 00:00:00

var snap1 = Snapshot.Create(p);
p.Name = "Daniel"; // make a change
var snap2 = Snapshot.Create(p);
	
var query = QB.Update(p, propFilter: snap2.Diff(snap1));
query.Dump(); // shows nicely in LinqPad - comment out otherwise

// [QueryInfo]
// [SQL] : "UPDATE [Person] SET [Name]=@@Name WHERE Id=@w@Id"
// [ParameterMap] : [Key : Value]
//   "@@Name"      : "John"
//   "@@w@Id"      : cf9fad7a-9775-28b9-7693-11e6ea3b1484

await db.QueryAsync(query); // executing the built query
Clone this wiki locally