/// <summary> /// Used by the query provider to materialize database objects /// </summary> /// <typeparam name="T"></typeparam> /// <param name="builder"></param> /// <returns></returns> public List <T> GetObjectsFromDb <T>(ISqlStatementBuilder builder) { var selectResults = _dbDriver.RunSelectStatement(builder.SelectStatement); var objects = new List <T>(); foreach (var result in selectResults) { var myObject = MaterializeObject <T>(result); var id = GetId(myObject); var possiblyExistingObject = ChangeTracker.GetEntry(id, typeof(T))?.Item; var objectIsNew = possiblyExistingObject == null; if (!objectIsNew) { objects.Add((T)possiblyExistingObject); continue; } var changeTrackerEntry = new ChangeTrackerEntry { State = ChangeTrackerEntry.States.Unmodified, Item = myObject, }; changeTrackerEntry.UpdateOriginals(myObject); ChangeTracker.AddEntry(changeTrackerEntry); objects.Add(myObject); } return(objects); }
/// <summary> /// Used to add objects to the ORM /// </summary> /// <param name="objectToInsert"></param> public void Insert(object objectToInsert) { SetId(objectToInsert, --_insertionId); var changeTrackerEntry = new ChangeTrackerEntry { State = ChangeTrackerEntry.States.Inserted, Item = objectToInsert, }; changeTrackerEntry.UpdateOriginals(objectToInsert); ChangeTracker.AddEntry(changeTrackerEntry); }