private void ModelVMSynchro <TModel, TVM>(List <TModel> lstModels, ObservableCollection <TVM> collectionVM, Func <TModel, TVM> createVMElement) { //number of lines changed that means //delete or insert occured if (lstModels.Count != collectionVM.Count) { collectionVM.Clear(); lstModels.ForEach(modelElement => { collectionVM.Add(createVMElement.Invoke(modelElement)); } ); } else { //note: at this point assume amount of elements in lstModels is //equal of collectionVM for (int i = 0; i < lstModels.Count; i++) { List <PropertyInfo> sourceProperties = lstModels[i].GetType().GetProperties().ToList(); List <PropertyInfo> destProperties = collectionVM[i].GetType().GetProperties().ToList(); if (sourceProperties.Count != destProperties.Count) { throw new ApplicationException("ModelVMSyncro. sourceProperties.Count != destProperties.Count"); } sourceProperties.ForEach (srcProp => { bool bfound = false; destProperties.ForEach (dstProp => { if (srcProp.Name == dstProp.Name) { bfound = true; object dstEl = dstProp.GetValue(collectionVM[i], null); object srcEl = srcProp.GetValue(lstModels[i], null); if (!CUtilReflex.IsEqualValues(srcEl, dstEl)) { dstProp.SetValue(collectionVM[i], srcEl, null); } } } ); if (!bfound) { Error("ModelVMSyncro. " + srcProp.Name + "not found"); } } ); } } }
private void CheckUpdateOrAdd(List <TModelElement> inpList) { foreach (var modelElement in inpList) { bool bFoundEqKeys = false; for (int i = 0; i < _collectionVm.Count; i++) { List <PropertyInfo> sourceProperties = modelElement.GetType().GetProperties().ToList(); List <PropertyInfo> destProperties = _collectionVm[i].GetType().GetProperties().ToList(); int cntEqKeysFound = 0; //bool bNotEqKeyFnd = false; //compare properties of two current elements foreach (var srcPropInfo in sourceProperties) { foreach (var dstPropInfo in destProperties) { if (_lstKeys.Contains(srcPropInfo.Name) && srcPropInfo.Name == dstPropInfo.Name) { if (CUtilReflex.IsEqualValues(srcPropInfo.GetValue(modelElement, null), dstPropInfo.GetValue(_collectionVm[i], null))) { cntEqKeysFound++; //all of keys are matched if (cntEqKeysFound == _lstKeys.Count) { _collectionVm[i] = _createVMElement(modelElement); //keys found //no need to iterate more properties bFoundEqKeys = true; break; } } } } //keys found no need to iterate more if (bFoundEqKeys) { break; } } } //we iterated all _lstModelElements and not found element from source //so we need to add element to _lstModelElemets if (!bFoundEqKeys) { _collectionVm.Add(_createVMElement.Invoke((modelElement))); } } }
public static void Test() { int i1 = 2; int i2 = 2; Assert.IsTrue(CUtilReflex.IsEqualValues((object)i1, (object)i2)); i1 = 2; i2 = 3; Assert.IsFalse(CUtilReflex.IsEqualValues((object)i1, (object)i2)); long l1 = 2; long l2 = 2; Assert.IsTrue(CUtilReflex.IsEqualValues((object)l1, (object)l2)); l1 = 2; l2 = 3; Assert.IsFalse(CUtilReflex.IsEqualValues((object)l1, (object)l2)); decimal d1 = 2.01m; decimal d2 = 2.01m; Assert.IsTrue(CUtilReflex.IsEqualValues((object)d1, (object)d2)); d1 = 2.01m; d2 = 2.02m; Assert.IsFalse(CUtilReflex.IsEqualValues((object)d1, (object)d2)); double dbl1 = 2.01; double dbl2 = 2.01; Assert.IsTrue(CUtilReflex.IsEqualValues((object)dbl1, (object)dbl2)); dbl1 = 2.01; dbl2 = 2.02; Assert.IsFalse(CUtilReflex.IsEqualValues((object)dbl1, (object)dbl2)); string st1 = "ab"; string st2 = "ab"; Assert.IsTrue(CUtilReflex.IsEqualValues((object)st1, (object)st2)); st1 = "ab"; st2 = "aB"; Assert.IsFalse(CUtilReflex.IsEqualValues((object)st1, (object)st2)); }
private void CheckDelete(int stockExchId, List <TModelElement> inpList) { if (stockExchId == 0) { _client.Error("CBaseDataSyncher. CheckDelete. StockExchId == 0"); } //Enumerate all elements in collectionVM. If element is not // exist in inpList (for the same StockExchId) do delete it from collectionVm for (int i = 0; i < _collectionVm.Count; i++) { List <PropertyInfo> destProperties = _collectionVm[i].GetType().GetProperties().ToList(); PropertyInfo dstPrpInf = destProperties.Find(dstPrp => dstPrp.Name == KeysDependenciesTrdMgr.StockExchId); if ((int)dstPrpInf.GetValue(_collectionVm[i], null) != stockExchId) { continue; } bool bFoundEqKeys = false; foreach (var modelElement in inpList) { List <PropertyInfo> sourceProperties = modelElement.GetType().GetProperties().ToList(); int cntEqKeysFound = 0; //bool bNotEqKeyFnd = false; //compare properties of two current elements foreach (var srcPropInfo in sourceProperties) { foreach (var dstPropInfo in destProperties) { if (_lstKeys.Contains(srcPropInfo.Name) && srcPropInfo.Name == dstPropInfo.Name) { if (CUtilReflex.IsEqualValues(srcPropInfo.GetValue(modelElement, null), dstPropInfo.GetValue(_collectionVm[i], null))) { cntEqKeysFound++; //all of keys are matched if (cntEqKeysFound == _lstKeys.Count) { //keys found //no need to iterate more properties bFoundEqKeys = true; } } } } //keys found no need to iterate more if (bFoundEqKeys) { break; } } } //we iterated all _lstModelElements and not found element from source //so we need to add element to _lstModelElemets if (!bFoundEqKeys) { _collectionVm.RemoveAt(i); } } }