public Dictionary <UbjectMetadata, object> GetObjectsIncMetadata(Type type, List <Tuple <string, string, string> > filterProperties = null) { Dictionary <UbjectMetadata, object> ubjects = new Dictionary <UbjectMetadata, object>(); IDbTableManager databaseTable = DbManager.GetDbTableManagerForObject(type, false); if (databaseTable != null) { ubjects = databaseTable.GetObjects(type, filterProperties); if (ubjects.Count > 0) { var objectAnatomyDefinition = GetObjectAnatomyDefinition(ubjects.Values.First()); foreach (var collectionObject in objectAnatomyDefinition.CollectionReferenceTypeObjects) { IEnumerator enumerator = collectionObject.Value.GetEnumerator(); Type collectionItemType = enumerator.GetType().GetGenericArguments()[0]; IDbTableManager childDatabaseTable = DbManager.GetDbTableManagerForObject(collectionItemType, false); if (childDatabaseTable != null) { foreach (KeyValuePair <UbjectMetadata, object> ubject in ubjects) { List <Tuple <string, string, string> > mappingFilterCondition = new List <Tuple <string, string, string> >(); mappingFilterCondition.Add(new Tuple <string, string, string>(parentTableKey, databaseTable.TableName, "=")); mappingFilterCondition.Add(new Tuple <string, string, string>(parentObjectKey, ubject.Key.PrimaryKey.ToString(), "=")); mappingFilterCondition.Add(new Tuple <string, string, string>(childTableKey, childDatabaseTable.TableName, "=")); List <UbjectMapping> ubjectMappings = GetObjects <UbjectMapping>(mappingFilterCondition); List <object> childObjects = new List <object>(); foreach (var ubjectMapping in ubjectMappings) { List <Tuple <string, string, string> > childObjectFilterCondition = new List <Tuple <string, string, string> >(); childObjectFilterCondition.Add(new Tuple <string, string, string>(UbjectMetadata.PRIMARY_KEY, ubjectMapping.ChildObjectKey, "=")); childObjects.AddRange(GetObjectsIncMetadata(collectionItemType, childObjectFilterCondition).Values); } PropertyInfo propertyInfo = type.GetProperty(collectionObject.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); MethodInfo methodInfo = propertyInfo.PropertyType.GetMethod("Add"); object collectionInstance = Activator.CreateInstance(propertyInfo.PropertyType); foreach (object childObject in childObjects) { methodInfo.Invoke(collectionInstance, new object[] { childObject }); } propertyInfo.SetValue(ubject.Value, collectionInstance, null); } } } } } return(ubjects); }
public void RemoveObject(object data, List <string> filterProperties) { Dictionary <UbjectMetadata, object> objects = new Dictionary <UbjectMetadata, object>(); IDbTableManager databaseTable = DbManager.GetDbTableManagerForObject(data.GetType()); if (databaseTable != null) { objects = databaseTable.GetObjects(data.GetType(), CreateCompleteFilterFromPropertyAndData(data, filterProperties)); if (objects.Count > 0) { var objectAnatomyDefinition = GetObjectAnatomyDefinition(objects.Values.First()); foreach (var collectionObject in objectAnatomyDefinition.CollectionReferenceTypeObjects) { IEnumerator enumerator = collectionObject.Value.GetEnumerator(); Type collectionItemType = enumerator.GetType().GetGenericArguments()[0]; IDbTableManager mappingDatabaseTable = DbManager.GetDbTableManagerForObject(typeof(UbjectMapping), false); IDbTableManager childDatabaseTable = DbManager.GetDbTableManagerForObject(collectionItemType, false); if ((mappingDatabaseTable != null) && (childDatabaseTable != null)) { foreach (KeyValuePair <UbjectMetadata, object> ubject in objects) { List <Tuple <string, string, string> > mappingFilterCondition = new List <Tuple <string, string, string> >(); mappingFilterCondition.Add(new Tuple <string, string, string>(parentTableKey, databaseTable.TableName, "=")); mappingFilterCondition.Add(new Tuple <string, string, string>(parentObjectKey, ubject.Key.PrimaryKey.ToString(), "=")); mappingFilterCondition.Add(new Tuple <string, string, string>(childTableKey, childDatabaseTable.TableName, "=")); Dictionary <UbjectMetadata, UbjectMapping> objectMappings = GetObjectsIncMetadata <UbjectMapping>(mappingFilterCondition); List <UbjectMetadata> childObjectsMetadata = new List <UbjectMetadata>(); foreach (var objectMapping in objectMappings.Values) { List <Tuple <string, string, string> > childObjectFilterCondition = new List <Tuple <string, string, string> >(); childObjectFilterCondition.Add(new Tuple <string, string, string>(UbjectMetadata.PRIMARY_KEY, objectMapping.ChildObjectKey, "=")); List <UbjectMetadata> ubjectMetadata = new List <UbjectMetadata>(GetObjectsIncMetadata(collectionItemType, childObjectFilterCondition).Keys); childObjectsMetadata.AddRange(ubjectMetadata); } childObjectsMetadata.ForEach(x => childDatabaseTable.DeleteObject(x)); objectMappings.Keys.ToList().ForEach(x => mappingDatabaseTable.DeleteObject(x)); } } } objects.Keys.ToList().ForEach(x => databaseTable.DeleteObject(x)); } } }
public Tuple <string, string> PersistObject(object data, List <string> filterProperties = null) { string primaryKey = string.Empty; IDbTableManager databaseTable = DbManager.GetDbTableManagerForObject(data.GetType()); if (databaseTable != null) { bool objectUpdated = false; var objectAnatomyDefinition = GetObjectAnatomyDefinition(data); if (filterProperties != null) { var objects = databaseTable.GetObjects(data.GetType(), CreateCompleteFilterFromPropertyAndData(data, filterProperties)); if (objects.Count == 1) { databaseTable.UpdateObject(objectAnatomyDefinition.PropertiesAndValues, objects.Keys.First().PrimaryKey); objectUpdated = true; } else if (objects.Count != 0) { throw new Exception(string.Format("Multiple existing objects found while attempting to persist object. Filter: {0} Number of objects returned: {1}", string.Join(", ", filterProperties), objects.Count)); } } if (!objectUpdated) { primaryKey = databaseTable.AddObject(objectAnatomyDefinition.PropertiesAndValues).ToString(); } foreach (KeyValuePair <string, ICollection> collectionObject in objectAnatomyDefinition.CollectionReferenceTypeObjects) { foreach (var collectionItem in collectionObject.Value) { Tuple <string, string> childObjectKey = PersistObject(collectionItem); PersistObject(new UbjectMapping(databaseTable.TableName, primaryKey, childObjectKey.Item1, childObjectKey.Item2)); } } } return(new Tuple <string, string>(databaseTable.TableName, primaryKey)); }