omar wrote:
If I want to have a linked property (ReadOnly) or an Own property (Read-Write) that looks up its data from another entity based on an ID field.
example: upon extending the Employee entity, I wanted to add a ReadOnly property (DepartmentName) that looks up the departments name from the Department table.
I know I could use the RelationPredicateBucket to walk the PK-FK relation between the two tables and obtain whatever fields I want from the Department entity, but my question really concerns optimizing this process to minimize round-trips to the presistance storage. Is there a best practice approach to such a scenario?
As DevilDog explained, use a prefetch path, that way you know the related entity is there and it will fetch it in a separate query, 1 per type (so if you fetch multiple employees, it will still use 2 queries, one for the employee and one for the department)
One approach I am contemplating is to build a dynamic list from the Department entity that
I could use to lookup the department's name instead of going back to the database (of course I am wrestling with the synchronization issues when the persistence storage becomes out-of synch with the cached dynamic list).
Did anybody wrestle with such issues? I would like very much to learn from other people’s experiences so that not to travel a dead-end road...
Caching data that doesn't change a lot can help performance. Like a hashtable which contains data stored by a given key which you use to lookup data for a given purpose. Caching is however more efficient when it is done higher up the call-chain. For example: it's way more efficient to cache a webpage for 1 minute, than to cache the data used to build the webpage: with the data-cache you still have to perform the processing to build the webpage.
For stale data issues (data outside the database gets outofsync with the data inside the database), you can setup a variety of systems to get that worked out: for example refresh data in the cache every X minutes, or setup some sort of mechanism which invalidates the cache based on parameters like a poll every 10 minutes in the database.
Caching of data is however often not that fast as you might think, especially when the data changes a lot: as soon as you have to verify fetched data with data in a cache, the process will become slower than when you fetch the data directly, again. After all: the database caches query resultsets also, as well as query execution plans, which makes it often faster to refetch the data than to walk lists in memory sequentially.