首页 > 解决方案 > 用于多个查找表的 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");

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

解决方案


您可以创建一个数据传输对象 (DTO) 来满足您的前端需求。

public class EditTransaction {
    CustomerTransaction customerTransaction { get; set; }
    SelectList productTypes { get; set; }
    SelectList statusType { get; set; }
    SelectList customerTypes { get; set; }
}

包括访问 DAL 存储库并组装 DTO 的类/方法/控制器/任何东西。您的客户端代码获取 DTO。存储库和 UI 中没有什么“特别”的。


推荐阅读