0
這可能是一個品味的問題,但創造的ViewModels當是去與多個公共類,如下面的最佳實踐(牢記這3類服務只是一個單一的視圖):MVC視圖模型範圍
namespace WebApp.Areas.Commerce.Models.ViewModels
{
public class TravellersViewModel
{
public TicketHolder TicketHolder { get; set; }
public List<Traveller> TicketMembers { get; set; }
public int MaxAge { get; set; }
public bool ShowAddress { get; set; }
}
public class TicketHolder
{
public string EmailAddress { get; set; }
public string TelephoneNumber { get; set; }
public string MobileNumber { get; set; }
public string Address1 { get; set; }
public string Postcode { get; set; }
public string Country { get; set; }
}
public class Traveller
{
public int Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
public bool Policyholder { get; set; }
public Traveller()
{
Title = "Mr";
}
}
}
或者是最好巢單一視圖模型
namespace WebApp.Areas.Commerce.Models.ViewModels
{
public class TravellersViewModel
{
public TicketHolderDetails TicketHolder { get; set; }
public List<Traveller> TicketMembers { get; set; }
public int MaxAge { get; set; }
public bool ShowAddress { get; set; }
public class TicketHolderDetails
{
public string EmailAddress { get; set; }
public string TelephoneNumber { get; set; }
public string MobileNumber { get; set; }
public string Address1 { get; set; }
public string Postcode { get; set; }
public string Country { get; set; }
}
public class Traveller
{
public int Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
public bool Policyholder { get; set; }
public Traveller()
{
Title = "Mr";
}
}
}
}
我很喜歡建立視圖模型,因爲它使一切togther的第二種方式的範圍之內的子類。
可能重複[c#公共嵌套類或更好的選項?](http://stackoverflow.com/questions/7984529/c-sharp-public-nested-classes-or-better-option) –
嗨丹尼斯,你可能是正確的,但ViewModel類與普通類稍有不同,更簡單,我認爲有足夠的差異來保證單獨的問題/答案 – BrightonDev
不,沒有足夠的差異。分離類是分離類。 –