internal static void UpdateFromReference(IProperty property, IEntity entity, PropertyValue value) { var relationPro = property.As <RelationProperty>(); if (relationPro == null || value.IsNullOrEmpty() || relationPro.RelationPropertyType != RelationPropertyType.Entity) { return; } var refEntity = value.GetStorageValue().As <IEntity>(); var relationKey = RelationshipUnity.GetRelationship(relationPro); if (relationKey == null) { return; } IEntity descEntity; IEntity sourceEntity; if (relationKey.Style == RelationshipStyle.One2Many) { descEntity = entity; sourceEntity = refEntity; } else { descEntity = refEntity; sourceEntity = entity; } foreach (var key in relationKey.Keys) { descEntity.InternalSetValue(key.OtherProperty, sourceEntity.InternalGetValue(key.ThisProperty)); } }
/// <summary> /// 清空并标识,将删除实体集中的所有对象。 /// </summary> /// <param name="value"></param> internal static void SetEntitySetToNull(PropertyValue value) { if (value.IsNullOrEmpty()) { return; } value.GetStorageValue().As <IEntitySetInternalExtension>(e => e.IsNeedClear = true); }
/// <summary> /// 改为移除状态,确定该该实体删除。 /// </summary> /// <param name="value"></param> internal static void SetEntityToNull(PropertyValue value) { if (value.IsNullOrEmpty()) { return; } value.GetStorageValue().As <IEntity>(entity => entity.SetState(EntityState.Detached)); }
/// <summary> /// 获取指定属性的值。 /// </summary> /// <param name="instance">要读取的实体实例。</param> /// <param name="propertyName">要读取的属性的名称。</param> /// <returns>指定属性的值。</returns> public override object ReadValue(TEntity instance, string propertyName) { var property = PropertyUnity.GetProperty(typeof(TEntity), propertyName, true); if (property != null && property is ILoadedProperty) { var value = instance.InternalGetValue(property); return(PropertyValue.IsNullOrEmpty(value) ? null : value.GetStorageValue()); } return(null); }
/// <summary> /// 检查 EntitySet 里的元素是还被销毁。 /// </summary> /// <param name="property"></param> /// <param name="oldValue"></param> /// <param name="newValue"></param> /// <returns></returns> private bool CheckEntitySetDestroy(IProperty property, PropertyValue oldValue, PropertyValue newValue) { if (!property.Is <EntitySetProperty>()) { return(false); } if (oldValue == null || oldValue.IsNullOrEmpty()) { oldValue = ProcessSupposedProperty(property); } if (oldValue != null && !oldValue.IsNullOrEmpty() && !newValue.IsNullOrEmpty()) { var oldSet = oldValue.GetStorageValue() as IEntitySetInternalExtension; oldSet.As <IList>(s => s.Clear()); var newSet = newValue.GetStorageValue() as IEntitySetInternalExtension; newSet.ShiftDetachedList(oldSet); return(true); } return(false); }
/// <summary> /// 内部设置属性值的方法。 /// </summary> /// <param name="property"></param> /// <param name="value"></param> private void InternalSetValue(IProperty property, PropertyValue value) { if ((value.IsNullOrEmpty())) { if (SetNullable(property)) { return; } } if (property is RelationProperty) { lazyMgr.SetValueCreated(property.Name); } valueEntry.Modify(property.Name, value); }
/// <summary> /// 将一个实体对象添加到 <see cref="DataTable"/> 对象里,该 <see cref="DataTable"/> 的结构必须保证与实体结构相符。 /// </summary> /// <param name="entity">当前的实体对象。</param> /// <param name="table">一个使用 <see cref="Construct"/> 构造的 <see cref="DataTable"/>。</param> public static void Putin(this IEntity entity, DataTable table) { Guard.ArgumentNull(entity, "entity"); Guard.ArgumentNull(table, "table"); var properties = PropertyUnity.GetPersistentProperties(entity.EntityType) .Where(s => table.Columns.Contains(s.Name)).ToList(); var count = properties.Count; var data = new object[count]; for (var i = 0; i < count; i++) { var value = entity.InternalGetValue(properties[i]); data[i] = PropertyValue.IsNullOrEmpty(value) ? DBNull.Value : value.GetStorageValue(); } table.Rows.Add(data); }
/// <summary> /// 检查关联属性的空值,如果属性值已设置为null,则不应显示给客户端。 /// </summary> /// <param name="property">要检查的属性。</param> /// <param name="value">属性的值。</param> /// <returns></returns> internal static PropertyValue CheckReturnValue(IProperty property, PropertyValue value) { var relationPro = property.As <RelationProperty>(); if (relationPro == null || value.IsNullOrEmpty()) { return(value); } switch (relationPro.RelationPropertyType) { case RelationPropertyType.Entity: return(CheckReturnEntityValue(value.GetStorageValue().As <IEntity>(), value)); case RelationPropertyType.EntitySet: return(CheckReturnEntitySetValue(value.GetStorageValue().As <IEntitySetInternalExtension>(), value)); default: return(value); } }
/// <summary> /// 读取实体实例的所有值。 /// </summary> /// <param name="instance">要读取的实体实例。</param> /// <returns>所有属性的值。</returns> public override IEnumerable <object> ReadValues(TEntity instance) { return(PropertyUnity.GetLoadedProperties(typeof(TEntity), true) .Select(p => instance.InternalGetValue(p)) .Select(value => PropertyValue.IsNullOrEmpty(value) ? null : value.GetStorageValue())); }