示例#1
0
        /// <summary>
        /// 对数据进行最后的修饰,例如对日期类型的属性加工
        /// </summary>
        /// <param name="item"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private static object DecorateDate(ORMappingItem item, object data)
        {
            object result = data;

            if (data is DateTime)
            {
                DateTime dt = (DateTime)data;

                if (item.UtcTimeToLocal)
                {
                    DateTime changedDT   = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
                    DateTime convertedDT = TimeZoneContext.Current.ConvertTimeFromUtc(changedDT);
                    result = DateTime.SpecifyKind(convertedDT, DateTimeKind.Local);
                }
                else
                {
                    if (dt.Kind == DateTimeKind.Unspecified)
                    {
                        result = DateTime.SpecifyKind(dt, DateTimeKind.Local);
                    }
                }
            }

            return(result);
        }
示例#2
0
        private static ORMappingItemCollection GetMappingItems(RelativeAttributes attrs, MemberInfo mi)
        {
            ORMappingItemCollection items = new ORMappingItemCollection();

            ORMappingItem item = new ORMappingItem();

            item.PropertyName  = mi.Name;
            item.DataFieldName = mi.Name;

            if (attrs.FieldMapping != null)
            {
                FillMappingItemByAttr(item, attrs.FieldMapping);
            }

            if (attrs.SqlBehavior != null)
            {
                FillMappingItemByBehaviorAttr(item, attrs.SqlBehavior);
            }

            if (attrs.PropertyEncryption != null)
            {
                FillMappingItemByEncryptionAttr(item, attrs.PropertyEncryption);
            }

            item.MemberInfo    = mi;
            item.DeclaringType = mi.DeclaringType;

            items.Add(item);

            return(items);
        }
示例#3
0
 private static void FillMappingItemByBehaviorAttr(ORMappingItem item, SqlBehaviorAttribute sba)
 {
     item.BindingFlags              = sba.BindingFlags;
     item.DefaultExpression         = sba.DefaultExpression;
     item.ForceUseDefaultExpression = sba.ForceUseDefaultExpression;
     item.EnumUsage = sba.EnumUsage;
 }
示例#4
0
        private static void SetMemberValueToObject(ORMappingItem item, MemberInfo mi, object graph, object data)
        {
            data = DecorateDate(item, data);

            IMemberAccessor accessor = graph as IMemberAccessor;

            if (accessor != null)
            {
                accessor.SetValue(graph, mi.Name, data);
            }
            else
            {
                switch (mi.MemberType)
                {
                case MemberTypes.Property:
                    PropertyInfo pi = (PropertyInfo)mi;
                    if (pi.CanWrite)
                    {
                        pi.SetValue(graph, data, null);
                    }
                    break;

                case MemberTypes.Field:
                    FieldInfo fi = (FieldInfo)mi;
                    fi.SetValue(graph, data);
                    break;

                default:
                    ThrowInvalidMemberInfoTypeException(mi);
                    break;
                }
            }
        }
示例#5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="row">DataRow对象</param>
        /// <param name="items">映射关系</param>
        /// <param name="graph">对象</param>
        /// <param name="dod"></param>
        public static void DataRowToObject(DataRow row, ORMappingItemCollection items, object graph, DataToObjectDeligations dod)
        {
            ExceptionHelper.FalseThrow <ArgumentNullException>(row != null, "row");
            ExceptionHelper.FalseThrow <ArgumentNullException>(items != null, "items");
            ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph");
            ExceptionHelper.FalseThrow <ArgumentNullException>(row.Table != null, "row.Table");

            using (ORMappingContext context = ORMappingContext.GetContext())
            {
                foreach (DataColumn column in row.Table.Columns)
                {
                    if (items.ContainsKey(column.ColumnName))
                    {
                        ORMappingItem item = items[column.ColumnName];

                        System.Type realType = GetRealType(item.MemberInfo);

                        object data = row[column];

                        if (item.EncryptProperty)
                        {
                            data = DecryptPropertyValue(item, data);
                        }

                        if (Convertible(realType, data))
                        {
                            SetValueToObject(item, graph, ConvertData(item, data), row, dod);
                        }
                    }
                }
            }
        }
示例#6
0
        /// <summary>
        /// 将DataReader的值写入到对象中
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="dr">IDataReader对象</param>
        /// <param name="items">映射关系</param>
        /// <param name="graph">对象</param>
        /// <param name="dod"></param>
        public static void DataReaderToObject <T>(IDataReader dr, ORMappingItemCollection items, T graph, DataToObjectDeligations dod)
        {
            ExceptionHelper.FalseThrow <ArgumentNullException>(dr != null, "dr");
            ExceptionHelper.FalseThrow <ArgumentNullException>(items != null, "items");
            ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph");

            DataTable schemaTable = dr.GetSchemaTable();

            using (ORMappingContext context = ORMappingContext.GetContext())
            {
                foreach (DataRow row in schemaTable.Rows)
                {
                    string columnName = row["ColumnName"].ToString();
                    if (items.ContainsKey(columnName))
                    {
                        ORMappingItem item     = items[row["ColumnName"].ToString()];
                        System.Type   realType = GetRealType(item.MemberInfo);

                        object data = dr[columnName];

                        if (item.EncryptProperty)
                        {
                            data = DecryptPropertyValue(item, data);
                        }

                        if (Convertible(realType, data))
                        {
                            SetValueToObject(item, graph, ConvertData(item, data), dr, dod);
                        }
                    }
                }
            }
        }
示例#7
0
        private static void SetValueToObject(ORMappingItem item, object graph, object data, object row, DataToObjectDeligations dod)
        {
            if (string.IsNullOrEmpty(item.SubClassPropertyName))
            {
                SetMemberValueToObject(item.MemberInfo, graph, data);
            }
            else
            {
                if (graph != null)
                {
                    MemberInfo mi = TypePropertiesWithNonPublicCacheQueue.Instance.GetPropertyInfoDirectly(graph.GetType(), item.PropertyName);

                    if (mi == null)
                    {
                        mi = graph.GetType().GetField(item.PropertyName,
                                                      BindingFlags.Instance | BindingFlags.Public);
                    }

                    if (mi != null)
                    {
                        object subGraph = GetMemberValueFromObject(mi, graph);

                        if (subGraph == null)
                        {
                            bool useDefaultObject = true;

                            if (dod != null)
                            {
                                MappingEventArgs args = new MappingEventArgs();

                                args.DataFieldName = item.DataFieldName;
                                args.PropertyName  = item.PropertyName;
                                args.Graph         = graph;

                                subGraph = dod.OnCreateSubObjectDelegate(row, args, ref useDefaultObject);
                            }

                            if (useDefaultObject)
                            {
                                if (string.IsNullOrEmpty(item.SubClassTypeDescription) == false)
                                {
                                    subGraph = TypeCreator.CreateInstance(item.SubClassTypeDescription);
                                }
                                else
                                {
                                    subGraph = Activator.CreateInstance(GetRealType(mi), true);
                                }
                            }

                            SetMemberValueToObject(item.MemberInfo, subGraph, data);
                            SetMemberValueToObject(mi, graph, subGraph);
                        }
                        else
                        {
                            SetMemberValueToObject(item.MemberInfo, subGraph, data);
                        }
                    }
                }
            }
        }
示例#8
0
        private static object GetValueFromObjectDirectly(ORMappingItem item, object graph)
        {
            object data = GetMemberValueFromObject(item.MemberInfo, graph);

            if (data != null)
            {
                System.Type dataType = data.GetType();
                if (dataType.IsEnum)
                {
                    if (item.EnumUsage == EnumUsageTypes.UseEnumValue)
                    {
                        data = (int)data;
                    }
                    else
                    {
                        data = data.ToString();
                    }
                }
                else
                if (dataType == typeof(TimeSpan))
                {
                    data = ((TimeSpan)data).TotalSeconds;
                }
            }

            return(data);
        }
示例#9
0
        private static ORMappingItemCollection GetMappingItemsBySubClass(RelativeAttributes attrs, MemberInfo sourceMI)
        {
            ORMappingItemCollection items = new ORMappingItemCollection();

            System.Type subType = attrs.SubClassType != null ? attrs.SubClassType.Type : GetRealType(sourceMI);

            MemberInfo[] mis = GetTypeMembers(subType);

            foreach (SubClassORFieldMappingAttribute attr in attrs.SubClassFieldMappings)
            {
                MemberInfo mi = GetMemberInfoByName(attr.SubPropertyName, mis);

                if (mi != null)
                {
                    if (items.ContainsKey(attr.DataFieldName) == false)
                    {
                        ORMappingItem item = new ORMappingItem();

                        item.PropertyName         = sourceMI.Name;
                        item.SubClassPropertyName = attr.SubPropertyName;
                        item.MemberInfo           = mi;
                        item.DeclaringType        = sourceMI.DeclaringType;

                        if (attrs.SubClassType != null)
                        {
                            item.SubClassTypeDescription = attrs.SubClassType.TypeDescription;
                        }

                        FillMappingItemByAttr(item, attr);

                        items.Add(item);
                    }
                }
            }

            foreach (SubClassSqlBehaviorAttribute attr in attrs.SubClassFieldSqlBehaviors)
            {
                ORMappingItem item = FindItemBySubClassPropertyName(attr.SubPropertyName, items);

                if (item != null)
                {
                    FillMappingItemByBehaviorAttr(item, attr);
                }
            }

            foreach (SubClassPropertyEncryptionAttribute attr in attrs.SubClassPropertyEncryptions)
            {
                ORMappingItem item = FindItemBySubClassPropertyName(attr.SubPropertyName, items);

                if (item != null)
                {
                    FillMappingItemByEncryptionAttr(item, attr);
                }
            }

            return(items);
        }
示例#10
0
 private static void FillMappingItemByAttr(ORMappingItem item, ORFieldMappingAttribute fm)
 {
     item.DataFieldName = fm.DataFieldName;
     item.IsIdentity    = fm.IsIdentity;
     item.IsNullable    = fm.IsNullable;
     item.Length        = fm.Length;
     item.PrimaryKey    = fm.PrimaryKey;
     item.Format        = fm.Format;
 }
示例#11
0
        private static object GetMemberValueFromObject(ORMappingItem item, MemberInfo mi, object graph)
        {
            try
            {
                object data = null;

                IMemberAccessor accessor = graph as IMemberAccessor;

                if (accessor != null)
                {
                    data = accessor.GetValue(graph, mi.Name);
                }
                else
                {
                    switch (mi.MemberType)
                    {
                    case MemberTypes.Property:
                        PropertyInfo pi = (PropertyInfo)mi;
                        if (pi.CanRead)
                        {
                            data = pi.GetValue(graph, null);
                        }
                        break;

                    case MemberTypes.Field:
                        FieldInfo fi = (FieldInfo)mi;
                        data = fi.GetValue(graph);
                        break;

                    default:
                        ThrowInvalidMemberInfoTypeException(mi);
                        break;
                    }
                }

                if (data is DateTime)
                {
                    if (item.UtcTimeToLocal)
                    {
                        DateTime dt = (DateTime)data;

                        dt = DateTime.SpecifyKind(dt, DateTimeKind.Unspecified);

                        data = TimeZoneContext.Current.ConvertTimeToUtc(dt);
                    }
                }

                return(data);
            }
            catch (System.Exception ex)
            {
                System.Exception realEx = ExceptionHelper.GetRealException(ex);

                throw new ApplicationException(string.Format("读取属性{0}值的时候出错,{1}", mi.Name, realEx.Message));
            }
        }
示例#12
0
        private static object FormatValue(object dataValue, ORMappingItem item)
        {
            object result = dataValue;

            if (item.Format.IsNotEmpty())
            {
                result = string.Format(item.Format, dataValue);
            }

            return(result);
        }
示例#13
0
        private static void DoWhereSqlClauseBuilder <T>(SqlClauseBuilderIUW builder, ORMappingItem item, T graph)
        {
            object data = GetValueFromObject(item, graph);

            if ((data == null || data == DBNull.Value))
            {
                builder.AppendItem(item.DataFieldName, data, SqlClauseBuilderBase.Is);
            }
            else
            {
                builder.AppendItem(item.DataFieldName, FormatValue(data, item));
            }
        }
示例#14
0
        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="item"></param>
        /// <param name="originalData"></param>
        /// <returns></returns>
        private static object EncryptPropertyValue(ORMappingItem item, object originalData)
        {
            object result = originalData;

            if (originalData != null && originalData != DBNull.Value)
            {
                if (originalData is string == false || (string)originalData != string.Empty)
                {
                    ISymmetricEncryption encryptor = ORMappingItemEncryptionHelper.GetEncryptor(item.EncryptorName);
                    result = encryptor.EncryptString(originalData.ToString()).ToBase16String();
                }
            }

            return(result);
        }
示例#15
0
        private static ORMappingItem FindItemBySubClassPropertyName(string subPropertyName, ORMappingItemCollection items)
        {
            ORMappingItem result = null;

            foreach (ORMappingItem item in items)
            {
                if (item.SubClassPropertyName == subPropertyName)
                {
                    result = item;
                    break;
                }
            }

            return(result);
        }
示例#16
0
        private static object ConvertData(ORMappingItem item, object data)
        {
            try
            {
                System.Type realType = GetRealType(item.MemberInfo);

                return(DataConverter.ChangeType(data, realType));
            }
            catch (System.Exception ex)
            {
                throw new SystemSupportException(
                          string.Format(Resource.ConvertDataFieldToPropertyError,
                                        item.DataFieldName, item.PropertyName, ex.Message),
                          ex
                          );
            }
        }
示例#17
0
        private static object GetValueFromObject(ORMappingItem item, object graph)
        {
            object data = null;

            if (string.IsNullOrEmpty(item.SubClassPropertyName))
            {
                data = GetValueFromObjectDirectly(item, graph);

                if (item.EncryptProperty)
                {
                    data = EncryptPropertyValue(item, data);
                }
            }
            else
            {
                if (graph != null)
                {
                    MemberInfo mi = TypePropertiesWithNonPublicCacheQueue.Instance.GetPropertyInfoDirectly(graph.GetType(), item.PropertyName);

                    if (mi == null)
                    {
                        mi = graph.GetType().GetField(item.PropertyName,
                                                      BindingFlags.Instance | BindingFlags.Public);
                    }

                    if (mi != null)
                    {
                        object subGraph = GetMemberValueFromObject(mi, graph);

                        if (subGraph != null)
                        {
                            data = GetValueFromObjectDirectly(item, subGraph);

                            if (data != null && item.EncryptProperty)
                            {
                                data = EncryptPropertyValue(item, data);
                            }
                        }
                    }
                }
            }

            return(data);
        }
示例#18
0
        /// <summary>
        /// 复制一个MappingItem
        /// </summary>
        /// <returns></returns>
        public ORMappingItem Clone()
        {
            ORMappingItem newItem = new ORMappingItem();

            newItem.dataFieldName        = this.dataFieldName;
            newItem.propertyName         = this.propertyName;
            newItem.subClassPropertyName = this.subClassPropertyName;
            newItem.isIdentity           = this.isIdentity;
            newItem.primaryKey           = this.primaryKey;
            newItem.length     = this.length;
            newItem.isNullable = this.isNullable;
            newItem.subClassTypeDescription = this.subClassTypeDescription;
            newItem.bindingFlags            = this.bindingFlags;
            newItem.defaultExpression       = this.defaultExpression;
            newItem.memberInfo      = this.memberInfo;
            newItem.enumUsage       = this.enumUsage;
            newItem.encryptProperty = this.encryptProperty;
            newItem.encryptorName   = this.encryptorName;
            newItem.format          = this.format;

            return(newItem);
        }
示例#19
0
        private static object DecryptPropertyValue(ORMappingItem item, object originalData)
        {
            object result = originalData;

            if (originalData is string)
            {
                string stringValue = (string)originalData;

                if (stringValue.IsNotEmpty())
                {
                    try
                    {
                        ISymmetricEncryption encryptor = ORMappingItemEncryptionHelper.GetEncryptor(item.EncryptorName);
                        result = encryptor.DecryptString(stringValue.ToBase16Bytes());
                    }
                    catch (System.FormatException)
                    {
                    }
                }
            }

            return(result);
        }
示例#20
0
        private static void DoSelectSqlClauseBuilder <TBuilder, T>(TBuilder builder, ORMappingItem item, T graph) where TBuilder : SqlClauseBuilderIUW <TBuilder>
        {
            if (item.PrimaryKey == false)
            {
                object data = GetValueFromObject(item, graph);

                if ((data == null || data == DBNull.Value || (data != null && data.Equals(TypeCreator.GetTypeDefaultValue(data.GetType())))))
                {
                    if (item.DefaultExpression.IsNullOrEmpty())
                    {
                        builder.AppendItem(item.DataFieldName, "NULL", "");
                    }
                    else
                    {
                        builder.AppendItem(item.DataFieldName, item.DefaultExpression, "", true);
                    }
                }
                else
                {
                    builder.AppendItem(item.DataFieldName, FormatValue(data, item));
                }
            }
        }
示例#21
0
        private static void DoInsertUpdateSqlClauseBuilder <TBuilder, T>(TBuilder builder, ORMappingItem item, T graph) where TBuilder : SqlClauseBuilderIUW <TBuilder>
        {
            if (item.IsIdentity == false)
            {
                object data = GetValueFromObject(item, graph);

                bool considerNull = data == null || data == DBNull.Value || (data != null && data.Equals(TypeCreator.GetTypeDefaultValue(data.GetType())));

                if ((item.ForceUseDefaultExpression || considerNull) && item.DefaultExpression.IsNotEmpty())
                {
                    builder.AppendItem(item.DataFieldName, item.DefaultExpression, SqlClauseBuilderBase.EqualTo, true);
                }
                else
                {
                    builder.AppendItem(item.DataFieldName, FormatValue(data, item));
                }
            }
        }
示例#22
0
        private static void DoWhereSqlClauseBuilderByPrimaryKey <TBuilder, T>(TBuilder builder, ORMappingItem item, T graph) where TBuilder : SqlClauseBuilderIUW <TBuilder>
        {
            if (item.PrimaryKey)
            {
                object data = GetValueFromObject(item, graph);

                if ((data == null || data == DBNull.Value))
                {
                    builder.AppendItem(item.DataFieldName, data, SqlClauseBuilderBase.Is);
                }
                else
                {
                    builder.AppendItem(item.DataFieldName, data);
                }
            }
        }
示例#23
0
		/// <summary>
		/// 复制一个MappingItem
		/// </summary>
		/// <returns></returns>
		public ORMappingItem Clone()
		{
			ORMappingItem newItem = new ORMappingItem();

			newItem.dataFieldName = this.dataFieldName;
			newItem.propertyName = this.propertyName;
			newItem.subClassPropertyName = this.subClassPropertyName;
			newItem.isIdentity = this.isIdentity;
			newItem.primaryKey = this.primaryKey;
			newItem.length = this.length;
			newItem.isNullable = this.isNullable;
			newItem.subClassTypeDescription = this.subClassTypeDescription;
			newItem.bindingFlags = this.bindingFlags;
			newItem.defaultExpression = this.defaultExpression;
			newItem.memberInfo = this.memberInfo;
			newItem.enumUsage = this.enumUsage;
			newItem.encryptProperty = this.encryptProperty;
			newItem.encryptorName = this.encryptorName;
			newItem.format = this.format;

			return newItem;
		}
示例#24
0
        private static void DoInsertUpdateSqlClauseBuilder <T>(SqlClauseBuilderIUW builder, ORMappingItem item, T graph)
        {
            if (item.IsIdentity == false)
            {
                object data = GetValueFromObject(item, graph);

                if ((data == null || data == DBNull.Value || (data != null && data.Equals(TypeCreator.GetTypeDefaultValue(data.GetType())))) &&
                    string.IsNullOrEmpty(item.DefaultExpression) == false)
                {
                    builder.AppendItem(item.DataFieldName, item.DefaultExpression, SqlClauseBuilderBase.EqualTo, true);
                }
                else
                {
                    builder.AppendItem(item.DataFieldName, FormatValue(data, item));
                }
            }
        }
示例#25
0
 private static void FillMappingItemByEncryptionAttr(ORMappingItem item, PropertyEncryptionAttribute pea)
 {
     item.EncryptProperty = true;
     item.EncryptorName   = pea.EncryptorName;
 }
示例#26
0
        private static void DoWhereSqlClauseBuilderByChangedFieldsWithoutPrimaryKey <TBuilder, T>(TBuilder builder, ORMappingItem item, T graph) where TBuilder : SqlClauseBuilderIUW <TBuilder>
        {
            if (item.PrimaryKey == false)
            {
                object data = GetValueFromObject(item, graph);

                if ((data == null || data == DBNull.Value))
                {
                    builder.AppendItem(item.DataFieldName, data, SqlClauseBuilderBase.IsNot);
                }
                else
                {
                    builder.AppendItem(item.DataFieldName, FormatValue(data, item), SqlClauseBuilderBase.NotEqualTo);
                }
            }
        }