批准我有了這個測試代碼:編輯實體保存沒有我與NHibernate
int productId;
{
var codersAtWork = productService.Create();
codersAtWork.Title = "Coders At Work";
codersAtWork.Price = new Price(200, 160, 0.25m, "SEK");
codersAtWork.FieldBag.ReleaseDate = new DateTime(2009, 9, 1);
productService.Save(codersAtWork);
productId = codersAtWork.Id;
}
ReSetUp(); // Closes and opens a new session
{
var codersAtWork = productService.GetById(productId);
Assert.AreEqual(new DateTime(2009, 9, 1), codersAtWork.FieldBag.ReleaseDate);
codersAtWork.FieldBag.ReleaseDate = "Tomorrow";
Assert.Throws<InvalidFieldException>(() => { productService.Save(codersAtWork); });
var cleanCode = productService.Create();
cleanCode.Title = "Foo";
cleanCode.Price = new Price(300, 240, 0.25m, "SEK");
cleanCode.FieldBag.ReleaseDate = new DateTime(2009, 9, 1);
productService.Save(cleanCode); // This save will also save my incorrect product.
}
ReSetUp(); // Closes and opens a new session
{
var codersAtWork = productService.GetById(productId);
Assert.AreEqual(new DateTime(2009, 9, 1), codersAtWork.FieldBag.ReleaseDate);
}
我希望我的服務,以驗證我發微博,只保存它,如果它是有效的產品。但問題是,當我保存在同一會話中有效的其他內容或在會話上調用Flush時,它將被保存,而我無法控制其有效性。最後在這裏聲明將失敗。 product.FieldBag.ReleaseDate將是「明天」。
FieldBag是動態的,並保存爲Json到數據庫。但問題也適用於其他驗證。
燦NHibernate的是對