/// <summary> /// Updates database and creates all created objects /// </summary> private void CreateObjects() { foreach (var type in CreatedObjects) { foreach (PersistentObject createdObject in type.Value) { // extract all relations in objects to create and add them to the changed // objects List <PropertyWrapper> relationFields = WrappingHandler.GetClassWrapper(createdObject.GetType()).GetRelationWrapper(); foreach (PropertyWrapper relation in relationFields) { string relationMemberName = relation.OriginalField.Name; object o = createdObject.GetMemberValue(relationMemberName); if (o != null && !(o is GenericList)) { AddChangedObject(createdObject, relationMemberName, o); // createdObject.SetMemberValue(relationMemberName, null); } } connection.Create(createdObject); } } }
/// <summary> /// Loads all objects of a type (always loads from DB) /// </summary> /// <typeparam name="T">Type to load</typeparam> /// <param name="clause">the clause to apply while loading from DB</param> /// <returns>a list of <see cref="PersistentObject"/>s</returns> public List <T> GetObjects <T>(WhereClause clause) where T : PersistentObject { ClassWrapper classWrapper = WrappingHandler.GetClassWrapper(typeof(T)); List <T> objectsToReturn = new List <T>(); DataTable table = connection.GetTable(classWrapper, clause); foreach (DataRow row in table.Rows) { objectsToReturn.Add((T)LoadObject(classWrapper, row)); } ObjectCache.ApplyLoadedObjectsToCache(); return(objectsToReturn); }
/// <summary> /// Loads all objects of a type /// </summary> /// <typeparam name="T">Type to load</typeparam> /// <param name="loadFromDB">determines if object can be loaded from database</param> /// <returns>a list of <see cref="PersistentObject"/>s</returns> public List <T> GetObjects <T>(bool loadFromDB = false) where T : PersistentObject { if (loadFromDB) { RefreshType(WrappingHandler.GetClassWrapper(typeof(T))); ObjectCache.ApplyLoadedObjectsToCache(); } List <T> castedList = new List <T>(); foreach (PersistentObject obj in ObjectCache.Get(typeof(T))) { castedList.Add((T)obj); } return(castedList.Count > 0 ? castedList : null); }
/// <summary> /// Gets one object of type <paramref name="type"/> /// </summary> /// <param name="type">type of object to get</param> /// <param name="id">ID of object</param> /// <param name="loadFromDB">determines if object can be loaded from database</param> /// <returns>instance of found object of type <typeparamref name="T"/></returns> public PersistentObject GetObject(Type type, Guid id, bool loadFromDB = false) { object objToReturn = default; // check cache foreach (PersistentObject obj in ObjectCache.Get(type)) { if (obj.ID.Equals(id)) { objToReturn = obj; break; } } if (objToReturn == null && loadFromDB) { ClassWrapper clsWrapper = WrappingHandler.GetClassWrapper(type); DataRow row = connection.GetObject(clsWrapper, id); objToReturn = LoadObject(clsWrapper, row); } return(objToReturn as PersistentObject); }