/// <summary> /// Insures that a temporary pk is set if necessary /// </summary> /// <param name="aspect"></param> internal void UpdatePkIfNeeded(EntityAspect aspect) { if (KeyGenerator == null) { return; } var keyProperties = aspect.EntityType.KeyProperties; foreach (var aProperty in keyProperties) { var val = aspect.GetValue(aProperty.Name); var aUniqueId = new UniqueId(aProperty, val); // determine if a temp pk is needed. if (aProperty.IsAutoIncrementing) { if (!KeyGenerator.IsTempId(aUniqueId)) { // generate an id if it wasn't already generated aUniqueId = GenerateId(aspect.Entity, aProperty); } AddToTempIds(aUniqueId); } else if (aProperty.DefaultValue == val) { // do not call GenerateId unless the developer is explicit or the key is autoincrementing. } else { // this occurs if GenerateId was called before Attach - it won't have been added to tempIds in this case. if (KeyGenerator.IsTempId(aUniqueId)) { AddToTempIds(aUniqueId); } } } }
internal void MarkTempIdAsMapped(EntityAspect aspect, bool isMapped) { var keyProperties = aspect.EntityType.KeyProperties; foreach (var aProperty in keyProperties) { UniqueId aUniqueId = new UniqueId(aProperty, aspect.GetValue(aProperty.Name)); if (isMapped) { TempIds.Remove(aUniqueId); } else { if (KeyGenerator == null) { return; } if (KeyGenerator.IsTempId(aUniqueId)) { TempIds.Add(aUniqueId); } } } }
protected T GetValue <T>([CallerMemberName] string propertyName = "") { return(EntityAspect.GetValue <T>(propertyName)); }
/// <summary> /// Parse Object -- populating needed backingStore(s) /// </summary> private void ParseObject(NodeContext nodeContext, EntityAspect targetAspect) { // backingStore is a dict(?) that stores the properties as Json objects for the entity -- if its null, that means we aren't allowed to overwrite // backingStore will be null if not allowed to overwrite the entity. var backingStore = (targetAspect == null) ? null : targetAspect.BackingStore; var dict = (IDictionary <String, JToken>)nodeContext.Node; var structuralType = nodeContext.StructuralType; // needs to be the current namingConvention var nc = _mappingContext.EntityManager.MetadataStore.NamingConvention; dict.ForEach(kvp => { var key = nc.ServerPropertyNameToClient(kvp.Key, structuralType); var prop = structuralType.GetProperty(key); if (prop != null) { if (prop.IsDataProperty) { if (backingStore != null) { var dp = (DataProperty)prop; if (dp.IsComplexProperty) { var newCo = (IComplexObject)kvp.Value.ToObject(dp.ClrType); var co = (IComplexObject)backingStore[key]; var coBacking = co.ComplexAspect.BackingStore; newCo.ComplexAspect.BackingStore.ForEach(kvp2 => { coBacking[kvp2.Key] = kvp2.Value; }); } else { var val = kvp.Value; if (val.Type == JTokenType.Null && dp.ClrType != typeof(String) && !TypeFns.IsNullableType(dp.ClrType)) { // this can only happen if the client is nonnullable but the server is nullable. backingStore[key] = dp.DefaultValue; } else if (dp.IsEnumType || (dp.DataType.ClrType == typeof(TimeSpan))) { backingStore[key] = val.ToObject(dp.ClrType, _customSerializer); } else { var newValue = val.ToObject(dp.ClrType); if (dp.IsForeignKey && targetAspect != null) { var oldValue = targetAspect.GetValue(key); backingStore[key] = newValue; targetAspect.UpdateRelated(dp, newValue, oldValue); // Above is like next line but with fewer side effects // targetAspect.SetValue(key, val.ToObject(dp.ClrType)); } else { backingStore[key] = newValue; } } } } } else { // prop is a ComplexObject var np = (NavigationProperty)prop; if (kvp.Value.HasValues) { NodeContext newContext; if (np.IsScalar) { var nestedOb = (JObject)kvp.Value; newContext = new NodeContext() { Node = nestedOb, ObjectType = prop.ClrType, StructuralProperty = np }; var entity = (IEntity)CreateAndPopulate(newContext); if (backingStore != null) { backingStore[key] = entity; } } else { var nestedArray = (JArray)kvp.Value; var navSet = (INavigationSet)TypeFns.CreateGenericInstance(typeof(NavigationSet <>), prop.ClrType); nestedArray.Cast <JObject>().ForEach(jo => { newContext = new NodeContext() { Node = jo, ObjectType = prop.ClrType, StructuralProperty = np }; var entity = (IEntity)CreateAndPopulate(newContext); navSet.Add(entity); }); // add to existing nav set if there is one otherwise just set it. object tmp; if (backingStore != null) // TSS - ADDED THIS IF STATEMENT { if (backingStore.TryGetValue(key, out tmp)) { var backingNavSet = (INavigationSet)tmp; navSet.Cast <IEntity>().ForEach(e => backingNavSet.Add(e)); } else { navSet.NavigationProperty = np; navSet.ParentEntity = targetAspect.Entity; backingStore[key] = navSet; } } } } else { // do nothing //if (!np.IsScalar) { // return TypeFns.ConstructGenericInstance(typeof(NavigationSet<>), prop.ClrType); //} else { // return null; //} } } } else { if (backingStore != null) { backingStore[key] = kvp.Value.ToObject <Object>(); } } }); }
private void ParseObject(NodeContext nodeContext, EntityAspect targetAspect) { // backingStore will be null if not allowed to overwrite the entity. var backingStore = (targetAspect == null) ? null : targetAspect.BackingStore; var dict = (IDictionary<String, JToken>)nodeContext.Node; var structuralType = nodeContext.StructuralType; // needs to be the current namingConvention var nc = _mappingContext.EntityManager.MetadataStore.NamingConvention; dict.ForEach(kvp => { var key = nc.ServerPropertyNameToClient(kvp.Key, structuralType); var prop = structuralType.GetProperty(key); if (prop != null) { if (prop.IsDataProperty) { if (backingStore != null) { var dp = (DataProperty)prop; if (dp.IsComplexProperty) { var newCo = (IComplexObject)kvp.Value.ToObject(dp.ClrType); var co = (IComplexObject)backingStore[key]; var coBacking = co.ComplexAspect.BackingStore; newCo.ComplexAspect.BackingStore.ForEach(kvp2 => { coBacking[kvp2.Key] = kvp2.Value; }); } else { var val = kvp.Value; if (val.Type == JTokenType.Null && dp.ClrType != typeof(String) && !TypeFns.IsNullableType(dp.ClrType)) { // this can only happen if the client is nonnullable but the server is nullable. backingStore[key] = dp.DefaultValue; } else if (dp.IsEnumType || (dp.DataType.ClrType == typeof(TimeSpan))) { backingStore[key] = val.ToObject(dp.ClrType, _customSerializer); } else { var newValue = val.ToObject(dp.ClrType); if (dp.IsForeignKey && targetAspect != null) { var oldValue = targetAspect.GetValue(key); backingStore[key] = newValue; targetAspect.UpdateRelated(dp, newValue, oldValue); // Above is like next line but with fewer side effects // targetAspect.SetValue(key, val.ToObject(dp.ClrType)); } else { backingStore[key] = newValue; } } } } } else { // prop is a ComplexObject var np = (NavigationProperty)prop; if (kvp.Value.HasValues) { NodeContext newContext; if (np.IsScalar) { var nestedOb = (JObject)kvp.Value; newContext = new NodeContext() { Node = nestedOb, ObjectType = prop.ClrType, StructuralProperty = np }; var entity = (IEntity)CreateAndPopulate(newContext); if (backingStore != null) backingStore[key] = entity; } else { var nestedArray = (JArray)kvp.Value; var navSet = (INavigationSet)TypeFns.CreateGenericInstance(typeof(NavigationSet<>), prop.ClrType); nestedArray.Cast<JObject>().ForEach(jo => { newContext = new NodeContext() { Node = jo, ObjectType = prop.ClrType, StructuralProperty = np }; var entity = (IEntity)CreateAndPopulate(newContext); navSet.Add(entity); }); // add to existing nav set if there is one otherwise just set it. object tmp; if (backingStore.TryGetValue(key, out tmp)) { var backingNavSet = (INavigationSet)tmp; navSet.Cast<IEntity>().ForEach(e => backingNavSet.Add(e)); } else { navSet.NavigationProperty = np; navSet.ParentEntity = targetAspect.Entity; backingStore[key] = navSet; } } } else { // do nothing //if (!np.IsScalar) { // return TypeFns.ConstructGenericInstance(typeof(NavigationSet<>), prop.ClrType); //} else { // return null; //} } } } else { if (backingStore != null) backingStore[key] = kvp.Value.ToObject<Object>(); } }); }