Tentative use in DCI¶
Lookups can help a coordinator find an object that offers a capability needed for a use case. In a design influenced by Data, Context and Interaction (DCI), this can supply part of the context's object-selection responsibility: the coordinator asks for a role type, and a lookup supplies an available object. The lookup model explains that discovery mechanism without requiring DCI as a way to structure the application.
Reenskaug and Coplien's account of DCI separates finding and mapping the participating objects from the interaction algorithm they perform. Lookups addresses the narrower problem of finding objects by type. The application still defines the collaboration and decides which objects are suitable participants.
Finding an available capability¶
Consider a save operation that should work with whichever selected object
currently offers a Saveable capability. The lookup owner exposes that object, allowing
the coordinator to query the supplied lookup and call the capability's method.
Here, the function save_available is the coordinator and the lookup contains
one document:
from lookups import Lookup, singleton
class Saveable:
def save(self) -> None:
raise NotImplementedError
class Document(Saveable):
def __init__(self) -> None:
self.saved = False
def save(self) -> None:
self.saved = True
def save_available(available: Lookup) -> bool:
target = available.lookup(Saveable)
if target is None:
return False
target.save()
return True
document = Document()
assert save_available(singleton(document))
assert document.saved
Document already implements Saveable before entering the lookup. Searching
by that parent type finds the document without changing its class or attaching
methods. The coordinator handles absence explicitly and knows nothing about
the document's construction. The owner decides which object to expose and when, so
it could instead supply a lookup whose membership changes with the selection.
An application may expose a separate capability object, but discovery alone
does not establish that it is the data object itself.
Two meanings of role and context
This project's capability-oriented use of role means an interface expressed by a searched type. In the stricter DCI execution model, a role identifies a position occupied by an object within a particular context. Roles can carry context-specific interaction behaviour. A capability type does not by itself express that position or behaviour.
Context here means the coordinator, such as save_available, which
searches a separate lookup. Tulach's account also uses the term for a
populated lookup. Keep those meanings distinct when comparing the accounts.
Where discovery stops¶
The example illustrates selection and an ordinary method call. It supplies no context-local role methods, role-method precedence or DCI interaction execution semantics. Avoiding runtime method injection does not resolve those questions: an implementation still needs to define how role behaviour is resolved and how participating objects collaborate.
Reenskaug's DCI execution model, particularly sections 3 and 4, gives a more specific account of role methods and coherent binding. Lookups does not implement that execution model. In particular, separate queries against changing sources do not atomically bind a coherent network of participants for one interaction. Observing membership changes also does not establish such a binding.
This limitation matters when assessing broader suitability. In his discussion of Lookup and DCI, Tulach acknowledges that Reenskaug and Coplien challenged the general claim. His demonstrated cases involve one role or several roles of the same type, leaving more general configurations open to investigation. This Python library's type-based discovery should likewise be treated as a possible aid to selection, with broader DCI suitability unproved.
Choosing discovery¶
Discovery fits when the lookup owner should control availability independently of the coordinator, and the searched type expresses enough of what the coordinator needs. A controller, view or data object can supply the source, allowing the coordinator to use its members without owning their registration or construction. Results and deferred members can support changing availability or creation on demand, as described in the lookup model.
When the caller already knows the intended participants, passing them directly to the coordinator may express the use case more clearly. Type-based discovery adds little if selection depends on relationships or application rules that the lookup does not express. Several objects matching one type also require an application selection policy when their identities matter. Choose that policy explicitly instead of relying on a query for one match to make the decision. See Queries, results and items for query behaviour, and Project origins for the library's background.