[erlang-questions] Beginner Question: Spring-like IOC in erlang
Steve Vinoski
vinoski@REDACTED
Wed Apr 13 23:52:53 CEST 2011
On Wed, Apr 13, 2011 at 3:44 PM, Todd Greenwood-Geer
<t.greenwoodgeer@REDACTED> wrote:
> 1. Is there an idiomatic way to use dependency injection in erlang?
>
> Most of my use of Spring DI has been to facilitate injection of test classes
> into a stack for functional testing. Is this sort of thing done in erlang?
I guess it depends on the type of dependency you're injecting. Is it a
module, a function, a process? You can inject module dependencies by
passing their names, or using still-marked-as-experimental
parameterized modules, or even dynamically loading them under names
already known to the code using them (you can even go so far as to
generate and load modules on the fly). You can inject functions by
passing them. You can inject processes by passing pids or by
registering them under names already known to the code using them
using the standard registry, pg2, or (see especially) gproc.
> 2. How about AOP style join-points for before and after execution around a
> method?
>
> As an example, wouldn't it be cool to dynamically enable debug or trace
> level logging for an application when a certain error threshold is reached?
> Then, when the system returns to a normal state, the error logging could be
> turned back down... I think I might start doing this in the Java stacks I'm
> currently working with (log4j).
You don't need AOP to do that, since you can monitor the error
threshold with a separate process and have it enable debug or trace
logging on the problematic module when appropriate.
> 3. Is there a way to intercept messages to a process? This is the real
> question... Say a process dies unexpectedly and OTP restarts it... it would
> be cool to be able to dynamically proxy messages to a process that has
> exceeded some error threshold. The proxy could log them, or perform more
> intensive error analysis... Then, once the system is back in a normal state,
> the proxy could be removed from the message loop chain.
Strict interception can be done by registering a proxy in place of the
real process, but if you just want to see the messages without
requiring them to hop through an extra process, debug tracing can do
that. See Chapter 17 of Francesco's and Simon's book:
http://oreilly.com/catalog/9780596518189
Erlang's tracing facilities are incredibly powerful.
--steve
More information about the erlang-questions
mailing list