New Posts New Posts RSS Feed: Entity manager cache not working
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Entity manager cache not working

 Post Reply Post Reply Page  <12
Author
svk View Drop Down
Newbie
Newbie


Joined: 11-Oct-2012
Posts: 7
Post Options Post Options   Quote svk Quote  Post ReplyReply Direct Link To This Post Topic: Entity manager cache not working
    Posted: 07-Nov-2012 at 5:52pm
Sorry for the oversight... I previously read through all of the documentation and I must have overlooked this.  It probably wasn't relevant at the time I read it.  This is not a show stopper for me so I am happy to move on.    thanks again.
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 5:59pm
Just so we're clear: classes in different projects can share the same namespace. This happens all the time; many Microsoft classes in different assemblies share the same namespace.

You may not want to do that but you certainly can. 

If you don't want to do that, the Entity Framework DbContext documentation shows a simple workaround: create a subclass of the DbContext that is in the model's namespace. The DbContext subclass doesn't have to do anything other than inherit from your base DbContext ... and shift the namespace. This, I think, is a small price to pay to trick the metadata generation tooling.

I agree with separating the projects as you have done. That can be wise as models grow. Breeze will not interfere with that separation. You just need to spit on it a little bit to get the namespaces aligned.

-- The above is in reply to CCPony who wrote the following ... which somehow has disappeared (maybe I deleted by accident?) --

Placing the data components and the model files into separate projects and referencing those projects in the main web project causes the error.

For example, if you have the following three projects:

BreezeTest.Model - contains item.cs

BreezeTest.Data - contains BreezeTestDatabaseInitializer.cs and BreezeTestDbContext.cs. This projects references BreezeTest.Model

BreezeTest.Web references both BreezeTest.Model and BreezeTest.Data

When you run BreezeTest.Web in this configuration you get: "3 items in query" and "0 entities in cache".


As per Ward's attached sample, however, when all the files in the one project, you get "3 items in query" and "3 entities in cache".

So, the problem seems to be limited when using separate projects. By the way, I used the RequireJs attachment.

Let me know if you have any other questions. 



Edited by WardBell - 07-Nov-2012 at 6:08pm
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 8:22pm
Perhaps you are wondering 

(a) Why did the query return results but no entities in cache? 

(b) Why did Breeze allow the model type and DbContext namespaces to be mismatched? Why didn't it protest?

Let me assure you that Breeze behaved exactly as intended ... if not in the way you expected.

In this example, the client query was routed to the Web API controller's "Items" method. The server responded with JSON data for a type 'Item" belonging to the namespace 'Model'.  Breeze on the client doesn't know about an 'Item' type in the namespace 'Model'; it knows about an 'Item' type in the namespace 'Data'. But that's not the same type at all. Breeze simply doesn't know what type of data it received. Therefore, it cannot turn that data into 'Item' entities ... which is why there are no Items in cache.

But the server did return data and Breeze dutifully provides those data as JavaScript objects in the 'data.results' array of the query promise. In the post which started this thread, we know the server returned 45 'somethings'. They weren't entities - Breeze didn't know what they were. But you, the developer, asked for them and here they are.

Breeze does not demand that a query return entities. If the service returns data that it recognizes as entities - if the data are identified in metadata as entity types  - Breeze will turn those data into entities and will put them in cache. Otherwise it forwards them to the caller via the promise.

There is nothing wrong with the server sending arbitrary data to the client. That's actually a useful feature.  For example, suppose you have a Person entity and Person has 100 columns/properties including an image property that could be 100KB. We want to present a list of Persons, just their first and last names. We can't afford to download every property of every Person in the list. 

Well, in Breeze we can make a "projection query" - a query for selected properties - and transmit only the properties we need. Clearly the data objects that satisfy our query are not whole Person entities. They are something else, something unnamed and un-typed. But something useful nonetheless.

Breeze doesn't judge. It doesn't insist that the query return whole Person entities. It simply examines the incoming data. If they are Persons, they become entities in cache. If not, fine, pass them along to the caller.

Therefore, Breeze shouldn't protest when it receives a data object of an unrecognized type. It was prepared to recognize 'Data.Item' data; it received 'Model.Item" data; no big deal ... and no entities in cache either.

---

This isn't the entire story. Actually, Breeze examines the incoming data objects looking for entities. If the incoming data are object graphs,  Breeze traverses the graphs, looking for objects which it recognizes as entities. If it finds entities within the object graphs, it puts them in cache.

This is a super valuable feature. I use it to query and cache all of my pick-lists in a single shot. For example, I might create a service method called "Lookups" that returns a single object whose properties are arrays of Color, Status, Size, ProductType, ... you get the idea. That object is essentially a bag of lists that I'll use to populate comboboxes.

Then I make a single query to "Lookups" ... which returns this bag of lists. Now breeze doesn't recognize the bag at all. But each of the bag's properties is a a collection of objects that are described in metadata: Color is an entity type, Status is a type, Size is a type, ProductType is a type. Breeze recognizes that these nested objects are entities and puts them in cache. 

So in a single request, in a single payload, I'm able to populate the EntityManager cache with all of the little pick-lists.

That's pretty cool.


Edited by WardBell - 07-Nov-2012 at 8:29pm
Back to Top
 Post Reply Post Reply Page  <12

Forum Jump Forum Permissions View Drop Down