protected virtual void DataStore_Add_Item_Adds_Item() { // Arrange FooModel item = _dataStore.Create(); item.AutoPopulate(); // Act _dataStore.Add(item); _dataStore.SaveChanges(); var result = GetItemById(item.ID); // Assert Assert.IsNotNull(result); Assert.AreEqual(item.ID, result.ID); Assert.AreEqual(item.Title, result.Title); Assert.AreEqual(item.CreateDate.ToString(), result.CreateDate.Kind == DateTimeKind.Utc ? result.CreateDate.ToLocalTime().ToString() : result.CreateDate.ToString()); Assert.AreEqual(item.Content, result.Content); if (_dataStore.SupportsComplexStructures) { Assert.IsNotNull(result.Comments); Assert.AreEqual(item.Comments.Count, result.Comments.Count); try { Assert.AreEqual(item.Comments.OrderBy(c => c.ID).FirstOrDefault(), result.Comments.OrderBy(c => c.ID).FirstOrDefault()); } catch { Assert.AreEqual(item.Comments.OrderBy(c => c.ID).FirstOrDefault().ID, result.Comments.OrderBy(c => c.ID).FirstOrDefault().ID); Assert.AreEqual(item.Comments.OrderBy(c => c.ID).FirstOrDefault().Author, result.Comments.OrderBy(c => c.ID).FirstOrDefault().Author); Assert.AreEqual(item.Comments.OrderBy(c => c.ID).FirstOrDefault().Message, result.Comments.OrderBy(c => c.ID).FirstOrDefault().Message); } } // cleanup CleanOutItemFromStore(item); }
protected virtual void DataStore_Query_For_Item_Returns_Result() { // Arrange FooModel item = _dataStore.Create(); item.AutoPopulate(); AddItemToStore(item); SaveChanges(); // Act var result = _dataStore.SingleOrDefault(it => it.ID == item.ID); // Assert Assert.IsNotNull(result); Assert.AreEqual(item.ID, result.ID); Assert.AreEqual(item.Title, result.Title); Assert.AreEqual(GetLocalDateString(item.CreateDate), GetLocalDateString(result.CreateDate)); Assert.AreEqual(item.Content, result.Content); if (_dataStore.SupportsComplexStructures) { Assert.IsNotNull(result.Comments); Assert.AreEqual(item.Comments.Count, result.Comments.Count); try { Assert.AreEqual(item.Comments.FirstOrDefault(), result.Comments.FirstOrDefault()); } catch { Assert.AreEqual(item.Comments.OrderBy(c => c.ID).FirstOrDefault().ID, result.Comments.OrderBy(c => c.ID).FirstOrDefault().ID); Assert.AreEqual(item.Comments.OrderBy(c => c.ID).FirstOrDefault().Author, result.Comments.OrderBy(c => c.ID).FirstOrDefault().Author); Assert.AreEqual(item.Comments.OrderBy(c => c.ID).FirstOrDefault().Message, result.Comments.OrderBy(c => c.ID).FirstOrDefault().Message); } } // cleanup CleanOutItemFromStore(item); }
protected override void AddItemToStore(FooModel item) { var conn = NewConnection(); var sql = @"INSERT INTO FooModel ( ID, CreateDate, Title, Content ) VALUES ( @ID, @CreateDate, @Title, @Content )"; var cmd = new SqlCommand(sql, conn); cmd.Parameters.Add("@ID", item.ID); cmd.Parameters.Add("@CreateDate", item.CreateDate); cmd.Parameters.Add("@Title", item.Title); cmd.Parameters.Add("@Content", item.Content); }
protected override FooModel GetItemById(long id) { using (var conn = NewConnection()) { var cmdtext = "SELECT * FROM FooModel WHERE ID = @ID"; var cmd = new SqlCommand(cmdtext, conn); var dr = cmd.ExecuteReader(); var hydrator = new EntityMemberResolver<FooModel>(); dr.Read(); var item = new FooModel(); hydrator.HydrateFromDictionary(item, hydrator.ConvertToDictionary(dr)); return item; } }
protected override void CleanOutItemFromStore(FooModel item) { using (var conn = NewConnection()) { var cmdtext = "DELETE FROM FooModel WHERE ID = @ID"; var cmd = new SqlCommand(cmdtext, conn); cmd.ExecuteNonQuery(); } }
/// <summary> /// Item is removed directly from the storage mechanism via <see cref="DataStore"/>.DataSet /// if the item exists. /// </summary> /// <param name="item"></param> protected abstract void CleanOutItemFromStore(FooModel item);
protected override void CleanOutItemFromStore(FooModel item) { ((IDictionary<string, FooModel>)DataStore.DataSet).Remove(item.ID.ToString()); }
/// <summary> /// Item is added directly to the storage mechanism via <see cref="DataStore"/>.DataSet. /// </summary> /// <param name="item"></param> protected abstract void AddItemToStore(FooModel item);
protected override void AddItemToStore(FooModel item) { ((IDictionary<string, FooModel>)DataStore.DataSet).Add(item.ID.ToString(), item); }
protected override void AddItemToStore(FooModel item) { _dbset.Add(item); }
protected override void CleanOutItemFromStore(FooModel item) { try { _dbset.Remove(item); } catch {} }