Exemple #1
0
        private static ObjectMappingInfo GetObjectMapping(Type objType)
        {
            string cacheKey = objectMapCacheKey + objType.FullName;
            var    objMap   = (ObjectMappingInfo)DataCache.GetCache(cacheKey);

            if (objMap == null)
            {
                //Create an ObjectMappingInfo instance
                objMap            = new ObjectMappingInfo();
                objMap.ObjectType = objType.FullName;
                //Reflect on class to create Object Map
                objMap.PrimaryKey = GetPrimaryKey(objType);
                objMap.TableName  = GetTableName(objType);
                //Iterate through the objects properties and add each one to the ObjectMappingInfo's Properties Dictionary
                foreach (PropertyInfo objProperty in objType.GetProperties())
                {
                    objMap.Properties.Add(objProperty.Name.ToUpperInvariant(), objProperty);
                    objMap.ColumnNames.Add(objProperty.Name.ToUpperInvariant(), GetColumnName(objProperty));
                }
                //Persist to Cache
                DataCache.SetCache(cacheKey, objMap);
            }

            //Return Object Map
            return(objMap);
        }
Exemple #2
0
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CloneObject clones an object.
        /// </summary>
        /// <param name="objObject">The Object to Clone.</param>
        /// <returns></returns>
        /// -----------------------------------------------------------------------------
        public static object CloneObject(object objObject)
        {
            try
            {
                Type   objType      = objObject.GetType();
                object objNewObject = Activator.CreateInstance(objType);

                // get cached object mapping for type
                ObjectMappingInfo objMappingInfo = GetObjectMapping(objType);
                foreach (KeyValuePair <string, PropertyInfo> kvp in objMappingInfo.Properties)
                {
                    PropertyInfo objProperty = kvp.Value;
                    if (objProperty.CanWrite)
                    {
                        // Check if property is ICloneable
                        var objPropertyClone = objProperty.GetValue(objObject, null) as ICloneable;
                        if (objPropertyClone == null)
                        {
                            objProperty.SetValue(objNewObject, objProperty.GetValue(objObject, null), null);
                        }
                        else
                        {
                            objProperty.SetValue(objNewObject, objPropertyClone.Clone(), null);
                        }

                        // Check if Property is IEnumerable
                        var enumerable = objProperty.GetValue(objObject, null) as IEnumerable;
                        if (enumerable != null)
                        {
                            var list = objProperty.GetValue(objNewObject, null) as IList;
                            if (list != null)
                            {
                                foreach (object obj in enumerable)
                                {
                                    list.Add(CloneObject(obj));
                                }
                            }

                            var dic = objProperty.GetValue(objNewObject, null) as IDictionary;
                            if (dic != null)
                            {
                                foreach (DictionaryEntry de in enumerable)
                                {
                                    dic.Add(de.Key, CloneObject(de.Value));
                                }
                            }
                        }
                    }
                }

                return(objNewObject);
            }
            catch (Exception exc)
            {
                Exceptions.LogException(exc);
                return(null);
            }
        }
Exemple #3
0
        public static ArrayList GetPropertyInfo(Type objType)
        {
            var arrProperties = new ArrayList();

            //get cached object mapping for type
            ObjectMappingInfo objMappingInfo = GetObjectMapping(objType);

            arrProperties.AddRange(objMappingInfo.Properties.Values);

            return(arrProperties);
        }
Exemple #4
0
        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);
                            }
                        }
                    }
                }
            }
        }