gpt4 book ai didi

asp.net-mvc - 模型绑定(bind)器可以进行存储库查找吗?

转载 作者:行者123 更新时间:2023-12-02 01:33:53 24 4
gpt4 key购买 nike

我正在构建一个 MVC 应用程序,并为一个类设计一个自定义模型绑定(bind)器;本质上,模型的字段之一是存在于数据库中的对象,但事实证明很难将其与 HTML 中的适当对象关联起来。 (因为选择列表只能让我选择一个 int/string 字段,所以我真的无法将“对象”存储为选择列表的“值”)。

我正在考虑使用存储在选择列表中的 Id 来查找 Model Binder 中数据库中的对象 - 但我的一位同事告诉我这通常是一个坏主意。这是真的吗?如果是,我还有什么其他选择?

最佳答案

似乎是一个主观问题,但我认为在 Binder 中调用存储库是可以接受的。我的备份是一本优秀的书MVC in Action 2 。他们有一个关于模型绑定(bind)器的简短部分。下面是讨论在 Binder 中调用数据库的想法的引用(强调):

Most of the time, this action parameter is the primary key of the object or another unique identifier, so instead of putting this repeated data access code in all our actions, we can use a custom model binder that can load the stored object before the action is executed. Our action can then take the persisted object type as a parameter instead of the unique identifier.

如果您认为模型绑定(bind)器的全部意义是将您的 View 映射到底层域模型,那么这是有道理的。他们的示例代码也演示了 Binder 中的存储库调用。

关于asp.net-mvc - 模型绑定(bind)器可以进行存储库查找吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4159446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com