public WPOTableObject(WPOBaseObject obj) { WPOObject = obj; TableName = obj.GetTableName(); Inheritance = obj.GetInheritanceType(); PrimaryKey = obj.GetPrimaryKey(); if (Inheritance == InheritanceType.ClassTable) { WPOTableObject exsistObj = ExsistTableObjects.FirstOrDefault(e => e.GetType() == obj.GetType().BaseType); BaseTable = exsistObj ?? new WPOTableObject(obj.GetType().BaseType.CreateModelObj()); } }
private void InformChildrenAboutMyDeath() // Sets own reference to null in every object that is in relation with this object { var childrenTypes = this.GetPropertiesByAttribute <WPORelationAttribute>(); foreach (var childType in childrenTypes) { var attribute = childType.GetAttribute <WPORelationAttribute>(); WPOBaseObject child = GetPropertyValue(childType) as WPOBaseObject; // Remove in one to one relation if (attribute.RelationType == RelationType.OneToOne) { // Set reference to this object in child to null var propertyInfo = child.GetAllColumns().Single(prop => GetColumnName(prop) == attribute.ForeignKey); WPOBaseObject property = GetPropertyValue(propertyInfo) as WPOBaseObject; property = null; } // remove in one to many relation, from single object position else { var relatedCollection = child.GetWPOCollection(this.GetType()); if (relatedCollection != null) { int index = (relatedCollection as List <WPOBaseObject>).FindIndex(x => x.ObjectGuid == ObjectGuid); if (index >= 0) { (relatedCollection as List <WPOBaseObject>).RemoveAt(index); Type wpoCollectionType = typeof(WPOCollection <>).MakeGenericType(new Type[] { GetType() }); MethodInfo castMethod = relatedCollection.GetType().GetMethod("Cast", BindingFlags.Instance | BindingFlags.Public).MakeGenericMethod(GetType()); var castCollectionInstance = Activator.CreateInstance(wpoCollectionType, castMethod.Invoke(relatedCollection, new object[] { relatedCollection })); child.GetWPOCollectionType(GetType()).SetValue(child, castCollectionInstance); } } } } //Remove relation from objects in WPOCollections var collectionTypes = GetType().GetProperties().Where(prop => prop.PropertyType.IsGenericType && prop.PropertyType.Name.Contains("WPOCollection")); foreach (var collectionType in collectionTypes) { var collection = GetPropertyValue(collectionType) as IEnumerable <WPOBaseObject>; if (collection != null) { foreach (var obj in collection) { obj.GetType().GetProperties().Single(prop => obj.GetPropertyValue(prop) == this).SetValue(obj, null); } } } }
internal void Register(WPOBaseObject wpoObject) { if (wpoObject == null) { throw new NullReferenceException(nameof(wpoObject)); } if (objectsAll == null) { objectsAll = new HashSet <WPOBaseObject>(); } if (!objectsAll.Contains(wpoObject)) { objectsAll.Add(wpoObject); } }
// Turning the object properties into dictionary with positions as column name and value private Dictionary <string, Tuple <object, bool> > CreateDiffrentsDictionary(WPOBaseObject obj, bool onlyChangedPositions, string baseClassName = null) { Dictionary <string, Tuple <object, bool> > result = new Dictionary <string, Tuple <object, bool> >(); if (onlyChangedPositions) { WPOBaseObject originalObj = GetOriginalObject(obj); foreach (var property in obj.GetAllColumnsWithoutRelations(baseClassName)) { string columnName = obj.GetColumnName(property); object value = obj.GetPropertyColumnValue(property); object originalValue = originalObj.GetPropertyValueByColumnName(columnName); if ((value == null ^ originalValue == null) || (originalValue != null && !originalValue.Equals(value))) { result.Add(columnName, Tuple.Create(value, false)); } } foreach (var property in obj.GetAllRelations()) { object value = obj.GetForgeinKeyValue(property); object originalValue = originalObj.GetForgeinKeyValue(property); if ((value == null ^ originalValue == null) || (originalValue != null && !originalValue.Equals(value))) { result.Add(obj.GetColumnName(property), Tuple.Create(value, true)); } } } else { foreach (var property in obj.GetAllColumnsWithoutRelations(baseClassName)) { result.Add(obj.GetColumnName(property), Tuple.Create(obj.GetPropertyColumnValue(property), false)); } foreach (var property in obj.GetAllRelations().Where(prop => obj.GetPropertyValue(prop) != null)) { result.Add(obj.GetColumnName(property), Tuple.Create(obj.GetForgeinKeyValue(property), true)); } } return(result); }
private WPOBaseObject GetOriginalObject(WPOBaseObject obj) { return(objectsFromDatabase.FirstOrDefault(x => x.ObjectGuid == obj.ObjectGuid)); }