public void Test_ObjectsChangeTracker_GetChanges_RegisterObject() { var tracker = new ObjectsChangeTracker(); var a = new A(); tracker.RegisterObject(a); a.F2 = 3; var changes = tracker.GetChanges(a); changes[0].Name.ShouldBe(nameof(A.F2)); tracker.RegisterObject(a); changes = tracker.GetChanges(a); changes.Length.ShouldBe(0); a.F1 = "new"; a.F2 = 13; a.F3 = false; for (var i = 0; i < 10; ++i) { tracker.GetChanges(a); } changes = tracker.GetChanges(a); changes.Select(c => c.Name).ShouldBe(new[] { "F1", "F2", "F3" }); changes = tracker.GetChanges(new A()); Assert.Null(changes); }
public void EntryUpdateStack() { var cc = _tracker.GetChanges(this); _oldValues = new Dictionary <string, object>(); foreach (var trackingMember in cc) { _oldValues.Add(trackingMember.name, trackingMember.value); } _tracker.RegisterObject(this); }
public void Test_ObjectsChangeTracker_GetChanges_two_objects() { var tracker = new ObjectsChangeTracker(); var original = new A { F1 = "F1old", F2 = 2, F3 = true }; var current = new A { F1 = "F1New", F2 = 2, F3 = false }; var changes = tracker.GetChanges(original, current); changes.Count().ShouldBe(2); changes[1].Name.ShouldBe("F3"); changes[1].CurrentValue.ShouldBe(false); changes[0].Name.ShouldBe("F1"); changes[0].CurrentValue.ShouldBe("F1New"); changes = tracker.GetChanges(original, null); Assert.Null(changes); changes = tracker.GetChanges(null, current); Assert.Null(changes); }
/// <summary> /// Builds the update command. /// </summary> /// <param name="cmd">The CMD.</param> /// <param name="obj">The obj.</param> /// <param name="tableName">Name of the table.</param> /// <param name="idFieldNames">The id field names.</param> /// <param name="includeFields">The include fields.</param> /// <param name="excludeFields">The exclude fields.</param> /// <param name="changeTracker">The change tracker.</param> /// <param name="dbSettings">The db settings.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public static bool BuildUpdateCommand( this DbCommand cmd, object obj, string tableName, IEnumerable <string> idFieldNames, IEnumerable <string> includeFields, IEnumerable <string> excludeFields, ObjectsChangeTracker changeTracker, DbSettings dbSettings) { if (idFieldNames == null) { idFieldNames = new string[0]; } idFieldNames = idFieldNames.Select(n => n.ToUpper()); if (changeTracker != null) { var changedFields = changeTracker.GetChanges(obj); if (changedFields != null) { if (includeFields == null) { includeFields = changedFields.Select(c => c.Name); } else { includeFields = includeFields.Intersect(changedFields.Select(c => c.Name)); } } } if (includeFields != null) { includeFields = includeFields.Concat(idFieldNames); } IMappingConfigurator config = new AddDbCommandsMappingConfig( dbSettings, includeFields, excludeFields, "updateop_inc_" + includeFields.ToCsv("_") + "_exc_" + excludeFields.ToCsv("_")); var mapper = Mapper.Default.GetMapper(obj.GetType(), typeof(DbCommand), config); var fields = mapper.StoredObjects.OfType <SrcReadOperation>().Select(m => m.Source.MemberInfo.Name) .Where(f => !idFieldNames.Contains(f)); if (!fields.Any()) { return(false); } var cmdStr = "UPDATE " + tableName + " SET " + fields.Select( f => dbSettings.GetEscapedName(f.ToUpper()) + "=" + dbSettings.GetParamName(f.ToUpper())).ToCsv(",") + " WHERE " + idFieldNames .Select(fn => dbSettings.GetEscapedName(fn) + "=" + dbSettings.GetParamName(fn)).ToCsv(" AND "); cmd.CommandText = cmdStr; cmd.CommandType = CommandType.Text; mapper.Map(obj, cmd, null); return(true); }