Exemplo n.º 1
0
        private static void DoFillEntity(IEntityDataSource ds, Object obj, Type type, string prefix)
        {
            //装载属性的值——简单类型BaseType的
            foreach (string columnName in ds)
            {
                string mappingName = columnName.ToUpper();

                if (!String.IsNullOrEmpty(prefix))
                {
                    if (mappingName.StartsWith(prefix.ToUpper()))
                    {
                        mappingName = mappingName.Substring(prefix.Length);
                    }
                }

                if (String.IsNullOrEmpty(prefix))
                {
                    prefix = string.Empty;
                }

                PropertyDataBindingInfo propertyBindingInfo = GetPropertyInfo(type, mappingName);

                if (propertyBindingInfo == null || columnName.ToUpper() != (prefix.ToUpper() + mappingName))
                {
                    continue;
                }

                if (ds[columnName] != DBNull.Value && ValidateData(propertyBindingInfo, ds[columnName]))
                {
                    Object val = ds[columnName];
                    if (propertyBindingInfo.PropertyInfo.PropertyType == typeof(string))
                    {
                        val = val.ToString().Trim();
                    }
                    propertyBindingInfo.PropertyInfo.SetValue(obj, val, null);
                }
                else if (ds[columnName] == DBNull.Value && ValidateData(propertyBindingInfo, ds[columnName]) && propertyBindingInfo.PropertyInfo.PropertyType == typeof(string))
                {
                    //这个是对String类型的特殊处理,防止为null,其他的都以默认值展示
                    propertyBindingInfo.PropertyInfo.SetValue(obj, string.Empty, null);
                }
            }

            // fill referenced objects
            List <ReferencedTypeBindingInfo> refList = GetReferenceObjects(type);

            foreach (ReferencedTypeBindingInfo refObj in refList)
            {
                if (refObj.Type is IList)
                {
                    refObj.PropertyInfo.SetValue(obj, BuildEntityList(ds, refObj.Type, refObj.Prefix), null);
                }
                else
                {
                    refObj.PropertyInfo.SetValue(obj, BuildEntity(ds, refObj.Type, refObj.Prefix), null);
                }
            }
        }
Exemplo n.º 2
0
        private static void DoFillEntity(IEntityDataSource ds, object obj, Type type, string prefix)
        {
            // fill properties
            foreach (string columnName in ds)
            {
                string mappingName;
                mappingName = columnName.ToUpper();
                if (!String.IsNullOrEmpty(prefix))
                {
                    if (mappingName.StartsWith(prefix.ToUpper()))
                    {
                        mappingName = mappingName.Substring(prefix.Length);
                    }
                }

                if (String.IsNullOrEmpty(prefix))
                {
                    prefix = string.Empty;
                }

                PropertyDataBindingInfo propertyBindingInfo = GetPropertyInfo(type, mappingName);
                if (propertyBindingInfo == null || columnName.ToUpper() != (prefix.ToUpper() + mappingName))
                {
                    continue;
                }

                if (ds[columnName] != DBNull.Value && ValidateData(propertyBindingInfo, ds[columnName]))
                {
                    propertyBindingInfo.PropertyInfo.SetValue(obj, GetPropertyValue(propertyBindingInfo, ds[columnName], type), null);
                }
            }

            // fill referenced objects
            List <ReferencedTypeBindingInfo> refList = GetReferenceObjects(type);

            foreach (ReferencedTypeBindingInfo refObj in refList)
            {
                if (TryFill(ds, refObj))
                {
                    refObj.PropertyInfo.SetValue(obj, BuildEntity(ds, refObj.Type, refObj.Prefix), null);
                }
            }
        }
Exemplo n.º 3
0
        private static object GetPropertyValue(PropertyDataBindingInfo propertyBindingInfo, object val, Type type)
        {
            if (propertyBindingInfo.PropertyInfo.PropertyType == typeof(string))
            {
                return(val.ToString().Trim());
            }

            // check the rate
            if (propertyBindingInfo.PropertyInfo.PropertyType == typeof(decimal) && propertyBindingInfo.DataMapping.CaculatorType != null)
            {
                IMoneyCalculator cal = GetMoneyCalculator(propertyBindingInfo.DataMapping.CaculatorType);

                if (cal != null)
                {
                    val = cal.Calculate((decimal)val);
                }
                else
                {
                    throw new Exception("XFramework.DB实例对象Mapping时异常,异常信息:" + string.Format("Mapping对象:{0};属性名称:{1};计算类型:{2}", type.FullName, propertyBindingInfo.PropertyInfo.Name, propertyBindingInfo.DataMapping.CaculatorType.FullName));
                }
            }
            return(val);
        }
Exemplo n.º 4
0
 private static bool ValidateData(PropertyDataBindingInfo bindingInfo, Object dbValue)
 {
     //todo: implements in AOP?
     return(true);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Validate data binding info.
 /// Note: type checking is skipped here.
 /// </summary>
 /// <param name="bindingInfo"></param>
 /// <param name="dbValue"></param>
 /// <returns></returns>
 private static bool ValidateData(PropertyDataBindingInfo bindingInfo, object value)
 {
     return(true);
 }