Title: | R Dependency Injection |
---|---|
Description: | R dependency injection framework. Dependency injection allows a program design to follow the dependency inversion principle. The user delegates to external code (the injector) the responsibility of providing its dependencies. This separates the responsibilities of use and construction. |
Authors: | Lev Kuznetsov |
Maintainer: | Lev Kuznetsov <[email protected]> |
License: | LGPL (>= 3) |
Version: | 0.2.4 |
Built: | 2025-02-28 05:47:34 UTC |
Source: | https://github.com/cran/injectoR |
Binder factory
binder(parent = .binder, callback = function(binder) binder)
binder(parent = .binder, callback = function(binder) binder)
parent |
of the new binder, injection will propagate up the parent stack looking for keys; if omitted defaults to root binder |
callback |
called with the newly created binder and the result is returned; if omitted just the new binder is returned |
result of the injected callback if one is specified, otherwise the new binder
b <- binder ()
b <- binder ()
Default scope, bindings are provisioned each time a bean is injected
default(provider)
default(provider)
provider |
unscoped delegate, no argument function responsible for provision |
Creates a key to factory binding
define(..., scope = default, binder = .binder)
define(..., scope = default, binder = .binder)
... |
injectable bean identifier to factory mappings, the key is the name is matched to a parameter name during injection, the factory responsible for provisioning of the bean, a factory may accept any number of arguments in which case the framework will attempt to inject the argument if a binding to the parameter name exists; if it does not, that argument will not be injected, in which case it is the factory's responsibility to deal with a missing argument |
scope |
of the bean, wraps the injected factory call specifying provisioning strategy, if omitted a new bean instance will be provisioned each time injection is requested; injectoR also ships with with the singleton scope which will provide once and cache the bean for subsequent calls. Interface allows for custom scoping, the scope parameter must be a function accepting key (name) and the provider - the wrapped injected factory call - a function accepting no parameters responsible for actual provisioning |
binder |
for this binding, if omitted the new binding is added to the root binder |
define (hello = function () 'world', binder = binder ())
define (hello = function () 'world', binder = binder ())
Injects the callback function
inject(callback, binder = .binder)
inject(callback, binder = .binder)
callback |
function to inject, a function accepting arguments to be matched to injectable keys; no errors are thrown if no binding is found for a key, this is the intended mechanic for optional injection, if the callback is able to deal with a missing argument the argument becomes optional |
binder |
containing the injectables, defaults to root binder if omitted |
result of the injected callback evaluation
inject (function (two) two, define (two = function () 2, binder = binder ())) inject (function (power) power (2, 4), define (power = function (power) function (x, n) if (n < 1) 1 else x * power (x, n - 1))) inject (function (fibonacci) fibonacci (8), define (fibonacci = function (fibonacci) function (n) if (n < 3) 1 else fibonacci (n - 1) + fibonacci (n - 2), binder = binder ()))
inject (function (two) two, define (two = function () 2, binder = binder ())) inject (function (power) power (2, 4), define (power = function (power) function (x, n) if (n < 1) 1 else x * power (x, n - 1))) inject (function (fibonacci) fibonacci (8), define (fibonacci = function (fibonacci) function (n) if (n < 3) 1 else fibonacci (n - 1) + fibonacci (n - 2), binder = binder ()))
Aggregates multiple factories under one key
multibind(key, scope = default, combine = function(this, parent) base::c(this, parent), binder = .binder)
multibind(key, scope = default, combine = function(this, parent) base::c(this, parent), binder = .binder)
key |
injectable bean identifier |
scope |
of the bean, wraps the injected factory call specifying provisioning strategy, if omitted a new bean instance will be provisioned each time injection is requested; injectoR also ships with with the singleton scope which will provide once and cache the bean for subsequent calls. Interface allows for custom scoping, the scope parameter must be a function accepting key (name) and the provider - the wrapped injected factory call - a function accepting no parameters responsible for actual provisioning |
combine |
aggregation procedure for combination of context and inherited values, a function accepting a list of injectable values from the current binder context and a no argument function to retrieve values of the parent context; if omitted will the binding will aggregate all values |
binder |
for this binding, if omitted the binding is added to the root binder |
a function accepting one or more factories for adding elements to the binding; naming the factories will result in named values injected; optionally accepts a scope for the bindings, if omitted defaults to provide on injection; please be aware that the scope is called without key for unnamed multibinding
multibind ('keys', binder = binder ()) (function () 'skeleton')
multibind ('keys', binder = binder ()) (function () 'skeleton')
Shims libraries
shim(..., library.paths = .libPaths(), callback = function() binder, binder = .binder)
shim(..., library.paths = .libPaths(), callback = function() binder, binder = .binder)
... |
zero or more library names to shim binding each exported variable to the binder; if a library name is specified in a named list format (for example shim(s4='stats4',callback=function(s4.AIC))) all exported variable names from that library will be prepended with that name and a dot (as in the example); if a library cannot be loaded, no bindings are created for that library and no errors are thrown (but there is an error to console as reported by requireNamespace) |
library.paths |
to use for loading namespace |
callback |
injected for convenience using the binder specified after shim is completed, if omitted the call returns the binder |
binder |
for this shim |
result of the callback if specified, binder otherwise
shim ('injectoR', callback = function (inject) inject, binder = binder ())
shim ('injectoR', callback = function (inject) inject, binder = binder ())
Singleton scope, bindings of this scope are provided once, on initial demand
singleton(provider)
singleton(provider)
provider |
unscoped delegate, no argument function responsible for provision |
define (three = function () 3, scope = singleton, binder = binder ())
define (three = function () 3, scope = singleton, binder = binder ())