You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Basically, you get what you ask for. This magic is called Dependency Injection, or DI. And is pretty slick.
Here's an implementation.
# Dependency injection demonstrationimportinspectdefprocess(instance):
"""This is the function in which we ask for dependencies, instance in this case"""printinstanceclassInstance(object):
passclassContext(object):
pass# Available dependencies for injectionvariables= {'instance': Instance(),
'context': Context()}
# What is being asked for?arguments=inspect.getargspec(process)[0]
# Based on the names of the arguments being asked for,# inject the corresponding dependencies.inject= []
forargumentinarguments:
ifargumentinvariables:
inject.append(variables[argument])
process(*inject)
# prints Instance object
Result
Cleaner code. If we need neither Instance nor Context, we are also free to do:
defprocess():
pass
In which case no dependencies are injected (which would effectively never get called).
I'll implement this in our current release as it doesn't break any of our current functionality, but instead augments what can already be done. I'd suggest we keep the current methods, process_instance et al., around for the time being and slowly progress into the new behaviour. If, of course, it turns out to be favourable.
Let me know what you think!
Best,
Marcus
The text was updated successfully, but these errors were encountered:
Yeah, hope so! I'm sure there will be other issues with it, but apparently DI is fairly common, especially in IoC frameworks like ours. It's also good for attracting crowds. "Dependency injection? Woaah" ;)
Hi all,
This is an idea for simplified plug-in development.
Goal
To merge
process_context
andprocess_instance
into a singleprocess
method.Architecture
The question then is:
The answer is:
I picked it up from AngularJS, in which arguments to a function is "injected" based on what is being asked for. For example.
Basically, you get what you ask for. This magic is called Dependency Injection, or DI. And is pretty slick.
Here's an implementation.
Result
Cleaner code. If we need neither Instance nor Context, we are also free to do:
In which case no dependencies are injected (which would effectively never get called).
See here for more information.
Roadmap
I'll implement this in our current release as it doesn't break any of our current functionality, but instead augments what can already be done. I'd suggest we keep the current methods,
process_instance
et al., around for the time being and slowly progress into the new behaviour. If, of course, it turns out to be favourable.Let me know what you think!
Best,
Marcus
The text was updated successfully, but these errors were encountered: