c# - 多个查找表的 MVC 核心存储库模式

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

我们的客户交易表包含多个带外键的查找表。当 CustomerService 创建客户订单交易时,我们希望使用这些查找表创建下拉菜单。如果一个人稍后查看交易,他们会看到 4 个表连接在一起。

我会创建,

(a) 4 个接口(interface)和 4 个存储库,

(b) 或 2 个接口(interface)(1 个用于客户交易,1 个接口(interface)用于查找表),1 个存储库用于客户交易,3 个存储库用于查找表接口(interface)?

我们想将 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; }
}

包括一个类/方法/ Controller /任何访问您的 DAL 存储库并组装 DTO 的东西。您的客户端代码获取 DTO。存储库和 UI 中没有任何“特殊”内容。

关于c# - 多个查找表的 MVC 核心存储库模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50456115/

相关文章:

c# - Asp.net Core - 不要在 await next.Invoke() ("green"中断时中断)

c# - ASP.NET MVC 6 中的 MVC Controller 和 Web API Controller 有什么区别?

c# - 如何在 .net 核心 JwtBearerOptions.Events 中使用依赖注入(inject)?

C# EMIT IL 性能问题

C# 如何计算我的 AppDomain 中的托管线程?

jQuery DataTable 无法在 asp.net mvc 4 中工作

c# - 在 .net 中显示文件上传进度

c# - 添加新的 XML 元素并仍然读取旧版本的 XML 文档

c# - Entity Framework 和类

asp.net-mvc - 将项目添加到 asp.net mvc 列表框客户端?