fetch semantics/overhead
By default, only (primitive typed) attributes in Entity instances are directly available, while references must be fetched. This can be quite confusing as it’s often unclear whether some reference has been fetched or not. Always fetching by hand might seem the safer option, but this adds a lot of noise to the code.
Isn’t it possible to fetch references lazily in all cases?
Submitted by Meinte Boersma on 9 March 2011 at 21:59
Issue Log
Well, I suppose it would be possible, but it would require massive changes in the way mobl works. Currently it is assumed that obtaining the value of any entity property is an efficient, synchronous operation. However, if in certain cases this would not be the case (e.g. when fetching an object lazily from the database), it would have to be come an asynchronous operation. That’s a technical reason not to do this.
Another reason to do this is efficiency. The current system basically forces you to think ahead: “will I need this reference at some point?” if so, prefetch it. If not, don’t. It leads to more efficient code. Writing inefficient code (where all references are fetched using
fetch
) leads to ugly, noisy code.
Ah, good point(s). But in the current situation, it’s quite difficult to see whether something really needs to be fetched or not. In particular, it demands reasoning about state (being fetched or not) and invariants which is awkward in a language which abstracts a fair portion of that away. Also, an error might only surface in a really contrived user interaction.
As a workaround, would it be possible to have the mobl editor (and compiler) issue a warning when a reference is accessed without it being prefetched? Obviously, this is difficult because of the mixture of declarative and procedural styles but it doesn’t need to be watertight to be helpful in a lot of cases.
(And it would help if this was documented somewhere in the mobl documentation, outside of the Entity type docs and the reference to persistence.js.)
Ideally, dataflow analysis could analyze where references need to be prefetched or not (and would mark them for prefetching automatically). That’s definitely something I’d like to add.
Log in to post comments