/// <summary> /// Attaches the object to the specified context /// </summary> /// <param name="context"></param> /// <param name="overwriteExisting"></param> public void Attach(EntityContext context, bool overwriteExisting = false) { if (context == null) { throw new ArgumentNullException("context"); } if (!(context is BrightstarEntityContext)) { throw new ArgumentException( String.Format("An object of type {0} can only be attached to a context that extends {1}", GetType().FullName, typeof(BrightstarEntityContext).FullName)); } if (IsAttached) { if (!context.Equals(_context)) { _context.UntrackObject(this); } } _context = context as BrightstarEntityContext; if (_identity == null) { AssertIdentity(); } if (DataObject == null && _identity != null) { DataObject = _context.GetDataObject(new Uri(_identity), false); var identityInfo = EntityMappingStore.GetIdentityInfo(GetType()); if (identityInfo != null && identityInfo.EnforceClassUniqueConstraint && !overwriteExisting) { _context.EnforceClassUniqueConstraint(_identity, EntityMappingStore.MapTypeToUris(GetType())); } foreach (var typeUri in EntityMappingStore.MapTypeToUris(GetType())) { if (!String.IsNullOrEmpty(typeUri)) { var typeDo = _context.GetDataObject(new Uri(typeUri), false); if (typeDo != null) { DataObject.AddProperty(Client.DataObject.TypeDataObject, typeDo); } } } } // if (DataObject != null) // { _context.TrackObject(this); // } if (_currentItemValues != null) { foreach (var propertyName in _currentItemValues.Keys.ToList()) { PropertyInfo p = GetType().GetProperty(propertyName); p.SetValue(this, _currentItemValues[propertyName], null); } _currentItemValues.Clear(); } }
/// <summary> /// Raises the PropertyChanged event /// </summary> /// <param name="propertyName">The name of the property that has been modified</param> protected virtual void OnPropertyChanged(string propertyName) { var identityInfo = EntityMappingStore.GetIdentityInfo(GetType()); if (identityInfo != null && identityInfo.KeyProperties != null && identityInfo.KeyProperties.Any(p => p.Name.Equals(propertyName))) { var newKey = GenerateEntityKey(); SetKey(newKey); } PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } }
private string GenerateEntityKey() { var identityCacheInfo = EntityMappingStore.GetIdentityInfo(GetType()); if (identityCacheInfo != null && identityCacheInfo.KeyProperties != null) { // Generate the key string var values = new object[identityCacheInfo.KeyProperties.Length]; for (var i = 0; i < identityCacheInfo.KeyProperties.Length; i++) { if (!_currentItemValues.TryGetValue(identityCacheInfo.KeyProperties[i].Name, out values[i])) { values[i] = identityCacheInfo.KeyProperties[i].GetValue(this, null); } } return(identityCacheInfo.KeyConverter.GenerateKey(values, identityCacheInfo.KeySeparator, GetType())); } return(Guid.NewGuid().ToString()); }
internal string GetIdentityBase() { var identityCacheInfo = EntityMappingStore.GetIdentityInfo(GetType()); return(identityCacheInfo.BaseUri); }