File Exchange Pick of the Week

Our best user submissions

Doctor, Do I Need a Dependency Injection?

Posted by Guest Picker,

Greg's pick this week is MATLAB Dependency Injection by Matt McDonnell.

Imagine you want to build an application that dresses you in the morning.

What you wear on any given day will depend on the weather that day. If it's raining, you might wear a rain slicker, rain boots, and carry an umbrella. If it's bright and sunny you might not wear a jacket at all, wear sneakers, and ditch the umbrella.

How do you build an application that can perform the right operations with a large number of different scenarios.

One methodology you can apply is dependency injection.

Contents

Dependency Whaaaaa?

"Dependency injection is a technique for constructing an application from components without the individual components needing to be coupled to a particular implementation of other components."

I stole this right out of the top of the documentation for the "demo_mdepin" function in the File Exchange entry.

But wait... what does that even mean?

Basically this refers to the notion of software plugins. You have a generic application code that is customized by applying different specific implementations.

This enables you to configure the application based on selection criteria like, is it raining.

The modular and independent nature of the components also enables more simplified testing strategies.

You've Seen This Somewhere Before

You might be thinking "hmmm, this seems like using function handles in MATLAB."

if isRaining
  coatFunction = @putOnRainSlicker;
else
  coatFunction = @putOnSequinedJeanJacket;
end
getDressed(coatFunction);

Dependency injection itself is more than just passing around handles to different functions. However the above illustrates the key concept that you can vary implementation by determining which function gets called through some configuration.

But you're not just changing which coat you are wearing when it is raining. You might have completely different clothing needs depending on the weather.

It's more likely you would do something like:

config = struct();
config.dressForRain.getDressed = {'putOnRainSlicker'
                                  'putOnRainBoots'
                                  'grabUmbrella'};
config.dressForRain.putOnRainSlicker = putOnJacket('rainSlicker');
config.dressForRain.putOnRainBoots = ...
config.dressForRain.grabUmbrella = ...
...
config.dressForSun.getDressed = {'putOnSequinedJeanJacket'
                                 'putOnSneakers'};
config.dressForSun.putOnSequinedJeanJacket = putOnJacket('jeanJacket');
config.putOnJacket = ...
...
if isRaining
  outfit = assembleOutfit(config, 'dressForRain');
else
  outfit = assembleOutfit(config, 'dressForSun');
end
getDressed(outfit);

Where the resulting object "outfit" is an assembly of unique methods and properties that depends on the state of the weather. If it is raining the "outfit" object will have a method to "grabUmbrella" otherwise the outfit will not have any method to acquire an umbrella. Some of the component elements can even be shared, like "putOnJacket". It's very flexible, and will work as long as you have a method to "getDressed".

Why Did I Choose This File Exchange Entry

I had never heard of dependency injection prior to coming across this File Exchange entry. While I had actually seen it and experienced it many times, I didn't really understand much of what was going on.

So I thought I would share my learning experience with you.

The documentation for this entry has some excellent examples of how the framework is used. It also points to some very nice articles that describe dependency injection better than I can.

Do You Employ Dependency Injection?

Let us know if this is something you use. In MATLAB? In other programming languages?

You can submit a response to this post here.


Get the MATLAB code

Published with MATLAB® 8.6

5 views (last 30 days)  | |

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.