处理计算性能与breezejs和Web API [英] Handling calculated properties with breezejs and web api

查看:140
本文介绍了处理计算性能与breezejs和Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与使用BreezeControllerAttribute的Web API BreezeJS试验。应该如何在一个实体计算性能被曝光?我发现可靠地做到这一点的唯一方法是创建一个中间DTO从实体继承或使用投影。通常我会用一个只读属性为这种情况,但那些似乎被忽略。

I'm experimenting with BreezeJS with Web API using the BreezeControllerAttribute. How should calculated properties on an entity be exposed? The only way I've found to do this reliably is to create an intermediate DTO that inherits from the entity or use a projection. Normally I would use a readonly property for this scenario, but those appear to be ignored.

推荐答案

在微风地图JSON特性数据的​​实体,它的忽略不承认属性。这就是为什么你的服务器类的计算性能数据将被放弃,即使你看到他们在JSON在电线上。

When Breeze maps JSON property data to entities, it ignores properties that it does not recognize. That's why your server class's calculated property data are discarded even though you see them in the JSON on the wire.

幸运的是,你可以教微风通过识别属性的注册为未映射的属性即可。我会告诉你怎么做。让我举一些背景第一。

Fortunately, you can teach Breeze to recognize the property by registering it as an unmapped property. I'll show you how. Let me give some background first.

您计算的属性将被已知微风客户如果当时由数据库计算的属性。数据库支持的特性(经常和计算),在元数据的回升为映射属性。

Your calculated property would be "known" to the Breeze client had it been a property calculated by the database. Database-backed properties (regular and calculated) are picked up in metadata as mapped properties.

不过你的情况(如果我理解正确)的属性是在服务器端类的逻辑定义,而不是在数据库中。因此它不是在元数据映射的场所之间。它是从元数据隐藏。这是一个映射的实例属性。

But in your case (if I understand correctly) the property is defined in the logic of the server-side class, not in the database. Therefore it is not among the mapped properties in metadata. It is hidden from metadata. It is an unmapped instance property.

我假设你没有从串行隐藏它。如果你看一下网络流量类的查询,你可以看到你的计算性能数据在客户端到达。问题是,微风无视它时,它从这些查询结果物化的实体。

I assume you're not hiding it from the serializer. If you look at the network traffic for a query of the class, you can see your calculated property data arriving at the client. The problem is that Breeze is ignoring it when it "materializes" entities from these query results.

的解决方案是登记在MetadataStore 中的计算出的属性

The solution is to register the calculated property in the MetadataStore.

我修改的中的 entityExtensionTests.js 的商务部code样品,包括这个场景;你可以从GitHub上得到code或等待下一个版本微风

I modified the entityExtensionTests.js of the DocCode sample to include this scenario; you can get that code from GitHub or wait for the next Breeze release.

或者只是请跟随下面的code,开始在这个片段中的员工类的<​​em> NorthwindModel.cs 的:

Or just follow along with the code below, starting with this snippet from the Employee class in NorthwindModel.cs:


// Unmapped, server-side calculated property
[NotMapped] // Hidden from Entity Framework; still serialized to the client
public string FullName { 
    get { return LastName + 
             (String.IsNullOrWhiteSpace(FirstName)? "" : (", " + FirstName)); }
}

这是在自动化测试的 entityExtensionTests.js


test("unmapped property can be set by a calculated property of the server class", 2,
  function () {

    var store = cloneModuleMetadataStore(); // clones the Northwind MetadataStore

    // custom Employee constructor
    var employeeCtor = function () {
        //'Fullname' is a server-side calculated property of the Employee class
        // This unmapped property will be empty for new entities
        // but will be set for existing entities during query materialization
        this.FullName = ""; 
    };

    // register the custom constructor
    store.registerEntityTypeCtor("Employee", employeeCtor);

    var fullProp = store.getEntityType('Employee').getProperty('FullName');
    ok(fullProp && fullProp.isUnmapped,
        "'FullName' should be an unmapped property after registration");

    var em = newEm(store); // helper creates a manager using this MetadataStore

    var query = EntityQuery.from('Employees').using(em);

    stop(); // going async
    query.execute().then(success).fail(handleFail).fin(start);

    function success(data) {
        var first = data.results[0];
        var full = first.FullName();

        // passing test confirms that the FulllName property has a value
        ok(full, "queried 'Employee' should have a fullname ('Last, First'); it is "+full);
    }

});

您需要做的是在测试示例的这一小部分:

What you need to do is in this small part of the test example:


var yourTypeCtor = function () {
    this.calculatedProperty = ""; // "" or instance of whatever type is is supposed to be
};

// register your custom constructor
store.registerEntityTypeCtor("YourType", yourTypeCtor);

这篇关于处理计算性能与breezejs和Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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