使用实体框架预加载聚合根

发布于 2024-07-23 08:25:41 字数 1245 浏览 5 评论 0原文

我想创建一种更结构化的方法来加载所需的实体树:

我需要大量数据,因此我使用类型安全的包含(只是一个普通的包含,但使用 Lambda 的)来执行此操作 如此处所示

正如我所说,我需要大量数据,基本上是 1 个父项下的整个实体树。

现在,我可以这样做:

context.House
    .Include(x => x.Doors)
    .Include(x => x.Doors.FirstOrDefault().Joint)
    .Include(x => x.Doors.FirstOrDefault().Joint.FirstOrDefault().JointCategory)
    .Include(x => x.Doors.FirstOrDefault().Joint.FirstOrDefault().JointCategory.JointType)
    .Include(x => x.Windows)
    // ... same thing
    .Include(x => x.Roof)
    // ... same thing

如您所见,这一行包含包含内容可能会变得相当大。 这实际上是实际代码的一个非常简化的示例(顺便说一句,其中不包括房屋)

所以我想做的是创建方法,负责其在树中的分支。 该方法可以接受对象查询并包含子对象,然后调用“子加载器方法”。 此外,父级应该不重要,只要它具有子级类型的属性即可。

这可能没有多大意义,所以:

public void LoadHouse(int id)
{
    // ...
    ObjectQuery<House> query = context.House;

    // and now?
    LoadDoors(query, x => x.Door);

}

public void LoadDoors<T>(ObjectQuery<T> query, ..?..)
{
    // ... ?

    LoadJoints(...)


}

等等。 但我无法真正理解它......传入查询和调用子方法之间缺少链接。

有人做过这样的事吗? 或者有人可以给我一些指示吗?

I would like to create a more structured approach to loading the needed entity-tree:

I need a serious amount of data, so I'm doing this using type-safe Includes (just a normal Include but with Lambda's) as shown here.

As I said, I need a lot of data, basically a whole entity tree under 1 parent item.

Now, I could do this doing something like:

context.House
    .Include(x => x.Doors)
    .Include(x => x.Doors.FirstOrDefault().Joint)
    .Include(x => x.Doors.FirstOrDefault().Joint.FirstOrDefault().JointCategory)
    .Include(x => x.Doors.FirstOrDefault().Joint.FirstOrDefault().JointCategory.JointType)
    .Include(x => x.Windows)
    // ... same thing
    .Include(x => x.Roof)
    // ... same thing

As you can see, this line filled with includes can get quite huge. This is in fact a very simplified sample of the actual code (which doesn't include houses btw)

So what I would like to do is creating methods, responsible for its branch in the tree. Where the method can accept the object query and include the child, and in its turn, call the "child-loader methods". Also, the parent shouldn't matter, as long as it has a property with the type of the child.

This probably does not make much sense so:

public void LoadHouse(int id)
{
    // ...
    ObjectQuery<House> query = context.House;

    // and now?
    LoadDoors(query, x => x.Door);

}

public void LoadDoors<T>(ObjectQuery<T> query, ..?..)
{
    // ... ?

    LoadJoints(...)


}

And so on. But I can't really get my head around it... There's a missing link between the incoming query and calling the child methods.

Has anyone done something like this? Or could anyone give me some pointers?

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

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

发布评论

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

评论(1

网白 2024-07-30 08:25:41

尝试这样的事情:

query = LoadDoors(query, x => x.Door);

其中 LoadX 返回调用 Include 的结果。

Try something like this instead:

query = LoadDoors(query, x => x.Door);

Where LoadX returns the result of calling Include.

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