单个与多个Linq2sql存储库的对比[英] Single vs. multiple Linq2sql repositories

本文是小编为大家收集整理的关于单个与多个Linq2sql存储库的对比的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个用户表、事件表和一个用户事件映射.在我的代码的某些部分,我只需要基于用户的东西.在其他部分,我需要所有这些信息.(特别是:给定一个用户,他们订阅的每个事件的详细信息是什么?)

如果我有一个仅用于用户的存储库,而另一个用于用户 + 事件 + 用户事件的存储库,则自动创建的用户对象将被复制,并且在我重命名其中一个之前,代码不会编译.这是可能的,但不方便.另一方面,如果我只有一个包含所有 3 个表的存储库,当我只需要用户信息时,由于 linq 获取具有该用户 ID 的所有关联数据,是否会很昂贵?

在 Linq2Sql 中,如果您在单个 dbml/repository 中有更多表,是否会更昂贵?

推荐答案

Linq2Sql 使用延迟加载来获取附加信息.我相信它可以配置为一次获取​​所有内容,但这不是默认行为.如果您请求用户,除非您特别请求,否则您将不会收到事件.

本文地址:https://www.itbaoku.cn/post/1557212.html

问题描述

I have a Users table, Events table, and a mapping of UserEvents. In some parts of my code, I just need user-based stuff. In other parts, I need all of this information. (Especially: given a user, what are the details of each event they are subscribed to?)

If I have one repository just for users and another for users + events + userevents, then the auto-created users object is duplicated and the code won't compile until I rename one of them. This is possible but inconvenient. On the other hand, if I only have one repository with all 3 tables, when I just want user info, will it be expensive due to linq getting all the associated data with that user id?

In Linq2Sql, is it more expensive if you have more tables in a single dbml/repository?

推荐答案

Linq2Sql uses lazy loading to get additional information. I believe it can be configured to fetch all at once, but that is not the default behavior. If you ask for a user, you will not get events unless you specifically ask for them.