2015-03-25 48 views
1

MVC的新手,並在多個模型具有相同屬性時尋找最佳實踐。例如,我有幾個模型具有地址字段(地址,城市,州和郵政編碼)。假設我有這些領域的客戶模型和供應商模型。創建模型的最佳方式是什麼?我開始與第一例子有:創建具有相同屬性的數據模型的最佳做法

public class Customer 
{ 
    public int Id { get; set; } 
    public string AccountNumber { get; set; } 
    public string Customer { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string ZipCode { get; set; } 
    public string MainPhone { get; set; } 
    public string fax { get; set; } 
    public string ContactPhone { get; set; } 
} 

public class Supplier 
{ 
    public int Id { get; set; } 
    public string AccountNumber { get; set; } 
    public string Supplier { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string ZipCode { get; set; } 
    public string MainPhone { get; set; } 
    public string fax { get; set; } 
    public string ContactPhone { get; set; } 
} 

或交替:

public class Customer 
{ 
    public int Id { get; set; } 
    public string AccountNumber { get; set; } 
    public string Customer { get; set; } 
    public Address CustomerAddress { get; set; } 
    public Phone MainPhone { get; set; } 
    public Phone fax { get; set; } 
    public Phone ContactPhone { get; set; } 
} 

public class Supplier 
{ 
    public int Id { get; set; } 
    public string AccountNumber { get; set; } 
    public string Supplier { get; set; } 
    public Address SupplierAddress { get; set; } 
    public Phone MainPhone { get; set; } 
    public Phone fax { get; set; } 
    public Phone ContactPhone { get; set; } 
} 

public class Address 
{ 
    public string AddressLine1 { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string ZipCode { get; set; } 
} 

public class Phone 
{ 
    public int AreaCode { get; set; } 
    public int NPA { get; set; } 
    public int Station { get; set; } 
} 

什麼被認爲是最好的做法是非常讚賞

回答

1

我不知道,這是任何不同任何建議對於MVC比其他任何東西,但IMO,第二個好得多。當你決定添加工作地址和家庭地址時,就像你已經有了幾個電話號碼一樣,你不需要想出更多人爲的名字來區分。可重用代碼的所有標準原因仍然適用。

+0

謝謝你的回答! – Don 2015-03-25 22:58:47

相關問題