MVP中的模型和实体
本文关键字:实体 模型 MVP | 更新日期: 2023-09-27 18:05:35
我是MVP设计模式的初学者,在MVP三元组中,我有
Model - AdvanceInfo
View - AdvanceForm
Presenter - AdvancePresneter
和实体类 Advance
如下:
public class Advance
{
public int AdvanceID { get; set; }
public decimal AdvanceAmount { get; set; }
public DateTime AdvanceEnteredDate { get; set; }
public DateTime AdvanceProcessDate { get; set; }
}
所以我的模型应该如下…
class AdvanceInfo
{
public int AdvanceID { get { return Advance.AdvanceID; } }
public decimal AdvanceAmount { get { return Advance.AdvanceAmount; } set { Advance.AdvanceAmount = value; } }
public DateTime AdvanceEnteredDate { get { return Advance.AdvanceEnteredDate; } set { Advance.AdvanceEnteredDate = value; } }
public int EmployeeID { get; set; }
public string NameWithInitials { get; set; }
public string BankAccountName { get; set; }
public string BankAccountNumber { get; set; }
public Advance Advance = new Advance();
}
,否则……
class AdvanceInfo
{
public int AdvanceID { get; set; }
public decimal AdvanceAmount { get; set; }
public int EmployeeID { get; set; }
public string NameWithInitials { get; set; }
public string BankAccountName { get; set; }
public string BankAccountNumber { get; set; }
public DateTime AdvanceEnteredDate { get; set; }
public DateTime AdvanceProcessDate { get; set; }
}
注意:*我使用实体类Advance
作为DeductionInfo
类的聚合类。*我没有使用像EF这样的框架。只有VS.(因为这是一个学术目的项目)
第二种方法更好。模型不应该与实体模型耦合。您需要将实体模型的每个属性映射到模型(AdvanceInfo
)。这可以用
Advance entityModel = new Advance();
AdvanceInfo viewModel = new AdvanceInfo()
{
AdvanceAmount = entityModel.AdvanceAmount,
AdvanceEnteredDate = entityModel.AdvanceEnteredDate
// etc.
};
或者您可以使用AutoMapper之类的框架为您完成映射