Linq to Sql、存储库和 Asp.Net MVC ViewData:如何删除冗余?

发布于 2024-08-27 09:56:28 字数 893 浏览 4 评论 0原文

Linq to SQL 创建可 IQueryable 且充满关系的对象。
Html Helpers 需要特定的界面对象,例如 IEnumerable

我认为可能会发生什么:

  • 重用从 Linq 到 SQL 的对象而不带所有包袱,即从 Linq 到 SQL 对象返回 Pocos,而不需要额外的域模型类?
  • 提取可以轻松转换为(或本身)Html 辅助对象(例如 SelectListItem 枚举)的对象?

有没有办法在不破坏关注点分离的情况下做到这一点?一些巧妙的oop技巧来桥接需求?

例如,如果它位于存储库中,则 SelectListItem 将不存在。 select new 是一种从 Linq to SQL 中删除对象的好方法,但它仍然引用了一个不应该引用的类:

 IEnumerable<SelectListItem> result = (from record in db.table
                                       select new SelectListItem {
                                           Selected = record.selected,
                                           Text= record.Text, 
                                           Value= record.Value }
                                      ).AsEnumerable();

Linq to SQL creates objects which are IQueryable and full of relations.
Html Helpers require specific interface objects like IEnumerable<SelectListItem>.

What I think could happen:

  • Reuse the objects from Linq to SQL without all the baggage, i.e., return Pocos from the Linq to SQL objects without additional Domain Model classes?
  • Extract objects that easily convert to (or are) Html helper objects like the SelectListItem enumeration?

Is there any way to do this without breaking separation of concerns? Some neat oop trick to bridge the needs?

For example, if this were within a repository, the SelectListItem wouldn't be there. The select new is a nice way to cut out an object from the Linq to SQL without the baggage but it's still referencing a class that shouldn't be referenced:

 IEnumerable<SelectListItem> result = (from record in db.table
                                       select new SelectListItem {
                                           Selected = record.selected,
                                           Text= record.Text, 
                                           Value= record.Value }
                                      ).AsEnumerable();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女净妖师 2024-09-03 09:56:28

我见过IService接口和Service类的用户。基本上这是存储库和控制器之间的步骤。

优点:

  • 允许使用操作 IRepository 操作的返回值的方法
  • 良好的关注点分离
  • 可以成为涉及存储库之前验证逻辑所在的桥梁。
  • 可以使用依赖注入框架/模式

缺点:

  • 大量重复代码。除非您公开存储库,否则您可能会在 IService 中复制几乎所有 IRepository 方法,
  • 从而成为“包罗万象”的类。无所不能的神级! Muahahaha...等等

我确信还有更多,但这是我见过和使用过的。

I have seen the user of an IService interface and Service class. Basically it's the step between the repository and the controller.

Pros:

  • Allows for methods that manipulate the return values of IRepository actions
  • Good separation of concerns
  • Can be the bridge where the validation logic lives before the repository is involved.
  • Can work with Dependency injection frameworks/patterns

Cons:

  • Mucho duplication of code. Unless you expose the repository, you will probably be duplicating almost all of your IRepository methods in the IService
  • Can become a "catch-all" class. The god class that does everything! Muahahaha... etc

I'm sure there are more, but that's something I've seen and used.

┾廆蒐ゝ 2024-09-03 09:56:28

在此特定示例中,您可以在分部类定义中创建 ToSelectListItem() 方法或类似方法以删除重复项。或者也许扩展方法会更好地避免那里的一些耦合。

编辑:我通常将 JSON Web 服务与 DTO 一起使用。我可能有 ProductDto,它是我的 Product L2SQL 实体的扁平 JSON 友好表示。然后我只需在查询中调用 select ProductDto.FromProduct(product) 即可。现在重复已经很少了。

In this specific example, you can create a ToSelectListItem() method or similar in your partial class definition to remove duplication. Or maybe an extension method would be better to avoid some coupling there.

Edit: I generally use JSON web services with DTOs. I might have ProductDto that is my flattened JSON-friendly representation of my Product L2SQL entity. Then I just have to call select ProductDto.FromProduct(product) in my query. The duplication is now minimal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文