2015-03-25 79 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

相关问题