Esempio n. 1
0
        private void SetEntityValue(AbstractEntity entity, SchemaColumn fieldAttr, object value)
        {
            string columnName = fieldAttr.Name;
            object fieldValue;

            if (fieldAttr.IsSerialized)
            {
                //指定序列化方式
                if (fieldAttr.DbType == ColumnDbType.LongBlob || fieldAttr.DbType == ColumnDbType.Blob)
                {
                    fieldValue = DeserializeBinaryObject(value, fieldAttr);
                }
                else
                {
                    fieldValue = DeserializeJsonObject(value, fieldAttr);
                }
                if (fieldValue is EntityChangeEvent)
                {
                    ((EntityChangeEvent)fieldValue).PropertyName = columnName;
                }
            }
            else
            {
                fieldValue = AbstractEntity.ParseValueType(value, fieldAttr.ColumnType);
            }
            if (fieldAttr.CanWrite)
            {
                entity.SetPropertyValue(columnName, fieldValue);
            }
            else
            {
                entity.SetFieldValue(columnName, fieldValue);
            }
        }
Esempio n. 2
0
        private void SetEntityValue(SchemaTable schemaTable, IDataReader reader, AbstractEntity entity)
        {
            var columns = schemaTable.GetColumnNames();

            foreach (var columnName in columns)
            {
                SchemaColumn fieldAttr;
                if (!schemaTable.Columns.TryGetValue(columnName, out fieldAttr))
                {
                    continue;
                }
                object fieldValue = null;
                if (fieldAttr.IsSerialized)
                {
                    var value = reader[columnName];
                    //指定序列化方式
                    if (fieldAttr.DbType == ColumnDbType.LongBlob || fieldAttr.DbType == ColumnDbType.Blob)
                    {
                        fieldValue = DeserializeBinaryObject(schemaTable, entity, value, fieldAttr, columnName);
                    }
                    else
                    {
                        fieldValue = DeserializeJsonObject(schemaTable, entity, value, fieldAttr, columnName);
                    }
                    if (fieldValue is EntityChangeEvent)
                    {
                        ((EntityChangeEvent)fieldValue).PropertyName = columnName;
                    }
                }
                else
                {
                    try
                    {
                        fieldValue = entity.ParseValueType(reader[columnName], fieldAttr.ColumnType);
                    }
                    catch (Exception ex)
                    {
                        TraceLog.WriteError("Table:{0} column:{1} parse value error:\r\n{0}", schemaTable.EntityName, columnName, ex);
                    }
                }
                if (fieldAttr.CanWrite)
                {
                    entity.SetPropertyValue(columnName, fieldValue);
                }
                else
                {
                    entity.SetFieldValue(columnName, fieldValue);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="column"></param>
        /// <param name="value"></param>
        protected virtual void SetPropertyValue(T entity, SchemaColumn column, object value)
        {
            object fieldValue;

            if (column.IsSerialized)
            {
                //指定序列化方式
                if (column.DbType == ColumnDbType.LongBlob ||
                    column.DbType == ColumnDbType.Blob)
                {
                    fieldValue = ProtoBufUtils.Deserialize(value as byte[], column.ColumnType);
                }
                else
                {
                    fieldValue = DeserializeJsonObject(value, column);
                }
            }
            else
            {
                fieldValue = AbstractEntity.ParseValueType(value, column.ColumnType);
            }
            ObjectAccessor.Create(entity)[column.Name] = fieldValue;
        }
Esempio n. 4
0
        private void SetEntityValue(SchemaTable schemaTable, IDataReader reader, AbstractEntity entity)
        {
            var columns = schemaTable.GetColumnNames();

            foreach (var columnName in columns)
            {
                SchemaColumn fieldAttr;
                if (!schemaTable.Columns.TryGetValue(columnName, out fieldAttr))
                {
                    continue;
                }
                object fieldValue = null;
                if (fieldAttr.IsJson)
                {
                    var value = reader[columnName];
                    if (fieldAttr.ColumnType.IsSubclassOf(typeof(Array)))
                    {
                        value = value.ToString().StartsWith("[") ? value : "[" + value + "]";
                    }
                    try
                    {
                        string tempValue = value.ToNotNullString();
                        if (!string.IsNullOrEmpty(fieldAttr.JsonDateTimeFormat) &&
                            tempValue.IndexOf(@"\/Date(") == -1)
                        {
                            fieldValue = JsonUtils.DeserializeCustom(tempValue, fieldAttr.ColumnType, fieldAttr.JsonDateTimeFormat);
                        }
                        else
                        {
                            fieldValue = JsonUtils.Deserialize(tempValue, fieldAttr.ColumnType);
                        }
                    }
                    catch (Exception ex)
                    {
                        TraceLog.WriteError("Table:{0} key:{1} column:{2} deserialize json error:{3} to {4}\r\nException:{5}",
                                            schemaTable.Name,
                                            entity.GetKeyCode(),
                                            columnName,
                                            fieldValue,
                                            fieldAttr.ColumnType.FullName,
                                            ex);
                    }
                    if (fieldValue is EntityChangeEvent)
                    {
                        ((EntityChangeEvent)fieldValue).PropertyName = columnName;
                    }
                }
                else
                {
                    try
                    {
                        fieldValue = entity.ParseValueType(reader[columnName], fieldAttr.ColumnType);
                    }
                    catch (Exception ex)
                    {
                        TraceLog.WriteError("Table:{0} column:{1} parse value error:\r\n{0}", schemaTable.Name, columnName, ex);
                    }
                }
                if (fieldAttr.CanWrite)
                {
                    entity.SetPropertyValue(columnName, fieldValue);
                }
                else
                {
                    entity.SetFieldValue(columnName, fieldValue);
                }
            }
        }