iwhp
iwhp

Reputation: 843

Breeze/OData calling FetchEntityByKey does use filter (EntitySetController.GetEntityByKey not used)

When using Breeze with OData and calling the entityManager.FetchEntityByKey() the following request is sent:

/odata/Customers?$filter=Id eq 2

I would have expected

/odata/Customers(2)

Is it possible to make Breeze using the later one?

Update 2013-12-10:

YES! I have figured out that I can do the following (imho FetchEntityByKey should do this):

entityManager.executeQuery('Cusotmer(2)') ...

then the following request is issued:

/odata/Customers(2)

BUT! now it seems that Breeze (v1.9.6) cannot handle the result correctly. The returned result array is empty. Actually it should only return an item (entity) instead of an array.

Link to UserVoice

I have created a UserVoice feedback

Upvotes: 3

Views: 370

Answers (1)

Ward
Ward

Reputation: 17863

Do you really care whether the Breeze generated url is /odata/Customers(2) or /odata/Customers?$filter=Id eq 2 ? Why?

They are both legitimate OData. The OData server will happily give you the correct answer to either query.

Breeze does have a preference for the latter. Here's why.

The Customers(2) syntax returns one object or null (not 404-unfound).

The Customers?$filter=Id eq 2 filter syntax always returns an array. The array has one item or zero items. But you're going to get an array.

One of our simplifications on the client side is that a query always returns a data.results array. We have one query results API and it is that data.results is always an array no matter how you query. You don't have to test data.results for null. And you don't have to remember "does my query return an object or an array?"

That's our guiding client-side consistency principle. We're unlikely to deviate from that principle.

As it happens, you can intercept the URL generation and change it if you wish. That's an exercise for you. Of course (as you discovered) you'd have to do some work to turn the result back into an array. Neglecting to do is the reason I suspect that your attempt to trick Breeze into the other syntax Customers(2) led to the confusing empty array result.

We're unlikely to change the default behavior unless we get some serious push-back, supported by compelling reasons.

Upvotes: 2

Related Questions