Automated Dependency Injector
Updated 22 Jun 2018
Please read the full documentation at https://github.com/fschwaiger/matlab-dependency-injector.
This class automates the wiring of dependencies that are exposed in class constructors. It inspects the constructor argument names and finds appropriate dependencies via lookup files that you can define with each package. At any time, you can override dependencies, as shown in the example below.
% file +package/MyClass.m
classdef MyClass < handle
function MyClass(firstDep, secondDep)
% file +package/InjectorConfig.m
classdef (Abstract, Hidden) InjectorConfig
firstDep = 'FirstDependencyClass'
function value = secondDep()
value = 42;
% file application.m
instance = Injector().get(?package.MyClass);
% file unittest.m
firstDep = Mock(?package.FirstDependencyClass)
instance = Injector(firstDep).get(?package.MyClass);
Installation is really simple, you only need to copy a single file (Injector.m) to your project.
This class has been tested using newer versions of Matlab only (R2014b+), but it uses mostly standard tools that should be backwards compatible till R2009a. Please, if you find compatibility issues, notify me.
Florian Schwaiger (2021). Automated Dependency Injector (https://github.com/fschwaiger/matlab-dependency-injector), GitHub. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!