public void UpdateCustomer() { ObjectMapperManager objMan = new ObjectMapperManager(); Guid guid = Guid.NewGuid(); using (var ts = new TransactionScope()) using (var connection = CreateConnection()) { var customer = DBTools.ExecuteReader( connection, "select top 1 * from [dbo].[Customers]", null, r => r.ToObject <Customer>() ); var tracker = new ObjectsChangeTracker(); tracker.RegisterObject(customer); customer.Address = guid.ToString(); DBTools.UpdateObject( connection, customer, "Customers", new[] { "CustomerID" }, tracker, DbSettings.MSSQL ); } }
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); }
// [Fact] /// <summary> /// Updates the customer. /// </summary> public void UpdateCustomer() { var objMan = new Mapper(); var guid = Guid.NewGuid(); // todo: there is a bug , In the callstack of DBTools and DataReaderToObjectMapper occur two times Reader.Read(); so.. using var ts = new TransactionScope(); using var connection = CreateConnection(); var customer = DbTools.ExecuteReader( connection, "select * from Customers limit 1 ", null, r => r.ToObject <Customer>()); customer.ShouldNotBeNull(); var tracker = new ObjectsChangeTracker(); tracker.RegisterObject(customer); customer.Address = guid.ToString(); var result = DbTools.UpdateObject( connection, customer, "Customers", new[] { "CustomerID" }, tracker, DbSettings.Mssql); result.Result.ShouldBe(1); }
/// <summary> /// Reads the single. /// </summary> /// <param name="reader">The reader.</param> /// <param name="changeTracker">The change tracker.</param> /// <returns>`0.</returns> public TEntity ReadSingle(IDataReader reader, ObjectsChangeTracker changeTracker) { var result = MapUsingState(reader, reader); changeTracker?.RegisterObject(result); return(result); }
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); }
public void UpdateCustomer() { ObjectMapperManager objMan = new ObjectMapperManager(); Guid guid = Guid.NewGuid(); // todo: there is a bug , In the callstack of DBTools and DataReaderToObjectMapper ocur two times Reader.Read(); so.. using (var ts = new TransactionScope()) using (var connection = CreateConnection()) { var customer = DBTools.ExecuteReader( connection, "select * from Customers limit 1 ", null, r => r.ToObject <Customer>() ); Assert.IsNotNull(customer); var tracker = new ObjectsChangeTracker(); tracker.RegisterObject(customer); customer.Address = guid.ToString(); var result = DBTools.UpdateObject( connection, customer, "Customers", new[] { "CustomerID" }, tracker, DbSettings.MSSQL ); Assert.IsTrue(result.Result == 1); } }
/// <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); }
protected BaseEntity() { _tracker = new ObjectsChangeTracker(); _tracker.RegisterObject(this); }