Ejemplo n.º 1
0
        /// <summary>
        /// DataReader到Collection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="dr"></param>
        /// <param name="items"></param>
        /// <param name="dod"></param>
        public static void DataReaderToCollection <T>(EditableDataObjectCollectionBase <T> collection, IDataReader dr, ORMappingItemCollection items, DataToObjectDeligations dod) where T : new()
        {
            dr.NullCheck("dr");
            items.NullCheck("items");
            dod.NullCheck("dod");

            using (ORMappingContext context = ORMappingContext.GetContext())
            {
                while (dr.Read())
                {
                    T graph = new T();

                    DataReaderToObject(dr, items, graph, dod);

                    collection.Add(graph);
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 将DataReader的值写入到对象中
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dr"></param>
 /// <param name="graph"></param>
 /// <param name="dod"></param>
 public static void DataReaderToObject <T>(IDataReader dr, T graph, DataToObjectDeligations dod)
 {
     DataReaderToObject(dr, InnerGetMappingInfoByObject <T>(graph), graph, dod);
 }
Ejemplo n.º 3
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);
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="row"></param>
 /// <param name="graph"></param>
 /// <param name="dod"></param>
 public static void DataRowToObject <T>(DataRow row, T graph, DataToObjectDeligations dod)
 {
     DataRowToObject(row, InnerGetMappingInfoByObject <T>(graph), graph, dod);
 }
Ejemplo n.º 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);
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// DataView的数据转换到集合中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="view"></param>
        /// <param name="dod"></param>
        public static void DataViewToCollection <T>(EditableDataObjectCollectionBase <T> collection, DataView view, DataToObjectDeligations dod) where T : new()
        {
            collection.NullCheck("collection");
            view.NullCheck("view");

            using (ORMappingContext context = ORMappingContext.GetContext())
            {
                foreach (DataRowView drv in view)
                {
                    T graph = new T();

                    DataRowToObject(drv.Row, graph, dod);

                    collection.Add(graph);
                }
            }
        }
Ejemplo n.º 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);
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// DataReader到Collection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="dr"></param>
        /// <param name="items"></param>
        /// <param name="dod"></param>
        /// <returns>返回原有的集合</returns>
        public static EditableDataObjectCollectionBase <T> DataReaderToCollection <T>(EditableDataObjectCollectionBase <T> collection, IDataReader dr, ORMappingItemCollection items, DataToObjectDeligations dod) where T : new()
        {
            dr.NullCheck("dr");
            items.NullCheck("items");
            dod.NullCheck("dod");

            using (ORMappingContext context = ORMappingContext.GetContext())
            {
                while (dr.Read())
                {
                    collection.Add(DataReaderToObject(dr, items, new T(), dod));
                }
            }

            return(collection);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// DataView的数据转换到集合中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="view"></param>
        /// <param name="dod"></param>
        /// <returns>返回原有的集合</returns>
        public static EditableDataObjectCollectionBase <T> DataViewToCollection <T>(EditableDataObjectCollectionBase <T> collection, DataView view, DataToObjectDeligations dod) where T : new()
        {
            collection.NullCheck("collection");
            view.NullCheck("view");

            using (ORMappingContext context = ORMappingContext.GetContext())
            {
                foreach (DataRowView drv in view)
                {
                    collection.Add(DataRowToObject(drv.Row, new T(), dod));
                }
            }

            return(collection);
        }
Ejemplo n.º 10
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="row"></param>
 /// <param name="graph"></param>
 /// <param name="dod"></param>
 /// <returns>所填充的graph</returns>
 public static T DataRowToObject <T>(DataRow row, T graph, DataToObjectDeligations dod)
 {
     return((T)DataRowToObject(row, InnerGetMappingInfoByObject <T>(graph), graph, dod));
 }