コード例 #1
0
ファイル: CBO.cs プロジェクト: oscarllop/CollectorsClub
        private static void HydrateObject(object hydratedObject, IDataReader dr)
        {
            PropertyInfo objPropertyInfo = null;
            Type         propType        = null;
            object       coloumnValue;
            Type         objDataType;
            int          intIndex;
            //get cached object mapping for type
            ObjectMappingInfo objMappingInfo = GetObjectMapping(hydratedObject.GetType());

            if (hydratedObject is BaseEntityInfo && !(hydratedObject is ScheduleItem))
            {
                //Call the base classes fill method to populate base class properties
                ((BaseEntityInfo)hydratedObject).FillBaseProperties(dr);
            }
            //fill object with values from datareader
            for (intIndex = 0; intIndex <= dr.FieldCount - 1; intIndex++)
            {
                //If the Column matches a Property in the Object Map's PropertyInfo Dictionary
                if (objMappingInfo.Properties.TryGetValue(dr.GetName(intIndex).ToUpperInvariant(), out objPropertyInfo))
                {
                    //Get its type
                    propType = objPropertyInfo.PropertyType;
                    //If property can be set
                    if (objPropertyInfo.CanWrite)
                    {
                        //Get the Data Value from the data reader
                        coloumnValue = dr.GetValue(intIndex);
                        //Get the Data Value's type
                        objDataType = coloumnValue.GetType();
                        if (coloumnValue == null || coloumnValue == DBNull.Value)
                        {
                            //set property value to Null
                            objPropertyInfo.SetValue(hydratedObject, Null.SetNull(objPropertyInfo), null);
                        }
                        else if (propType.Equals(objDataType))
                        {
                            //Property and data objects are the same type
                            objPropertyInfo.SetValue(hydratedObject, coloumnValue, null);
                        }
                        else
                        {
                            //business object info class member data type does not match datareader member data type
                            //need to handle enumeration conversions differently than other base types
                            if (propType.BaseType.Equals(typeof(Enum)))
                            {
                                //check if value is numeric and if not convert to integer ( supports databases like Oracle )
                                if (Globals.NumberMatchRegex.IsMatch(coloumnValue.ToString()))
                                {
                                    objPropertyInfo.SetValue(hydratedObject,
                                                             Enum.ToObject(propType, Convert.ToInt32(coloumnValue)),
                                                             null);
                                }
                                else
                                {
                                    objPropertyInfo.SetValue(hydratedObject, Enum.ToObject(propType, coloumnValue), null);
                                }
                            }
                            else if (propType == typeof(Guid))
                            {
                                //guid is not a datatype common across all databases ( ie. Oracle )
                                objPropertyInfo.SetValue(hydratedObject,
                                                         Convert.ChangeType(new Guid(coloumnValue.ToString()), propType),
                                                         null);
                            }
                            else if (propType == typeof(Version))
                            {
                                objPropertyInfo.SetValue(hydratedObject, new Version(coloumnValue.ToString()), null);
                            }
                            else if (coloumnValue is IConvertible)
                            {
                                objPropertyInfo.SetValue(hydratedObject, ChangeType(coloumnValue, propType), null);
                            }
                            else
                            {
                                // try explicit conversion
                                objPropertyInfo.SetValue(hydratedObject, coloumnValue, null);
                            }
                        }
                    }
                }
            }
        }