在開始我的解釋之前,我看了一下其他類似的問題,但細微差別(主要是設計目的)意味着這些答案中提供的解決方案不適用於我。
我正在嘗試創建一個「基礎數據訪問庫」以供將來的項目使用,這樣我就不必花時間在多個項目中編寫相同的邏輯。其目的是簡單地導入這個庫,並立即擁有從IBaseDao繼承的能力,並立即擁有標準的「CRUD」能力。我無法確切知道我的業務對象會是什麼(Customer,FileStructure,Cat,Dog,Order等)我已經在我的基礎庫中定義了一個接口,其中所有的業務對象必須在它們可以與這個庫一起使用之前執行。在「真正的」應用程序,我打算有類似下面的東西所以現在
public interface IEntity
{
/// Indicates weather this entity is used as test
/// data or if it is a real-world entity.
bool IsMockObject { get; set; }
/// This is not intended for use in a 'real' application
/// and is only used in testing.
string ReadableName { get; set; }
/// The unique identifier for this object.
Guid Id { get; set; }
}
:
public class Customer : IEntity
{
public Customer()
{
}
string name = "";
public virtual String Name
{
get
{
return name;
}
set
{
name = value;
}
}
private DateTime birthday = DateTime.Now;
public virtual DateTime Birthday
{
get;
set;
}
private List<Order> orders = new List<Order>();
public virtual List<Order> Orders
{
get
{
return orders;
}
set
{
orders = value;
}
}
#region IEntity Members
private bool isMockObject;
public virtual bool IsMockObject
{
get
{
return true;
}
set
{
isMockObject = value;
}
}
private string readableName;
public virtual string ReadableName
{
get
{
return readableName;
}
set
{
readableName = value;
}
}
private Guid id;
public virtual Guid Id
{
get
{
return id;
}
set
{
id = value;
}
}
#endregion
}
現在這裏是哪裏的問題出現這樣的接口定義如下。在我的單元測試期間,當我嘗試並保存「客戶」時,出現參數計數不匹配異常。我懷疑這個問題的原因是我的數據庫中的Customer表不包含所有IEntity屬性的列。但它確實包含一個Id列。到目前爲止,我CustomerMap看起來是這樣的:
public class CustomerMap : ClassMap<Customer>
{
public CustomerMap()
{
Table("Customer");
// From IEntity
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Birthday);
// From IEntity
Map(x => x.IsMockObject);
// From IEntity
Map(x => x.ReadableName);
HasMany(x => x.Orders);
}
}
我真正想要的NHibernate辦時節省保存客戶表中,所有獨特的Customer
還有IEntity.Id
財產的屬性,然後保存在一個單獨的表稱爲(如果IsMockEntity爲true)MockEntitiy模擬對象的Id及其可讀的名稱。
我發現ClassMap很難理解,因爲我對NHibernate的基本持久性做了很少的工作。任何幫助或鏈接相關材料大大讚賞。
謝謝你的時間。
我相當肯定有一個與類映射,因爲當我看看什麼NHibernate的實際上是試圖做的,它試圖在「IsMockObject」和「ReadableName」值添加到客戶表中的問題而不是從Customer對象中取出'Id',並將其放在'MockEntities'表中的'ReadableName'旁邊。我可以看到它爲什麼試圖這樣做,因爲CustomerMap的ClassMap沒有提到MockEntites表。 – user407356 2010-08-30 11:58:36