private static IDictionary <TKey, TValue> FillDictionaryFromReader <TKey, TValue>(string keyField, IDataReader dr, IDictionary <TKey, TValue> objDictionary, bool closeReader) { TValue objObject; TKey keyValue = default(TKey); bool isSuccess = Null.NullBoolean; try { //iterate datareader while (dr.Read()) { //Create the Object objObject = (TValue)CreateObjectFromReader(typeof(TValue), dr, false); if (keyField == "KeyID" && objObject is IHydratable) { //Get the value of the key field from the KeyID keyValue = (TKey)Null.SetNull(((IHydratable)objObject).KeyID, keyValue); } else { //Get the value of the key field from the DataReader if (typeof(TKey).Name == "Int32" && dr[keyField].GetType().Name == "Decimal") { keyValue = (TKey)Convert.ChangeType(Null.SetNull(dr[keyField], keyValue), typeof(TKey)); } else if (typeof(TKey).Name.Equals("string", StringComparison.OrdinalIgnoreCase) && dr[keyField].GetType().Name.Equals("dbnull", StringComparison.OrdinalIgnoreCase)) { keyValue = (TKey)Convert.ChangeType(Null.SetNull(dr[keyField], ""), typeof(TKey)); } else { keyValue = (TKey)Convert.ChangeType(Null.SetNull(dr[keyField], ""), typeof(TKey)); } } //add to dictionary if (objObject != null) { objDictionary[keyValue] = objObject; } } isSuccess = true; } finally { //Ensure DataReader is closed if ((!isSuccess)) { closeReader = true; } CloseDataReader(dr, closeReader); } //Return the dictionary return(objDictionary); }
/// ----------------------------------------------------------------------------- /// <summary> /// InitializeObject initialises all the properties of an object to their /// Null Values. /// </summary> /// <param name="objObject">The object to Initialise</param> /// ----------------------------------------------------------------------------- public static void InitializeObject(object objObject) { //initialize properties foreach (PropertyInfo objPropertyInfo in GetObjectMapping(objObject.GetType()).Properties.Values) { if (objPropertyInfo.CanWrite) { objPropertyInfo.SetValue(objObject, Null.SetNull(objPropertyInfo), null); } } }
/// ----------------------------------------------------------------------------- /// <summary> /// InitializeObject initialises all the properties of an object to their /// Null Values. /// </summary> /// <param name="objObject">The object to Initialise</param> /// <param name="objType">The type of the object</param> /// ----------------------------------------------------------------------------- public static object InitializeObject(object objObject, Type objType) { //initialize properties foreach (PropertyInfo objPropertyInfo in GetObjectMapping(objType).Properties.Values) { if (objPropertyInfo.CanWrite) { objPropertyInfo.SetValue(objObject, Null.SetNull(objPropertyInfo), null); } } return(objObject); }
public static object InitializeObject(object objObject, Type objType) { // get properties for type ArrayList objProperties = GetPropertyInfo(objType); // initialize properties for (int intProperty = 0; intProperty < objProperties.Count; intProperty++) { PropertyInfo objPropertyInfo = (PropertyInfo)objProperties[intProperty]; if (objPropertyInfo.CanWrite) { object objValue = Null.SetNull(objPropertyInfo); objPropertyInfo.SetValue(objObject, objValue, null); } } return(objObject); }
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); } } } } } }
/// <summary> /// Generic version of CreateObject creates an object of a specified type from the /// provided DataReader /// </summary> /// <typeparam name="T">The type of the business object</typeparam> /// <param name="dr">The DataReader</param> /// <returns>The custom business object</returns> /// <remarks></remarks> private static T CreateObject <T>(IDataReader dr) { Type objPropertyType = null; T objObject = Activator.CreateInstance <T>(); // get properties for type ArrayList objProperties = GetPropertyInfo(objObject.GetType()); // get ordinal positions in datareader int[] arrOrdinals = GetOrdinals(objProperties, dr); // fill object with values from datareader for (int intProperty = 0; intProperty <= objProperties.Count - 1; intProperty++) { PropertyInfo objPropertyInfo = (PropertyInfo)objProperties[intProperty]; if (objPropertyInfo.CanWrite) { object objValue = Null.SetNull(objPropertyInfo); if (arrOrdinals[intProperty] != -1) { if (Information.IsDBNull(dr.GetValue(arrOrdinals[intProperty]))) { // translate Null value objPropertyInfo.SetValue(objObject, objValue, null); } else { try { // try implicit conversion first objPropertyInfo.SetValue(objObject, dr.GetValue(arrOrdinals[intProperty]), null); } catch { // business object info class member data type does not match datareader member data type try { objPropertyType = objPropertyInfo.PropertyType; //need to handle enumeration conversions differently than other base types if (objPropertyType.BaseType.Equals(typeof(Enum))) { // check if value is numeric and if not convert to integer ( supports databases like Oracle ) if (Information.IsNumeric(dr.GetValue(arrOrdinals[intProperty]))) { ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Enum.ToObject(objPropertyType, Convert.ToInt32(dr.GetValue(arrOrdinals[intProperty]))), null); } else { ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Enum.ToObject(objPropertyType, dr.GetValue(arrOrdinals[intProperty])), null); } } else { // try explicit conversion objPropertyInfo.SetValue(objObject, Convert.ChangeType(dr.GetValue(arrOrdinals[intProperty]), objPropertyType), null); } } catch { objPropertyInfo.SetValue(objObject, Convert.ChangeType(dr.GetValue(arrOrdinals[intProperty]), objPropertyType), null); } } } } else { // property does not exist in datareader } } } return(objObject); }