何时将扩展其他复杂类型添加到Breeze实体上 [英] When to add extend additional complex types onto a Breeze entity

查看:112
本文介绍了何时将扩展其他复杂类型添加到Breeze实体上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中加载一个实体,然后使用它的ICollection(来自模型类)加载一些子数据.如果这是一对多关系,那么对于单个视图模型来说,这将足够简单,但是我的结构要复杂一些-

父母有很多孩子.每个孩子都有许多孙子孙,需要将其链接回适当的孩子.层次结构需要保持完整.

到目前为止,我想出的其他选择可能不是最好的方法,所以我的问题是-装载孙子孙女的最佳做法是什么-或其他方法?

在配置metadataStore时在构造函数中-

function configureMetadataStore(metadataStore) {
    metadataStore.registerEntityTypeCtor(
        'Child', null, childInitializer);
}

function childInitializer(child) {
    child.grandchildren = (Do something here)
        return grandchildren;
    });
}

在填充了子代的viewmodel中-

function refresh() {
    return datacontext.getChildren(childs, parentId);
}

var addGrandChildren = function () {
    $.each(childs, function (i) {
        var grandChildren = ko.observableArray();
        var childId = $(this).data(id);
        datacontext.getGrandChildren(grandChildren, childId);
    });
    return;
};

还是其他方法?

解决方案

如果您的关系不是单向的,Breeze实体在查询时会自动将它们自身挂钩. (从1.3.5版开始-微风也将连接单向关系.)

这意味着,如果您使用查询来提取n个刚好相关的实体,则所有这些实体都将以正确的方式自动链接到彼此.如果您使用EntityQuery.expand方法,则会发生相同的情况.因此,您的问题很简单,就是如何在最少的调用次数中查询所需的图的任何部分.

注意:如果您确实要真正遍历"图形,则还应该查看EntityAspect.loadNavigationProperty方法.但是,如果您要处理大型图形,这可能会导致性能下降.

I want to load an entity from the database, and then using it's ICollection (from the model class) load up some child data. This would be simple enough to do from individual view models if it was a 1 to many relationship, but I have a bit more complex structure -

Parent has many children. Each child has many grandchildren, that need to be linked back to the proper child. The hierarchy needs to remain in tact.

The other options that I have come up with so far may not be the best way so I my question is - what is the best practice to load up the grandchildren - or some other method?

in a constructor while configuring the metadataStore -

function configureMetadataStore(metadataStore) {
    metadataStore.registerEntityTypeCtor(
        'Child', null, childInitializer);
}

function childInitializer(child) {
    child.grandchildren = (Do something here)
        return grandchildren;
    });
}

In the viewmodel where children are being populated -

function refresh() {
    return datacontext.getChildren(childs, parentId);
}

var addGrandChildren = function () {
    $.each(childs, function (i) {
        var grandChildren = ko.observableArray();
        var childId = $(this).data(id);
        datacontext.getGrandChildren(grandChildren, childId);
    });
    return;
};

Or some other method?

解决方案

Providing that your relationships are not unidirectional, Breeze entities will automatically hook themselves together when you query them. ( Edit: as of v 1.3.5 - breeze will also hook up unidirectional relations as well. )

This means that if you use a query to extract n entities that just happen to be related all of them will be automatically linked to one another in the correct fashion. The same occurs if you use the EntityQuery.expand method. So your issue is simply how to query for whatever portion of the graph you want in the least number of calls.

Note: you should also look at the EntityAspect.loadNavigationProperty method if you really want to actually "walk" the graph. But this can be non-performant if you are dealing with large graphs.

这篇关于何时将扩展其他复杂类型添加到Breeze实体上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆