【问题标题】:MVC Core Bind ViewModel to ModelMVC Core 将 ViewModel 绑定到模型
【发布时间】:2018-05-22 06:05:07
【问题描述】:

我们有带有多个外键查找表的客户事务表。我们希望看到 3 个表连接在一起并将 ViewModel 绑定回 Model 并反向。我听说存储库不应访问 ViewModel。

(一) 执行此操作的正确软件协议是什么?我还不想使用 AutoMapper。我应该创建一个数据访问对象或服务,有人可以在下面为我写一个快速示例吗? 我应该在 MVC 中创建另一个名为 Data Service 的文件夹吗?我买了 3 本 MVC 书籍,没有一本讨论 DTO 或绑定模型 视图模型。 谢谢,

存储库:

void GetByCustomerTransactionId()
{
   var result = from ct in CustomerTransaction
    join pt in ProductType on pt.ProductTypeId equals ct.ProductTypeId 
    join ss in Status on s.StatusId equals ct.StatusId 
    select new all fields
}

型号:

public class CustomerTransaction
{
    public int CustomerTransactionId{ get; set; },
    public int ProductTypeId {get; set; }, //joins to ProductTypeTable
    public int StatusID {get; set; },  //joins to StatusTypeTable
    public string DateOfPurchase{ get; set; },
    public int PurchaseAmount { get; set; },
}

public class ProductType
{
    public int ProductTypeId{ get; set; }
    public string ProductName { get; set; },
    public string ProductDescription { get; set; },
}

public class StatusType
{
    public int StatusId{ get; set; }
    public string StatusName{ get; set; },
    public string Description{ get; set; },

}

视图模型:

public class CustomerTransactionViewModel
    {
        public int CustomerTransactionId{ get; set; },
        public string ProductName {get; set; }, //joins to ProductTypeTable
        public string ProductDescription {get; set; }, 
        public string StatusName {get; set; },  //joins to StatusTable
        public string DateOfPurchase{ get; set; },
        public int PurchaseAmount { get; set; },
    }

【问题讨论】:

  • DTO 和 ViewModel 本质上是相同的,只是 ViewModel 有更多的用途和行为(即在 WPF 等桌面技术或 MVC/WebApi 的验证属性上使用时)。在某一时刻,您必须对其进行映射。手动或使用 AutoMapper 等工具
  • 另外请记住,您只能使用 Automapper 将 Model 绑定到 ViewModel(或 MOdel -> DTO,或 Domain Model -> DTO/ViewModel),而不是相反。 AutoMapper 不适合绑定到持久性或域模型,仅适用于它们。在相反的情况下,您自己组合模型(使用 new 关键字并适当组合对象并验证它们的状态)
  • 你能写一个快速映射函数吗?它只是一个公共无效功能吗?谢谢,
  • 2个月前开始编程,所以努力学习MVC
  • @BlueLamp82 在我看来,您的问题不止一个。两件事-这是什么void GetByCustomerTransactionId()(为什么无效???)我看到您的问题中已经有ViewModel。你不满意还是什么?

标签: c# asp.net-mvc asp.net-core


【解决方案1】:

你有没有研究过AutoMapper 是将数据库对象映射到视图模型。是相当整洁干净的方式。

如果不想使用 AutoMapper,可以考虑使用静态方法。

var student = new Student { FirstName = "John", LastName = "Doe" };
var mdoel = student.ToDTO();

public static StudentViewModel ToDTO(this Student student)
{
    var model = new StudentViewModel();
    model.FullName = $"{student.FirstName} {student.LastName}";
    return model;
}

希望对你有所帮助

【讨论】:

  • 你也可以提出我的问题吗?由于某种原因被否决,谢谢-
【解决方案2】:
 public class CustomerTransactionViewModel : IViewModel
{
    public int CustomerTransactionId{ get; set; },
    public string ProductName {get; set; }, //joins to ProductTypeTable
    public string ProductDescription {get; set; }, 
    public string StatusName {get; set; },  //joins to StatusTable
    public string DateOfPurchase{ get; set; },
    public int PurchaseAmount { get; set; },
  // Every view model should always have mapper from dbmodel to vm.
  private void MaptoEntity(Entity e)
  {
    this.CustomerTransactionId = e.ID
    .....
    // this is also a repository that mapping db to view model.
    var prod = new ProductTypeViewModel().Load(e.ProductID);
    this.ProductName = prod.ProductName;
    this.ProductDescription = prod.ProductDescription;
    // so on.......
  }

  public bool Load(int id)
  {
   // Call data from DB.
    var entity = dbcontext.Entity.Find(id);
   // Map you from DB.
    this.MaptoEntity(entity)
  }
}

更好的结构是创建一个需要所有实现的接口 IViewModel。

现在你可以打电话了

var customerViewModel = new CustomerTransactionViewModel();
customerViewModel .Load(1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    相关资源
    最近更新 更多