Package 'injectoR'

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

Help Index


Binder factory

Description

Binder factory

Usage

binder(parent = .binder, callback = function(binder) binder)

Arguments

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

Value

result of the injected callback if one is specified, otherwise the new binder

Examples

b <- binder ()

Default scope, bindings are provisioned each time a bean is injected

Description

Default scope, bindings are provisioned each time a bean is injected

Usage

default(provider)

Arguments

provider

unscoped delegate, no argument function responsible for provision


Creates a key to factory binding

Description

Creates a key to factory binding

Usage

define(..., scope = default, binder = .binder)

Arguments

...

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

Examples

define (hello = function () 'world', binder = binder ())

Injects the callback function

Description

Injects the callback function

Usage

inject(callback, binder = .binder)

Arguments

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

Value

result of the injected callback evaluation

Examples

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 ()))

Dependency injection framework

Description

Dependency injection framework

Author(s)

levk


Aggregates multiple factories under one key

Description

Aggregates multiple factories under one key

Usage

multibind(key, scope = default, combine = function(this, parent)
  base::c(this, parent), binder = .binder)

Arguments

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

Value

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

Examples

multibind ('keys', binder = binder ()) (function () 'skeleton')

Shims libraries

Description

Shims libraries

Usage

shim(..., library.paths = .libPaths(), callback = function() binder,
  binder = .binder)

Arguments

...

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

Value

result of the callback if specified, binder otherwise

Examples

shim ('injectoR', callback = function (inject) inject, binder = binder ())

Singleton scope, bindings of this scope are provided once, on initial demand

Description

Singleton scope, bindings of this scope are provided once, on initial demand

Usage

singleton(provider)

Arguments

provider

unscoped delegate, no argument function responsible for provision

Examples

define (three = function () 3, scope = singleton, binder = binder ())