private bool SetupEntityInfoForSerialization(Type entityType, EntityInfo entityInfo, IClassMetadata meta) { if (!_removeMode || !_clientEntityObjects.ContainsKey(entityInfo)) { return(false); } var clientEntity = _clientEntityObjects[entityInfo]; var id = meta.GetIdentifier(entityInfo.Entity); meta.SetIdentifier(clientEntity, id); //We have to set the properties from the client object var propNames = meta.PropertyNames; var propTypes = meta.PropertyTypes; for (var i = 0; i < propNames.Length; i++) { var propType = propTypes[i]; var propName = propNames[i]; if (propType.IsAssociationType) { continue; } if (propType.IsComponentType) { var compType = (ComponentType)propType; var compPropNames = compType.PropertyNames; var compPropTypes = compType.Subtypes; var component = GetPropertyValue(meta, entityInfo.Entity, propName); var compValues = compType.GetPropertyValues(component); for (var j = 0; j < compPropNames.Length; j++) { var compPropType = compPropTypes[j]; if (!compPropType.IsAssociationType) { continue; } compValues[j] = null; } var clientCompVal = GetPropertyValue(meta, clientEntity, propName); compType.SetPropertyValues(clientCompVal, compValues); } else { var val = meta.GetPropertyValue(entityInfo.Entity, propName); meta.SetPropertyValue(clientEntity, propName, val); } } // TODO: update unmapped properties typeof(EntityInfo).GetProperty("Entity").SetValue(entityInfo, clientEntity); return(true); }
/// <summary> /// Return an empty placeholder object with the Identifier set. We can safely access the identifier /// property without the object being initialized. /// </summary> /// <typeparam name="T"> /// Тип /// </typeparam> /// <param name="persistentObject"> /// The persistent Object. /// </param> /// <param name="persistentType"> /// The persistent Type. /// </param> /// <param name="classMetadata"> /// The class Metadata. /// </param> /// <returns> /// The <see cref="T"/>. /// </returns> private static T CreatePlaceholder <T>(T persistentObject, Type persistentType, IClassMetadata classMetadata) { var placeholderObject = (T)Activator.CreateInstance(persistentType); if (classMetadata.HasIdentifierProperty) { var identifier = classMetadata.GetIdentifier(persistentObject, EntityMode.Poco); classMetadata.SetIdentifier(placeholderObject, identifier, EntityMode.Poco); } return(placeholderObject); }
/// <summary> /// Sets the value. /// </summary> /// <param name="targetResource">The target resource.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="propertyValue">The property value.</param> void IUpdatable.SetValue(object targetResource, string propertyName, object propertyValue) { IClassMetadata metadata = session.SessionFactory.GetClassMetadata(targetResource.GetType().FullName); if (metadata == null) { throw new DataServiceException("Type not recognized as a valid type for this Context"); } // See if its the Key property first if (metadata.IdentifierPropertyName == propertyName) { metadata.SetIdentifier(targetResource, propertyValue, EntityMode.Poco); } else // Else set the property { metadata.SetPropertyValue(targetResource, propertyName, propertyValue, EntityMode.Poco); } }
private bool SetupEntityInfoForSaving(Type entityType, EntityInfo entityInfo, IClassMetadata meta) { var id = meta.GetIdentifier(entityInfo.Entity); var sessionImpl = _session.GetSessionImplementation(); object dbEntity; string[] propNames; if (entityInfo.EntityState == EntityState.Added) { //meta.Instantiate(id) -> Instantiate method can create a proxy when formulas are present. Saving non persistent proxies will throw an exception dbEntity = Activator.CreateInstance(entityType, true); meta.SetIdentifier(dbEntity, id); } else { //dbEntity = session.Get(entityType, id); Get is not good as it can return a proxy if (meta.IdentifierType.IsComponentType) { // for entities with composite key the identifier is the entity itself // we need to create a copy as ImmediateLoad will fill the given entity var componentType = (ComponentType)meta.IdentifierType; dbEntity = Activator.CreateInstance(entityType, true); // We need to check if the primary key was changed var oldKeyValues = new object[componentType.PropertyNames.Length]; var keyModified = false; for (var i = 0; i < componentType.PropertyNames.Length; i++) { var propName = componentType.PropertyNames[i]; if (entityInfo.OriginalValuesMap.ContainsKey(propName)) { oldKeyValues[i] = entityInfo.OriginalValuesMap[propName]; keyModified = true; } else { oldKeyValues[i] = componentType.GetPropertyValue(entityInfo.Entity, i); } } componentType.SetPropertyValues(dbEntity, oldKeyValues); dbEntity = sessionImpl.ImmediateLoad(entityType.FullName, dbEntity); // As NHibernate does not support updating the primary key we need to do it manually using hql if (keyModified) { var newKeyValues = componentType.GetPropertyValues(entityInfo.Entity); var parameters = new Dictionary <string, KeyValuePair <object, IType> >(); var setStatement = "set "; var whereStatement = "where "; for (var i = 0; i < componentType.PropertyNames.Length; i++) { if (i > 0) { setStatement += ", "; whereStatement += " and "; } var propName = componentType.PropertyNames[i]; var paramName = string.Format("new{0}", propName); setStatement += string.Format("{0}=:{1}", propName, paramName); parameters.Add(paramName, new KeyValuePair <object, IType>(newKeyValues[i], componentType.Subtypes[i])); paramName = string.Format("old{0}", propName); whereStatement += string.Format("{0}=:{1}", propName, paramName); parameters.Add(paramName, new KeyValuePair <object, IType>(oldKeyValues[i], componentType.Subtypes[i])); } var updateQuery = sessionImpl.CreateQuery(new StringQueryExpression(string.Format("update {0} {1} {2}", entityType.Name, setStatement, whereStatement))); foreach (var pair in parameters) { updateQuery.SetParameter(pair.Key, pair.Value.Key, pair.Value.Value); } var count = updateQuery.ExecuteUpdate(); if (count != 1) { throw new InvalidOperationException(string.Format("Query for updating composite key updated '{0}' rows instead of '1'", count)); } componentType.SetPropertyValues(dbEntity, componentType.GetPropertyValues(entityInfo.Entity)); } } else { dbEntity = sessionImpl.ImmediateLoad(entityType.FullName, id); } //dbEntity = session.Get(entityType, id, LockMode.None); } if (dbEntity == null) { throw new NullReferenceException(string.Format("Entity of type '{0}' with id '{1}' does not exists in database", entityType.FullName, id)); } //var modelConfig = BreezeModelConfigurator.GetModelConfiguration(entityType); //Save the original client object _clientEntityObjects[entityInfo] = entityInfo.Entity; //We have to set the properties from the client object propNames = meta.PropertyNames; var propTypes = meta.PropertyTypes; var config = _breezeConfigurator.GetModelConfiguration(entityType); // TODO: set only modified properties for (var i = 0; i < propNames.Length; i++) { var propType = propTypes[i]; var propName = propNames[i]; var memberConfig = config.MemberConfigurations.ContainsKey(propName) ? config.MemberConfigurations[propName] : null; if (memberConfig != null && ( (memberConfig.Ignored.HasValue && memberConfig.Ignored.Value) || (memberConfig.Writable.HasValue && !memberConfig.Writable.Value) || (memberConfig.ShouldDeserializePredicate != null && memberConfig.ShouldDeserializePredicate.Invoke(entityInfo.Entity) == false) )) { continue; } if (propType.IsAssociationType) { continue; } if (propType.IsComponentType) { var compType = (ComponentType)propType; var componentVal = GetPropertyValue(meta, entityInfo.Entity, propName); var dbComponentVal = GetPropertyValue(meta, dbEntity, propName); var compPropsVal = compType.GetPropertyValues(componentVal); compType.SetPropertyValues(dbComponentVal, compPropsVal); } else { var val = meta.GetPropertyValue(entityInfo.Entity, propName); meta.SetPropertyValue(dbEntity, propName, val); } } typeof(EntityInfo).GetProperty("Entity").SetValue(entityInfo, dbEntity); return(true); }
private bool SetupEntityInfoForSerialization(Type entityType, EntityInfo entityInfo, IClassMetadata meta) { if (!_removeMode || !_clientEntityObjects.ContainsKey(entityInfo)) { return(false); } var clientEntity = _clientEntityObjects[entityInfo]; var id = meta.GetIdentifier(entityInfo.Entity); meta.SetIdentifier(clientEntity, id); //We have to set the properties from the client object var propNames = meta.PropertyNames; var propTypes = meta.PropertyTypes; using (var childSession = _session.SessionWithOptions().Connection().OpenSession()) { for (var i = 0; i < propNames.Length; i++) { var propType = propTypes[i]; var propName = propNames[i]; if (propType is IAssociationType associationType) { if (entityInfo.UnmappedValuesMap != null && new[] { "Id", "Code" }.Any(x => entityInfo.UnmappedValuesMap.ContainsKey(propName + x))) { var associatedEntityName = associationType.GetAssociatedEntityName((ISessionFactoryImplementor)_session.SessionFactory); var associatedEntityMetadata = _session.SessionFactory.GetClassMetadata(associatedEntityName); var associatedEntityValue = GetPropertyValue(meta, entityInfo.Entity, propName); if (associatedEntityValue != null) { clientEntity.SetMemberValue(propName, childSession.Load(associatedEntityName, GetPropertyValue(associatedEntityMetadata, associatedEntityValue, null))); } } } else if (propType.IsComponentType) { var compType = (ComponentType)propType; var compPropNames = compType.PropertyNames; var compPropTypes = compType.Subtypes; var component = GetPropertyValue(meta, entityInfo.Entity, propName); var compValues = compType.GetPropertyValues(component); for (var j = 0; j < compPropNames.Length; j++) { var compPropType = compPropTypes[j]; if (!compPropType.IsAssociationType) { continue; } compValues[j] = null; } var clientCompVal = GetPropertyValue(meta, clientEntity, propName); compType.SetPropertyValues(clientCompVal, compValues); } else { var val = meta.GetPropertyValue(entityInfo.Entity, propName); meta.SetPropertyValue(clientEntity, propName, val); } } } // TODO: update unmapped properties typeof(EntityInfo).GetProperty("Entity").SetValue(entityInfo, clientEntity); return(true); }