private void UpdateDocument(List <ImportItems> dictionary, IEnumerable <IValueTag> valueTags, IVortexObject prototype) { string id = dictionary.FirstOrDefault(p => p.Key == "_EntityId").Value; var existing = this.Repository.Queryable.Where(p => p._EntityId == id).FirstOrDefault(); if (existing != null) { ((dynamic)prototype).CopyPlainToShadow(existing); } ((dynamic)prototype)._EntityId.Shadow = id; if (existing != null) { ((dynamic)prototype).ChangeTracker.StartObservingChanges(); } // Swap values to shadow foreach (var item in dictionary) { if (!string.IsNullOrEmpty(item.Key)) { var tag = valueTags.FirstOrDefault(p => p.Symbol == item.Key); OnlinerBaseType type = tag as OnlinerBaseType; if (type is OnlinerString || type is OnlinerWString) { ((dynamic)tag).Shadow = (CastValue(type, item.Value) as string)?.Replace('►', ';'); } else { ((dynamic)tag).Shadow = CastValue(type, item.Value); } } } if (existing != null) { ((dynamic)prototype).ChangeTracker.StopObservingChanges(); } if (existing != null) { ((dynamic)prototype).ChangeTracker.Import(existing); ((dynamic)existing).CopyShadowToPlain((dynamic)prototype); this.Repository.Update(existing._EntityId, existing); } else { T newRecord = new T(); ((dynamic)prototype).ChangeTracker.Import(newRecord); ((dynamic)newRecord).CopyShadowToPlain((dynamic)prototype); this.Repository.Create(newRecord._EntityId, newRecord); } }
private dynamic CastValue(OnlinerBaseType type, string @value) { switch (type) { case OnlinerBool c: return(bool.Parse(@value)); case OnlinerByte c: return(byte.Parse(@value)); case OnlinerDate c: return(DateTime.Parse(@value)); case OnlinerDInt c: return(int.Parse(@value)); case OnlinerDWord c: return(uint.Parse(@value)); case OnlinerInt c: return(short.Parse(@value)); case OnlinerLInt c: return(long.Parse(@value)); case OnlinerLReal c: return(double.Parse(@value)); case OnlinerLTime c: return(TimeSpan.Parse(@value)); case OnlinerLWord c: return(ulong.Parse(@value)); case OnlinerReal c: return(float.Parse(@value)); case OnlinerSInt c: return(sbyte.Parse(@value)); case OnlinerString c: return(@value); case OnlinerTime c: return(TimeSpan.Parse(@value)); case OnlinerTimeOfDay c: return(TimeSpan.Parse(@value)); case OnlinerDateTime c: return(DateTime.Parse(@value)); case OnlinerUDInt c: return(uint.Parse(@value)); case OnlinerUInt c: return(ushort.Parse(@value)); case OnlinerULInt c: return(ulong.Parse(@value)); case OnlinerUSInt c: return(byte.Parse(@value)); case OnlinerWord c: return(ushort.Parse(@value)); case OnlinerWString c: return(@value); default: throw new Exception($"Unknown type {type.GetType()}"); } }