c# - 用于多个查找表的 MVC 核心存储库模式
问题描述
我们有带有多个带有外键的查找表的客户事务表。当 CustomerService 创建客户订单事务时,我们希望使用这些查找表创建下拉菜单。如果有人稍后查看事务,他们会看到 4 个表连接在一起。
我会创造,
(a) 4 个接口与 4 个存储库,
(b) 还是 2 个接口(1 个用于客户事务,1 个接口用于查找表),1 个用于客户事务的存储库,3 个用于查找表接口的存储库?
我们想将 Lookup table Repository 中继到下面的 SelectList。每个选择列表都在选择某些列。想要在代码中高效。
楷模:
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusKey {get; set; }, //joins to StatusTypeTable
public int CustomerTypeId {get; set; } //joins to CustomerTypeTable
public string DateOfPurchase{ get; set; },
public string PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public int Size { get; set; }
public int Weight { get; set; },
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusKey{ get; set; }
public string Description{ get; set; },
public string Symbol { get; set; },
}
public class CustomerType
{
public int KeyNumber{ get; set; },
public int Height{ get; set; }
public string HairColor{ get; set; },
public string NameOfPerson{ get; set; },
public string ResearchNotes{ get; set; },
}
下拉列表中的必填字段
ViewData["ProductTypeId"] = new SelectList(_context.ProductType, "ProductName", "ProductDescription");
ViewData["KeyNumber"] = new SelectList(_context.CustomerType , "NameofPerson", "Description");
ViewData["StatusKey"] = new SelectList(_context.StatusType, "Symbol", "ResearchNotes");
解决方案
您可以创建一个数据传输对象 (DTO) 来满足您的前端需求。
public class EditTransaction {
CustomerTransaction customerTransaction { get; set; }
SelectList productTypes { get; set; }
SelectList statusType { get; set; }
SelectList customerTypes { get; set; }
}
包括访问 DAL 存储库并组装 DTO 的类/方法/控制器/任何东西。您的客户端代码获取 DTO。存储库和 UI 中没有什么“特别”的。
推荐阅读
- c++ - for_each 算法导致 basic_string::_M_construct null 在 C++ 中无效
- scala - Scala 数字类型的隐式转换是否特殊?
- bash - 在从“find | xargs”运行的命令中使用参数扩展来防止输出覆盖
- php - 内网 PHP 页面上的文件 URL “不允许加载本地资源”
- s4sdk - CloudLoggerFactory 的 Sanitized Logger 在 Veracode Scan 中显示 CRLF 注入漏洞
- javascript - ReactJS - 如何从 json-server 迁移到 .json 文件?
- javascript - 循环遍历 json 对象并提取数据 - js
- android - Android Places API findCurrentPlace ApiException
- tfs - 在 TFS 2018 中积压转换后运行脚本
- css - lessc 的配置文件