Beispiel #1
0
        public void GetPrimaryKeyAttribute_PrimaryKey_ReturnsCorrectProperty()
        {
            var property = OrmUtilities.GetPrimaryKeyProperty(typeof(PersonWithPrimaryKey));

            property.ShouldNotBeNull();
            property.Name.ShouldBe("MyPrimaryKey");
            property.PropertyType.ShouldBe(typeof(int));
        }
        /// <inheritdoc />
        public ChangeTrackerEntry GetEntry(int id, Type type)
        {
            var idProperty = OrmUtilities.GetPrimaryKeyProperty(type);

            return(_entries.Values.
                   FirstOrDefault(entry => entry.Item.GetType() == type &&
                                  idProperty.GetValue(entry.Item).Equals(id)));
        }
Beispiel #3
0
        private void SubmitModifiedEntries()
        {
            var modifiedObjects = ChangeTracker.ModifiedObjects;

            foreach (var modifiedObject in modifiedObjects)
            {
                _sqlBuilder.TableName = OrmUtilities.GetTableName(modifiedObject.GetType());
                _sqlBuilder.Columns   = OrmUtilities.GetColumns(modifiedObject);
                _sqlBuilder.IdName    = OrmUtilities.GetPrimaryKeyProperty(modifiedObject.GetType()).Name;

                _dbDriver.RunUpdateStatement(_sqlBuilder.UpdateStatement);

                ChangeTracker.GetEntry(modifiedObject).State = ChangeTrackerEntry.States.Unmodified;
                ChangeTracker.GetEntry(modifiedObject).UpdateOriginals(modifiedObject);
            }
        }
Beispiel #4
0
        private void SubmitInsertedEntries()
        {
            var insertedObjects = ChangeTracker.InsertedObjects;

            foreach (var objectToInsert in insertedObjects)
            {
                _sqlBuilder.TableName = OrmUtilities.GetTableName(objectToInsert.GetType());
                _sqlBuilder.Columns   = OrmUtilities.GetColumns(objectToInsert);
                _sqlBuilder.IdName    = OrmUtilities.GetPrimaryKeyProperty(objectToInsert.GetType()).Name;

                var newId = _dbDriver.RunInsertStatement(_sqlBuilder.InsertStatement);
                SetId(objectToInsert, newId);

                ChangeTracker.GetEntry(objectToInsert).UpdateOriginals(objectToInsert);
                ChangeTracker.GetEntry(objectToInsert).State = ChangeTrackerEntry.States.Unmodified;
            }
        }
Beispiel #5
0
        private static void SetId(object objectToSet, int id)
        {
            var idProperty = OrmUtilities.GetPrimaryKeyProperty(objectToSet.GetType());

            idProperty.SetValue(objectToSet, id);
        }
Beispiel #6
0
        private static int GetId(object obj)
        {
            var idProperty = OrmUtilities.GetPrimaryKeyProperty(obj.GetType());

            return((int)idProperty.GetValue(obj));
        }
Beispiel #7
0
 public void GetPrimaryKeyAttribute_MultiplePrimaryKeys_ThrowsException()
 {
     Should.Throw <Exception>(
         () => OrmUtilities.GetPrimaryKeyProperty(typeof(PersonWithMultiplePrimaryKeys))
         );
 }
Beispiel #8
0
 public void GetPrimaryKeyAttribute_PrimaryKeyAsString_ThrowsException()
 {
     Should.Throw <Exception>(
         () => OrmUtilities.GetPrimaryKeyProperty(typeof(PersonWithPrimaryKeyAsString))
         );
 }