【发布时间】: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