DynamicBindings - how to get a snapshot
Stephen Pair
stephen at pairhome.net
Fri Apr 25 19:30:57 UTC 2003
Derek Brans wrote:
> I don't know if this is poor programming practice, but I want to take
> a snapshot of the bindings in one context and store them as an
> instance variable, to be used in another context.
>
> Doing that I can answer questions like:
> Who was my parent component at rendering time? Which component was
> named "containerA" at rendering time?
>
> Could you tell me if this is a misuse of DynamicBindings and if not,
> how to take a snapshot?
To get the current set of bindings, you can use "Bindings get" (which
answers the active instance of DynamicBindings). You might want to take
a copy of that if you don't want subsequent changes in bindings to
affect your "snapshot". As for whether or not it's poor programming
practice, I'd need to understand more about what you are trying to
accomplish to make that assessment. In general however, you should keep
in mind that the scope of dynamic bindings is broader than instance and
context activation variables, but more limited than globals. I would
say that many uses of globals would be better served with dynamic
bindings, but you should be careful that you're not using a dynamic
bindings where a method argument or instance variable would be more
appropriate.
For Seaside, I would say that there might need to be some sort of
encapsulation of the active tree of components for any given session and
have that encapsulation accessible through a dynamic binding that holds
the active session. That object could then give you an interface to get
"containerA". As for finding the parent component of a given component,
I'm not sure. It seems to me that you might want to have a parent
instance variable for that.
Here are a couple of cases where I think dynamic bindings are appropriate:
a) when a process' sole job is to respond to some network request (like
an HttpRequest), keeping state related to that request/response pair in
a dynamic binding is appropriate.
b) when you want to track objects that are changed during a given unit
of work (ie a block or method), it is useful to use a dynamic binding to
keep some sort of transaction type of object
c) keeping session and/or user context
d) there are lot's of globals in Squeak that I think would be better
served by dynamic bindings (like "Project current" for instance, where
the use of dynamic bindings would allow multiple simultaneously active
projects to exist in a single Squeak session)
If you find yourself in a situation where you have a number of classes
with similarly named methods that all need one or more of the same
parameters, you might need to use either a global, or a dynamic binding.
- Stephen
More information about the Squeak-dev
mailing list
|