Inversion of Control (IoC) refers to the inversion of the flow of control (the order in which individual statements, function calls, etc. are executed) in a software. You’ll often hear the term Hollywood principle being mentioned in the same breath as IoC, it simply states "Don’t call us, we’ll call you" which more or less sums up the principles of IoC.
In traditional software design, the flow of control is governed by a central piece of code which often have to address multiple concerns (logging, validation, etc.) and need to be aware of the implementation details of its dependencies. This creates a very tightly coupled application where changes in one component have a ripple effect throughout the rest of the application.
Following the principles of IoC can help you achieve:
- decoupling of execution of a task from implementation (through the use of interfaces)
- greater separation of concerns (each component only focuses on what it’s designed to do)
- more flexibility (implementation can be easily changed without any side effects on other components)
- more testable code (enables the use of stubs/mocks in place of concrete classes intended for production)
- Simplifies the building of specific tasks.
- Has the potential to make the flow of control in an application more complex, and therefore making it harder to follow.
- Misusing or abusing IoC can result in Macaroni code.
- IoC is not a silver bullet for all your system engineering problems, and remember, "Don’t fix what’s not broken"
- When adopting IoC, there is additional training needs for new joiners to the team.
- Design systems for flexibility, which allows quick adaptation to changing environment/requirements
- Avoid complicating system design by trying to be future-proof upfront, you can’t predict the future!
Loosen Up – Tame Your Software Dependencies For More Flexible Apps (MSDN article by James Kovac)
Design Pattern – Inversion of Control and Dependency Injection (by Shivprasad Koirala)
Enjoy what you’re reading? Subscribe to my newsletter and get more content on AWS and serverless technologies delivered straight to your inbox.
I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.
In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong