Introduction to IoC
1
Module Objectives • At the end of this module, participants will be able to:
– Understand what IoC is? – Various ways of Injecting dependencies – Benefits of IoC
2
2
What is IoC • Inversion of Control, also known as IOC, is an important objectoriented programming principle that can be used to reduce coupling inherent in computer programs. • IOC is also known as the Dependency Injection. - The Dependency injection technique is used in almost every framework and it is a simple example of the IoC principle applied.
3
3
Types of IoC • Types of dependency injection
– Setter Injection In Setter injection, the injection is done via a setter method. IoC uses setter methods to get the dependent classes it needs. – Constructor Injection. In Constructor injection, IOC implementing class defines a constructor to get all its dependants. The dependent classes are defined in the constructor arguments. 6
6
Benefits of IoC • Managing Dependencies
– It helps in externalizing dependencies to have a centralized control over them. Dependencies can be managed at a single place. • Improve testability
– Another reason for preferring dependency injection is that it makes testing easier. To do testing, you need to easily replace real service implementations with stubs or mocks. – The testing problem is intensified by component environments that are very intrusive, such as Java's EJB framework. 7
7
Key Points • Inversion of Control, is the core of Spring Framework which can be used to reduce coupling inherent in computer programs. • There 2 ways of injecting dependencies
– Setter – Constructor • Benefits of IoC include the following:-
– Managing Dependencies becomes easier – It helps in improving testability
9
9
Questions and Comments
10
10