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);
        }
        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);
            }

            item.MemberInfo = mi;

            items.Add(item);

            return(items);
        }
        private static object GetValueFromObject(ORMappingItem item, object graph)
        {
            object data = null;

            if (string.IsNullOrEmpty(item.SubClassPropertyName))
            {
                data = GetValueFromObjectDirectly(item, graph);
            }
            else
            {
                if (graph != null)
                {
                    MemberInfo mi = graph.GetType().GetProperty(item.PropertyName,
                                                                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                    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);
                        }
                    }
                }
            }

            return(data);
        }
 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;
 }
        public static string GetColumnName(System.Type type, string propName)
        {
            ORMappingItemCollection mapping      = InnerGetMappingInfo(type);
            ORMappingItem           propertyItem = mapping[propName];

            if (propertyItem != null)
            {
                return(propertyItem.DataFieldName);
            }
            throw new ArgumentException("找不到属性" + propName + "所对应的表字段!");
        }
        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, data);
            }
        }
        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);
        }
        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
                          );
            }
        }
        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.Contains(attr.DataFieldName) == false)
                    {
                        ORMappingItem item = new ORMappingItem();

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

                        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);
                }
            }

            return(items);
        }
Esempio n. 10
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;

            return(newItem);
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="row">DataRow对象</param>
        /// <param name="items">映射关系</param>
        /// <param name="graph">对象</param>
        /// <param name="dod"></param>
        public static void DataRowToObject <T>(DataRow row, ORMappingItemCollection items, T 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");

            foreach (DataColumn column in row.Table.Columns)
            {
                if (items.Contains(column.ColumnName))
                {
                    ORMappingItem item = items[column.ColumnName];

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

                    object data = row[column];
                    if (Convertible(realType, data))
                    {
                        SetValueToObject(item, graph, ConvertData(item, data), row, dod);
                    }
                }
            }
        }
        /// <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();

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

                    object data = dr[columnName];
                    if (Convertible(realType, data))
                    {
                        SetValueToObject(item, graph, ConvertData(item, data), dr, dod);
                    }
                }
            }
        }
        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 = graph.GetType().GetProperty(item.PropertyName,
                                                                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                    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);
                        }
                    }
                }
            }
        }
        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, data);
                }
            }
        }
 private static void DoSelectSqlClauseBuilder(SelectSqlClauseBuilder builder, ORMappingItem item)
 {
     builder.AppendItem(item.DataFieldName);
 }
 private static void FillMappingItemByBehaviorAttr(ORMappingItem item, SqlBehaviorAttribute sba)
 {
     item.BindingFlags      = sba.BindingFlags;
     item.DefaultExpression = sba.DefaultExpression;
     item.EnumUsage         = sba.EnumUsage;
 }