2011-04-18 41 views
2

當我目前工作的一個項目,並選擇使用哈瓦那爲我的ORM是強制性的。我正在使用SmoothHabanero來設置我的業務對象。我與另一個需要至少有一個對象實例有效的類有關係。我將如何做到這一點?我怎麼能強制執行的關係用SmoothHabanero

回答

1

OK,這可能是有點詳細所以讓我先從一些基本知識。

平滑是哈瓦那ORM守則第一個社區項目。 Smooth基於Convention over Configuration方法。

你哈瓦那域對象不需要有一個外鍵屬性爲它正確地映射到只要你遵循某種公約的那光滑的可以找出數據庫。

通常

public class Customer : BusinessObject 
{ 
} 
public class SalesOrder : BusinessObject 
{ 
    /// <summary> 
    /// The type this SalesOrder is for. 
    /// </summary> 
    public virtual Customer Customer 
    { 
     get { return Relationships.GetRelatedObject<Customer>("Customer"); } 
     set { Relationships.SetRelatedObject("Customer", value); } 
    } 
} 

在這種情況下銷售訂單有一個客戶的單一關係。 客戶對銷售訂單是強制性的,即如果沒有設置客戶,銷售訂單就不能持續。

在域級別這一切是有道理的。 但是,在數據庫級別,使用SalesOrder表上的外鍵映射客戶關係。 如果您遵循CustomerID是SalesOrder表上的外鍵屬性的約定,那麼這就是您需要做的所有操作以獲取在域模型中設置的關係並正確映射到數據庫。

現在的回答你的問題。

如果Classes是根據上述定義的,那麼您只需添加'[AutoMapCompulsory]'屬性,Smooth將按照您的強制要求設置關係及其外鍵的所有必要設置。

public class Customer : BusinessObject 
{ 
} 
public class SalesOrder : BusinessObject 
{ 

    /// <summary> 
    /// The type this SalesOrder is for. 
    /// </summary> 
    [AutoMapCompulsory] 
    public virtual Customer Customer 
    { 
     get { return Relationships.GetRelatedObject<Customer>("Customer"); } 
     set { Relationships.SetRelatedObject("Customer", value); } 
    } 
} 

希望這有助於 佈雷特

+0

@Brett - 感謝這正是我之後。 – Andrew 2011-04-18 11:01:57