Exemplo n.º 1
0
 internal static void DeleteEntity(Credit item, SqlConnection conn)
 {
     using (SqlCommand cmd = conn.CreateCommand())
     {
         cmd.CommandType = System.Data.CommandType.Text;
         cmd.CommandText = "delete Credit where CreditId = @CreditId";
         cmd.Parameters.AddWithValue("@CreditId", item.CreditId);
         cmd.ExecuteNonQuery();
     }
 }
Exemplo n.º 2
0
        internal static void InsertEntity(Credit item, SqlConnection conn)
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandType = System.Data.CommandType.Text;
                var sql = new StringBuilder();
                sql.Append("insert Credit (ShowId, PersonId, CreditTypeId, Character)");
                sql.Append("values ( @ShowId, @PersonId, @CreditTypeId, @Character);");
                sql.Append("select cast ( scope_identity() as int);");
                cmd.CommandText = sql.ToString();

                SetCommonParameters(item, cmd);

                item.CreditId = (int)cmd.ExecuteScalar();
            }
        }
Exemplo n.º 3
0
        internal static void UpdateEntity(Credit item, SqlConnection conn)
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandType = System.Data.CommandType.Text;
                var sql = new StringBuilder();
                sql.Append("update Credit set ");
                sql.Append(" ShowId = @ShowId, ");
                sql.Append(" PersonId = @PersonId, ");
                sql.Append(" CreditTypeId = @CreditTypeId, ");
                sql.Append(" Character = @Character ");
                sql.Append("where CreditId = @CreditId ");
                cmd.CommandText = sql.ToString();

                SetCommonParameters(item, cmd);
                cmd.Parameters.AddWithValue("@CreditId", item.CreditId);

                cmd.ExecuteNonQuery();
            }
        }
Exemplo n.º 4
0
 internal static Credit Persist(Credit item)
 {
     if (item.CreditId == 0 && item.IsMarkedForDeletion) return null;
     if (item.CreditId == 0)
     {
         // Insert
         var maxId = FakeDatabase.Instance.Credits.Max(o => o.CreditId);
         item.CreditId = ++maxId;
         var row = MapObjToRow(item);
         FakeDatabase.Instance.Credits.Add(row);
     }
     else
     {
         var existingRow = FakeDatabase.Instance.Credits
             .FirstOrDefault(o => o.CreditId == item.CreditId);
         if(existingRow == null)
         {
             throw new ApplicationException(
                 "Record not found, another user may have deleted it.");
         }
         else if(item.IsMarkedForDeletion)
         {
             // Delete
             FakeDatabase.Instance.Credits.Remove(existingRow);
         }
         else
         {
             // Update
             existingRow.ShowId = item.ShowId;
             existingRow.PersonId = item.PersonId;
             existingRow.CreditTypeId = item.CreditTypeId;
             existingRow.Character = item.Character;
         }
     }
     // reset dirty flag because obj is now synced with db
     if (item != null) item.IsDirty = false;
     return item;
 }
Exemplo n.º 5
0
 public Credit PersistChild(Credit credit, SqlConnection conn)
 {
     if (credit.CreditId == 0 && credit.IsMarkedForDeletion)
     {
         credit = null;
     }
     else if (credit.IsMarkedForDeletion)
     {
         DeleteEntity(credit, conn);
         credit = null;
     }
     else if (credit.CreditId == 0)
     {
         InsertEntity(credit, conn);
         credit.IsDirty = false;
     }
     else if (credit.IsDirty)
     {
         UpdateEntity(credit, conn);
         credit.IsDirty = false;
     }
     return credit;
 }
Exemplo n.º 6
0
        public void PersonRepository_CreditDirty_SetsGraphDirty()
        {
            // Arrange
            var repo = new PersonRepository();
            var all = repo.Fetch(null).ToList();
            var personId = all[0].PersonId;
            var fullName = all[0].FullName;

            var item = repo.Fetch(personId).Single();

            // Add one Credit to change a leaf
            // of the object graph

            var creditTypeRepository = new CreditTypeRepository();
            var ct = creditTypeRepository.Fetch().First();
            var showRepository = new ShowRepository();
            var s = showRepository.Fetch().First();
            var c = new Credit()
            {
                CreditTypeId = ct.CreditTypeId,
                ShowId = s.ShowId
            };

            item.Credits.Add(c);

            Assert.IsNotNull(item);
            Assert.IsTrue(item.PersonId == personId);
            Assert.IsTrue(item.FullName == fullName);
            Assert.IsFalse(item.IsMarkedForDeletion);

            // The IsDirty flag should be false
            Assert.IsFalse(item.IsDirty);

            // The HasChanges property should
            // be true, indicating the change to ShowGenres
            Assert.IsTrue(item.IsGraphDirty);
        }
Exemplo n.º 7
0
 private static Credit MapRowToObj(CreditRow row)
 {
     var existingItem = new Credit
     {
         CreditId = row.CreditId,
         ShowId = row.ShowId,
         PersonId = row.PersonId,
         CreditTypeId = row.CreditTypeId,
         Character = row.Character
     };
     existingItem.IsDirty = false;
     return existingItem;
 }
Exemplo n.º 8
0
 private static CreditRow MapObjToRow(Credit item)
 {
     return new CreditRow
            {
                CreditId = item.CreditId,
                ShowId = item.ShowId,
                PersonId = item.PersonId,
                CreditTypeId = item.CreditTypeId,
                Character = item.Character
            };
 }
Exemplo n.º 9
0
        private Person CreateSamplePerson()
        {
            var newPerson = new Person
            {
                Salutation = "",
                FirstName = "Jane",
                MiddleName = "Anne",
                LastName = "TestLastName",
                Suffix = "",
                StageName = "",
                Weight = 120.5,
                DateOfBirth = new DateTime(1943, 2, 5),
                HairColorId = 1,
                EyeColorId = 1
            };

            var creditTypeRepo = new CreditTypeRepository();
            var creditTypes = creditTypeRepo.Fetch(null).ToList();

            var showRepo = new ShowRepository();
            var shows = showRepo.Fetch(null).ToList();

            Credit crd1 = new Credit
            {
                ShowId = shows[0].ShowId,
                CreditTypeId = creditTypes[0].CreditTypeId,
                Character = "Henry"
            };
            newPerson.Credits.Add(crd1);

            return newPerson;
        }
Exemplo n.º 10
0
 private static void SetCommonParameters(Credit item, SqlCommand cmd)
 {
     cmd.Parameters.AddWithValue("@ShowId", item.ShowId);
     cmd.Parameters.AddWithValue("@PersonId", item.PersonId);
     cmd.Parameters.AddWithValue("@CreditTypeId", item.CreditTypeId);
     cmd.Parameters.AddWithValue("@Character",
             item.Character.AsSqlParameterValue());
 }