So for this week I have decided to read “Inversion of Control- An Introduction with Examples in .NET” from the joelabrahamsson website. The reason I chose this article is because with the subject of automatic testing, Inversion of Control and Dependency Injection is what I do not get mostly. It is usually a topic that I do not hear that much, so I believe this will help in understanding on the purpose of using both principles.
This article basically by the title goes over Inversion of Control from the definition to the ways of using it. So Inversion of Control is essentially a way to the invert the flow of control in comparison to procedural programming. It means that every module can focus on what is it designed for and ensures clarity with little complications. The benefits in using Inversion of Control is that when changing the interface of a particular class uses in runtime, that class can no longer be dependent on another class and the code can be decoupled. This means that the class is provided the ability to swap dependency implementations in different environments and allows a component be tested through mocking of dependencies. When having to apply Inversion of Control and getting the instance, there are two ways to get it without having to know about any concrete implementations. These ways are Dependency Injection and Service Locator. Dependency Injection injects an instance by using construction injection or property injection while Service Locator injects an instance based on the configurations of the concrete class. Dependency Injection is mostly used than that of Service Locator, but Service Locator can be used when there are situations Dependency Injection seems impractical.
Based on the content of this article, I would say that it was a good overview about the ideas of Inversion of Control. The author was able to give a few examples of using it and have the UML diagrams to back up with the codes provided. What I did before to understand Inversion of Control was to envision real life scenarios to compare without and with it. The concept is that without it, there is limited options for a software consumer. But by implementing Inversion of Control, there are more options over the software. With that in mind, it made envisioning scenarios easier than it seems.
What I learned from this article is that Inversion of Control shall not be used in all cases because of program flow. The idea is that while it is a useful way to design methods when writing a library that will be reused, it is only critical if there are many cross-cutting concerns. For future practice, I shall try to use Inversion of Control when it is necessary to change a small part of code. This is because it is a practical way of reducing code duplication and it is strong for coding against interfaces instead of implementations.
Link to the article: http://joelabrahamsson.com/inversion-of-control-an-introduction-with-examples-in-net/