/// <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);
                        }
                    }
                }
            }
        }
Exemple #2
0
        private static void FillSqlClauseBuilder <T>(
            SqlClauseBuilderIUW builder,
            T graph,
            ORMappingItemCollection mapping,
            ClauseBindingFlags bindingFlags,
            DoSqlClauseBuilder <T> builderDelegate,
            params string[] ignoreProperties)
        {
            ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph");

            using (ORMappingContext context = ORMappingContext.GetContext())
            {
                foreach (ORMappingItem item in mapping)
                {
                    if (Array.Exists <string>(ignoreProperties, target => (string.Compare(target, item.PropertyName, true) == 0)
                                              ) == false)
                    {
                        if ((item.BindingFlags & bindingFlags) != ClauseBindingFlags.None)
                        {
                            builderDelegate(builder, item, graph);
                        }
                    }
                }
            }

            builder.AppendTenantCode(typeof(T));
        }
        /// <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);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 读取字段的主键的键值对
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="graph"></param>
        /// <param name="ignoreProperties"></param>
        /// <returns></returns>
        public static Dictionary <string, object> GetPrimaryKeyValuePairs <T>(T graph, params string[] ignoreProperties)
        {
            Dictionary <string, object> result = new Dictionary <string, object>();

            ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph");

            ORMappingItemCollection mapping = InnerGetMappingInfoByObject(graph);

            using (ORMappingContext context = ORMappingContext.GetContext())
            {
                foreach (ORMappingItem item in mapping)
                {
                    if (Array.Exists <string>(ignoreProperties, target => (string.Compare(target, item.PropertyName, true) == 0)
                                              ) == false)
                    {
                        if (item.PrimaryKey)
                        {
                            result[item.PropertyName] = GetValueFromObject(item, graph);
                        }
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// DataReader到Collection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="dr"></param>
        /// <param name="dod"></param>
        /// <returns>返回原有的集合</returns>
        public static EditableDataObjectCollectionBase <T> DataReaderToCollection <T>(EditableDataObjectCollectionBase <T> collection, IDataReader dr, DataToObjectDeligations dod) where T : new()
        {
            dr.NullCheck("dr");

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

            return(collection);
        }
        /// <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);
        }
        /// <summary>
        /// DataReader到Collection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="dr"></param>
        /// <param name="dod"></param>
        public static void DataReaderToCollection <T>(EditableDataObjectCollectionBase <T> collection, IDataReader dr, DataToObjectDeligations dod) where T : new()
        {
            dr.NullCheck("dr");

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

                    DataReaderToObject(dr, graph, dod);

                    collection.Add(graph);
                }
            }
        }
        /// <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);
                }
            }
        }
        /// <summary>
        /// 根据对象拼Insert语句
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="graph">对象</param>
        /// <param name="mapping">映射关系</param>
        /// <param name="builder">生成Sql语句类型的Builder如TSqlBuilder或PlSqlBuilder</param>
        /// <param name="ignoreProperties">忽略的字段</param>
        /// <returns>根据传入的对象和对象映射时需要忽略的字段以及类定义上的表名,生成完整的Insert语句</returns>
        public static string GetUpdateSql <T>(T graph, ORMappingItemCollection mapping, ISqlBuilder builder, params string[] ignoreProperties)
        {
            ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph");
            ExceptionHelper.FalseThrow <ArgumentNullException>(mapping != null, "mapping");
            ExceptionHelper.FalseThrow <ArgumentNullException>(builder != null, "builder");

            using (ORMappingContext context = ORMappingContext.GetContext())
            {
                UpdateSqlClauseBuilder updateBuilder = GetUpdateSqlClauseBuilder(graph, mapping, ignoreProperties);
                WhereSqlClauseBuilder  whereBuilder  = GetWhereSqlClauseBuilderByPrimaryKey(graph, mapping);

                return(string.Format("UPDATE {0} SET {1} WHERE {2}",
                                     mapping.TableName,
                                     updateBuilder.ToSqlString(builder),
                                     whereBuilder.ToSqlString(builder)));
            }
        }
		public static ORMappingContext GetContext()
		{
			object objContext = null;
			ORMappingContext context = new ORMappingContext();

			if (ObjectContextCache.Instance.TryGetValue(ORMappingContext.ORMappingContextKey, out objContext))
			{
				//如果已经存在,则返回的实例中,指向原始的context
				context = new ORMappingContext();
				context.OriginalContext = (ORMappingContext)objContext;
			}
			else
			{
				//新创建的context是第一次使用的context
				context = new ORMappingContext();
				context.OriginalContext = context;
				ObjectContextCache.Instance.Add(ORMappingContext.ORMappingContextKey, context);
			}

			return context;
		}
Exemple #12
0
        /// <summary>
        /// 得到加密器
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static ISymmetricEncryption GetEncryptor(string name)
        {
            ISymmetricEncryption result = null;

            using (ORMappingContext context = ORMappingContext.GetContext())
            {
                if (name.IsNullOrEmpty())
                {
                    name = "DefaultPropertyEncryptor";
                }

                result = context.ItemEncryptors[name];

                if (result == null)
                {
                    result = new ORMappintItemEncryption(name);
                    context.ItemEncryptors.Add((ORMappintItemEncryption)result);
                }
            }

            return(result);
        }