In this session we will learn about what is Dependency injection in general, different types of dependency injection, what problem does it solve and why is it important and along with that we will see how Play framework allows us to implement dependency injection
2. Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Join the session 5 minutes prior to the session start time. We start on
time and conclude on time!
Feedback
Make sure to submit a constructive feedback for all sessions as it is very
helpful for the presenter.
Silent Mode
Keep your mobile devices in silent mode, feel free to move out of session
in case you need to attend an urgent call.
Avoid Disturbance
Avoid unwanted chit chat during the session.
3. 1. Understanding DI
What is DI
Why DI
Types of DI
2. DI in Play Framework
3. Demo
4.
5. What is Dependency Injection
In software engineering, dependency injection is a technique whereby one object supplies the
dependencies of another object. A dependency is an object that can be used (a service).
When class A uses some functionality of class B, then it's said that class A has a dependency
on class B.
So, transferring the task of creating the object to someone else and directly using the dependency
is called dependency injection.
6. Why to use Dependency Injection
Suppose I have a service – StorageService, that does some processing and persists processed
records to the database.
This service, depends on another service that is responsible for opening a connection and doing
the actual update on the Database. Eg. StorageDAO
Now, If I use Postgres in production I will have a separate PostgresStorageDAO and inject that
into my StorageService.
What's the problem with this approach?
Suppose, for tests, we want to use H2 Database.
The problem is that the implementation is tightly coupled.
The core principle is to separate behaviour from dependency resolution.
Advantages:
o Helps in Unit testing.
o Boiler plate code is reduced, as initializing of dependencies is done by the injector component.
o Extending the application becomes easier.
o Helps to enable loose coupling, which is important in application programming.
7. Types of Dependency Injection
There are 2 types of dependency injection -
o Compile time DI: https://di-in-scala.github.io/
o Runtime DI: dependencies wire up at the runtime.
Runtime dependency injection is so called because the dependency graph is created, wired and validated at runtime. If a dependency cannot be
found for a particular component, you won’t get an error until you run your application.
Eg: Guice
8. Dependency Injection in Play Framework
Play supports both runtime dependency injection based on and compile time dependency
injection in Scala.
Guice is supported in Play out of the box.
We just need to add a single line in build.sbt and we are ready to use.
o libraryDependencies += guice
9. Annotations to facilitate DI in Play
@Inject: The @Inject annotation indicates that a class or field should be instantiated and
initialized by the dependency injection framework. It is used with dependency injection
frameworks like Spring, Google Guice, or CDI to specify the dependencies of classes.
@Singleton: The default behavior is to re-create an instance each time it is injected. However,
when the @Singleton annotation is applied the same instance will be reused for the lifetime of the
application.
@Named: Used to differentiate between different objects of the same type bound in the same
scope.
@ImplementedBy: A pointer to the default implementation of a type.
Modules: Another way of instructing which class to bind to