示例#1
0
        /// <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);
        }
        public void AddEntry_NoItem_ThrowsException()
        {
            var            entry         = new ChangeTrackerEntry();
            IChangeTracker changeTracker = new ChangeTracker();

            Should.Throw <Exception>(() => changeTracker.AddEntry(entry));
        }
示例#3
0
        /// <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);
        }
        public void ModifiedObjects_HaveBeenAdded_ReturnEntries()
        {
            var myObject = new object();
            var entry    = new ChangeTrackerEntry
            {
                Item  = myObject,
                State = ChangeTrackerEntry.States.Modified
            };
            IChangeTracker changeTracker = new ChangeTracker();

            changeTracker.AddEntry(entry);

            changeTracker.ModifiedObjects.Single().ShouldBe(myObject);
        }
        public void AddEntry_HasItem_ContainsEntry()
        {
            var myObject = new object();
            var entry    = new ChangeTrackerEntry
            {
                Item = myObject
            };
            IChangeTracker changeTracker = new ChangeTracker();

            changeTracker.AddEntry(entry);

            changeTracker.Count.ShouldBe(1);
            changeTracker.Entries.Single().Item.ShouldBe(myObject);
        }
        public void GetEntryByObject_EntryExists_CorrectEntry()
        {
            var myObject = new object();
            var entry    = new ChangeTrackerEntry
            {
                Item = myObject
            };
            IChangeTracker changeTracker = new ChangeTracker();

            changeTracker.AddEntry(entry);

            var retrievedEntry = changeTracker.GetEntry(myObject);

            retrievedEntry.ShouldBe(entry);
            retrievedEntry.Item.ShouldBe(myObject);
        }