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.(因为这是一个学术目的项目)

我希望我的问题是清楚的;实际上我想知道的是实体类是如何用来建立模型的?或者我们可以单独使用Model类吗?

MVP中的模型和实体

第二种方法更好。模型不应该与实体模型耦合。您需要将实体模型的每个属性映射到模型(AdvanceInfo)。这可以用

一行的代码手动完成
Advance entityModel = new Advance();
AdvanceInfo viewModel = new AdvanceInfo()
{
    AdvanceAmount = entityModel.AdvanceAmount,
    AdvanceEnteredDate = entityModel.AdvanceEnteredDate
    // etc.
};

或者您可以使用AutoMapper之类的框架为您完成映射