private bool IsEqualTo(IBusinessObject businessObject) { Type t = this.GetType(); ComparableCache[] cache = BusinessObject.PropertiesComparableCache[t]; for (int i = 0; i < cache.Length; i++) { ComparableCache c = cache[i]; object ourValue = c.Property.GetValue(this, null); object otherValue = c.Property.GetValue(businessObject, null); Type type = c.Property.PropertyType; if (type == typeof(Boolean)) { if ((Boolean)ourValue != (Boolean)otherValue) { return(false); } } else if (type == typeof(String)) { if (String.Compare((string)ourValue, (string)otherValue, StringComparison.Ordinal) != 0) { return(false); } } else if (type == typeof(Decimal)) { if (!((decimal)ourValue).Equals((decimal)otherValue)) { return(false); } } else if (type == typeof(Decimal?)) { Decimal?ourDecimal = (Decimal?)ourValue; Decimal?otherDecimal = (Decimal?)otherValue; if (ourDecimal != null && otherDecimal == null || ourDecimal == null && otherDecimal != null) { return(false); } if (ourDecimal != null && otherDecimal != null && ourDecimal.Value != otherDecimal.Value) { return(false); } } else if (type == typeof(Int32)) { if (!((int)ourValue).Equals((int)otherValue)) { return(false); } } else if (type == typeof(Int32?)) { Int32?ourInt = (Int32?)ourValue; Int32?otherInt = (Int32?)otherValue; if (ourInt != null && otherInt == null || ourInt == null && otherInt != null) { return(false); } if (ourInt != null && otherInt != null && ourInt.Value != otherInt.Value) { return(false); } } else if (type == typeof(Guid)) { if ((Guid)ourValue != (Guid)otherValue) { return(false); } } else if (type == typeof(Guid?)) { Guid?ourGuid = (Guid?)ourValue; Guid?otherGuid = (Guid?)otherValue; if (ourGuid != null && otherGuid == null || ourGuid == null && otherGuid != null) { return(false); } if (ourGuid != null && otherGuid != null && ourGuid.Value != otherGuid.Value) { return(false); } } else if (type == typeof(DateTime)) { if ((DateTime)ourValue != ((DateTime)otherValue)) { return(false); } } else if (type == typeof(DateTime?)) { DateTime?ourDateTime = (DateTime?)ourValue; DateTime?otherDateTime = (DateTime?)otherValue; if (ourDateTime != null && otherDateTime == null || ourDateTime == null && otherDateTime != null) { return(false); } if (ourDateTime != null && otherDateTime != null && ourDateTime.Value != otherDateTime.Value) { return(false); } } else if (type == typeof(XElement)) { XElement ourXElement = (XElement)ourValue; XElement otherXElement = (XElement)otherValue; if (ourXElement != null && otherXElement == null || ourXElement == null && otherXElement != null) { return(false); } if (ourXElement != null && otherXElement != null && ourXElement.ToString() != otherXElement.ToString()) { return(false); } } else if (typeof(IBusinessObject).IsAssignableFrom(type)) { IBusinessObject ourObject = (IBusinessObject)ourValue; IBusinessObject otherObject = (IBusinessObject)otherValue; if (ourObject != null && otherObject == null || ourObject == null && otherObject != null) { return(false); } if (ourObject != null && otherObject != null && ourObject.Id.Value != otherObject.Id.Value) { return(false); } } else if (type.IsEnum) { if ((int)ourValue != (int)otherValue) { return(false); } } else { throw new InvalidOperationException("Unknown type to compare"); } } return(true); }
private static void CacheClass(Type t) { if (!BusinessObject.IsClassCached.ContainsKey(t)) { bool isObjectToCache = false; // get the class attributes object[] obj = t.GetCustomAttributes(typeof(XmlSerializableAttribute), true); if (obj != null && obj.Length == 1) { XmlSerializableAttribute attr = (XmlSerializableAttribute)obj[0]; BusinessObject.ClassXmlSerializationCache.Add(t, new XmlSerializationCache() { Attribute = attr }); isObjectToCache = true; } obj = t.GetCustomAttributes(typeof(DatabaseMappingAttribute), true); if (obj != null && obj.Length > 0) { DatabaseMappingCache[] cache = new DatabaseMappingCache[obj.Length]; for (int i = 0; i < obj.Length; i++) { DatabaseMappingAttribute attr = (DatabaseMappingAttribute)obj[i]; cache[i] = new DatabaseMappingCache() { Attribute = attr }; } BusinessObject.ClassDatabaseMappingCache.Add(t, cache); isObjectToCache = true; } if (isObjectToCache) { //get properties XmlSerializableAttribiute LinkedList <XmlSerializationCache> xmlSerializableCacheList = new LinkedList <XmlSerializationCache>(); foreach (PropertyInfo propertyInfo in t.GetProperties()) { obj = propertyInfo.GetCustomAttributes(typeof(XmlSerializableAttribute), true); if (obj != null && obj.Length == 1) { XmlSerializableAttribute attr = (XmlSerializableAttribute)obj[0]; XmlSerializationCache cache = new XmlSerializationCache() { Attribute = attr, Property = propertyInfo }; if (attr.ProcessLast) { xmlSerializableCacheList.AddLast(cache); } else { xmlSerializableCacheList.AddFirst(cache); } } } XmlSerializationCache[] xmlCache = new XmlSerializationCache[xmlSerializableCacheList.Count]; int u = 0; foreach (XmlSerializationCache c in xmlSerializableCacheList) { xmlCache[u++] = c; } BusinessObject.PropertiesXmlSerializationCache.Add(t, xmlCache); //get properties ComparableAttribiute List <ComparableCache> comparableCacheList = new List <ComparableCache>(); foreach (PropertyInfo propertyInfo in t.GetProperties()) { obj = propertyInfo.GetCustomAttributes(typeof(ComparableAttribute), true); if (obj != null && obj.Length == 1) { ComparableAttribute attr = (ComparableAttribute)obj[0]; ComparableCache cache = new ComparableCache() { Attribute = attr, Property = propertyInfo }; comparableCacheList.Add(cache); } } BusinessObject.PropertiesComparableCache.Add(t, comparableCacheList.ToArray()); //get properties DatabaseMappingCache List <DatabaseMappingCache> databaseMappingCacheList = new List <DatabaseMappingCache>(); foreach (PropertyInfo propertyInfo in t.GetProperties()) { obj = propertyInfo.GetCustomAttributes(typeof(DatabaseMappingAttribute), true); if (obj != null && obj.Length > 0) { foreach (object objAttr in obj) { DatabaseMappingAttribute attr = (DatabaseMappingAttribute)objAttr; DatabaseMappingCache cache = new DatabaseMappingCache() { Attribute = attr, Property = propertyInfo }; databaseMappingCacheList.Add(cache); } } } BusinessObject.PropertiesDatabaseMappingCache.Add(t, databaseMappingCacheList.ToArray()); BusinessObject.IsClassCached.Add(t, true); } } }