/// <summary> /// Rebinds a resource to a new entity type /// </summary> /// <typeparam name="T">The new entity type to bind to</typeparam> /// <param name="beo">The existing entity object to be rebound</param> /// <returns>An instance of <typeparamref name="T"/> that is bound to the same underlying resource as <paramref name="beo"/>.</returns> public T Become <T>(BrightstarEntityObject beo) { if (!Mappings.IsKnownInterface(typeof(T))) { throw new MappingNotFoundException(typeof(T)); } var implType = Mappings.GetImplType(typeof(T)); List <BrightstarEntityObject> trackedObjects; if (_trackedObjects.TryGetValue(beo.DataObject.Identity, out trackedObjects)) { var ret = trackedObjects.OfType <T>().FirstOrDefault(); if (ret != null) { return(ret); } } var dataObject = beo.DataObject; foreach (var typeUri in Mappings.MapTypeToUris(implType)) { var typeDo = GetDataObject(new Uri(typeUri), false); dataObject.AddProperty(DataObject.TypeDataObject, typeDo); } return((T)Activator.CreateInstance(implType, this, beo.DataObject)); }
internal void UntrackObject(BrightstarEntityObject obj) { if (_trackedObjects.ContainsKey(obj.DataObject.Identity)) { _trackedObjects.Remove(obj.DataObject.Identity); } _store.DetachDataObject(obj.DataObject); }
/// <summary> /// Creates a new entity collection /// </summary> /// <param name="context">The context that manages the entities</param> /// <param name="parent">The parent entity that contains this collection</param> /// <param name="propertyType">The property type that the collection maps to</param> /// <param name="isInverse">True if the collection represents the inverse of <paramref name="propertyType"/></param> public BrightstarEntityCollection(BrightstarEntityContext context, BrightstarEntityObject parent, string propertyType, bool isInverse = false) : base(new EntityFrameworkCollectionQueryProvider(QueryParser.CreateDefault(), new EntityFrameworkQueryExecutor(context))) { _context = context; _parent = parent; _propertyTypeUri = propertyType; _propertyType = _context.GetDataObject(new Uri(propertyType), false); _isInverse = isInverse; }
/// <summary> /// Removes any reference to <paramref name="toRemove"/> from locally /// tracked objects in this context. /// </summary> /// <param name="toRemove">The entity object that is to be removed</param> /// <remarks>This method is used when <paramref name="toRemove"/> has been locally deleted /// in the context.</remarks> private void RemoveReferences(BrightstarEntityObject toRemove) { foreach (var trackedList in _trackedObjects.Values) { foreach (var tracked in trackedList) { tracked.RemoveReferences(toRemove); } } }
/// <summary> /// Removes a type identifier from a resource /// </summary> /// <typeparam name="T">The entity type whose type identifier is to be removed</typeparam> /// <param name="beo">An existing entity bound to the resource to be updated</param> public void Unbecome <T>(BrightstarEntityObject beo) { if (!Mappings.IsKnownInterface(typeof(T))) { throw new MappingNotFoundException(typeof(T)); } var typeUri = Mappings.GetMappedInterfaceTypeUri(Mappings.GetImplType(typeof(T))); if (!String.IsNullOrEmpty(typeUri)) { var typeDo = GetDataObject(new Uri(typeUri), false); beo.DataObject.RemoveProperty(DataObject.TypeDataObject, typeDo); } }
/// <summary> /// /// </summary> /// <param name="o"></param> public void AddToLoadedObjects(BrightstarEntityObject o) { if (_loadedObjects == null) { // Still do the notification, even if the collection is not loaded OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, o, 0)); return; } if (!_loadedObjects.Any(x => x.DataObject.Identity.Equals(o.DataObject.Identity))) { _loadedObjects.Add(o); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, o, 0)); } }
/// <summary> /// Removes all references to the specified object from locally loaded properties /// on this object. /// </summary> /// <param name="toRemove">The object to be removed</param> /// <remarks>This method is used internally when an object is deleted to ensure that the object /// is removed from the properties and property collections of all locally tracked object</remarks> internal void RemoveReferences(BrightstarEntityObject toRemove) { var propertyNames = _currentPropertyValues.Where(p => p.Value.Equals(toRemove)).Select(p => p.Key).ToList(); foreach (var propertyName in propertyNames) { _currentPropertyValues.Remove(propertyName); OnPropertyChanged(propertyName); } foreach (var c in _currentPropertyCollections.Values) { c.RemoveFromLoadedObjects(toRemove.Identity); } }
internal void UpdatePropertyCollection(string propertyName, BrightstarEntityObject objectToAdd, string identityToRemove) { IBrightstarEntityCollection cachedValue; if (_currentPropertyCollections.TryGetValue(propertyName, out cachedValue)) { if (identityToRemove != null) { cachedValue.RemoveFromLoadedObjects(identityToRemove); } if (objectToAdd != null) { cachedValue.AddToLoadedObjects(objectToAdd); } } }
internal void TrackObject(BrightstarEntityObject obj) { List <BrightstarEntityObject> trackedObjects; if (_trackedObjects.TryGetValue(obj.DataObject.Identity, out trackedObjects)) { if (!trackedObjects.Contains(obj)) { trackedObjects.Add(obj); } } else { trackedObjects = new List <BrightstarEntityObject> { obj }; _trackedObjects[obj.DataObject.Identity] = trackedObjects; } }
/// <summary> /// /// </summary> /// <param name="o"></param> public void AddToLoadedObjects(BrightstarEntityObject o) { if (_loadedObjects == null) { // Still do the notification, even if the collection is not loaded #if WINDOWS_PHONE || PORTABLE OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, o, 0)); #else OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, o)); #endif return; } if (!_loadedObjects.Any(x => x.DataObject.Identity.Equals(o.DataObject.Identity))) { _loadedObjects.Add(o); #if WINDOWS_PHONE || PORTABLE OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, o, 0)); #else OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, o)); #endif } }
internal void UpdateProperty(string propertyName, BrightstarEntityObject newValue) { _currentPropertyValues[propertyName] = newValue; }
internal LiteralsCollection(BrightstarEntityObject beo, string propertyTypeUri) { _beo = beo; _propertyTypeUri = propertyTypeUri; _isAttached = true; }
internal void AddArc(BrightstarEntityObject subj, string propertyType, BrightstarEntityObject obj, bool overwrite) { if (overwrite) { subj.DataObject.SetProperty(propertyType, obj.DataObject); } else { subj.DataObject.AddProperty(propertyType, obj.DataObject); } foreach (var srcObject in GetTrackedObjects(subj.DataObject)) { foreach (var srcProperty in GetArcProperties(srcObject.GetType(), propertyType)) { if (IsCollectionProperty(srcProperty)) { Type itemType = GetItemType(srcProperty); if (itemType.IsAssignableFrom(obj.GetType())) { srcObject.UpdatePropertyCollection(srcProperty.Name, obj, null); } else { srcObject.UpdatePropertyCollection(srcProperty.Name, Bind(obj.DataObject, itemType) as BrightstarEntityObject, null); } } else { var existing = srcProperty.GetValue(srcObject, null) as BrightstarEntityObject; if (obj.Equals(existing)) { continue; } if (srcProperty.PropertyType.Equals(obj.GetType())) { srcObject.UpdateProperty(srcProperty.Name, obj); } else { srcObject.UpdateProperty(srcProperty.Name, Bind(obj.DataObject, srcProperty.PropertyType) as BrightstarEntityObject); } } } } foreach (var destObject in GetTrackedObjects(obj.DataObject)) { foreach (var destProperty in GetInverseArcProperties(destObject.GetType(), propertyType)) { if (IsCollectionProperty(destProperty)) { Type itemType = GetItemType(destProperty); if (itemType.IsAssignableFrom(subj.GetType())) { destObject.UpdatePropertyCollection(destProperty.Name, subj, null); } else { destObject.UpdatePropertyCollection(destProperty.Name, Bind(subj.DataObject, itemType) as BrightstarEntityObject, null); } } else { var existing = destProperty.GetValue(destObject, null) as BrightstarEntityObject; if (existing != null && subj.Equals(existing)) { continue; } if (destProperty.PropertyType.IsAssignableFrom(subj.GetType())) { destObject.UpdateProperty(destProperty.Name, subj); } else { destObject.UpdateProperty(destProperty.Name, Bind(subj.DataObject, destProperty.PropertyType) as BrightstarEntityObject); } } } } }