Dependency Injection
in .Net
Presented By:
Ahasanul Kalam Akib - Software Engineer
2
What is Dependency Injection?
3
What is Dependency Injection?
4
What is Dependency Injection?
• A set of software design principles and Patterns that enable us to develop loosely
coupled code
5
Benefits of loose coupling code
 Easy to extend
 Easy to test
 Easy to maintain
 Facilitates parallel development
 Facilitates late binding
6
Dependency injection patterns
• Constructor injection
• Property injection
• Method injection
• Ambient context
• Service locator
7
Dependency injection containers
• Autofac
• Ninject
• Unity
• Castle Windsor
• Spring.Net
• Built in container (ASP .NET Core)
8
Application layers
9
Tightly coupled code
Data store
10
Tightly coupled code
Data access layer
11
Tightly coupled code
Presentation layer
12
Tightly coupled code
View layer
13
Tightly coupled code
14
Tightly coupled code
• Requires a compile time reference
• Lifetime responsibility
View layer
• Requires a compile time reference
• Lifetime responsibility
• Selects data access reader
Presentation layer
15
Tightly coupled code
• Requires a compile time reference
• Lifetime responsibility
Data access layer
• View tightly coupled with presentation
• Presentation tightly coupled with Data access
• Data access tightly coupled with Data storage
• Means View tightly coupled with Data storage.
16
Tightly coupled code
As it is a simple application, Does it really matter?
17
New request from BOSS
• Different data sources
• Client side cache
• Unit tests
18
Violation
19
Solution
20
Loosely-coupled code using DI steps
1. Add some abstraction
2. Use constructor injection
3. Object composition
21
Loosely-coupled code using DI steps
22
Loosely-coupled code using DI steps
• Loose coupling with an interface
• Break coupling with constructor injection
• Snap the loosely-coupled pieces together
23
Loosely-coupled code using DI
24
Loosely-coupled code using DI
25
Loosely-coupled code using DI (Implementing Different data source)
26
Loosely-coupled code using DI (Implementing Different data source)
Changing Data access
layer
27
Loosely-coupled code using DI (Implementing Different data source)
Changing Presentation
layer
28
Loosely-coupled code using DI (Implementing Different data source)
29
Benefits
30
Benefits
Tightly coupling code
Loosely coupling code
31
Dependency injection patterns
Constructor injection
Method injection
32
Dependency injection patterns
Property injection
33
Built in IOC container responsibility
• Registration: the IoC Container needs to know which type of object to create for a
specific dependency; so, it provides a way to map a type to a class so that it can create
the correct dependency instance.
• Resolution: this feature allows the IoC Container to resolve a dependency by creating
an object and injecting it into the requesting class. Thanks to this feature, you don't
have to instantiate objects manually to manage dependencies.
• Disposition: the IoC Container manages the lifetime of the dependencies following
specific criteria.
34
More information about Built in IOC
In .NET Core, the dependencies managed by the container are called services. You have
two types of services:
• Framework services: these services are part of the .NET Core framework; some examples of
framework services are IApplicationBuilder, IConfiguration, ILoggerFactory, etc.
• Application services: these are the services that you create in your application; since the IoC
doesn't know them, you need to register them explicitly.
35
Service lifetime
• Singleton: this lifetime creates one instance of the service. The service instance may be created at the
registration time by using the Add() method. Alternatively, the service instance can be created the first time it is
requested by using the AddSingleton() method.
• Transient: by using this lifetime, your service will be created each time it will be requested. To create a service
with the transient lifetime, you have to use the AddTransient() method.
• Scoped: the scoped lifetime allows you to create an instance of a service for each client request. This is
particularly useful in the ASP.NET context since it allows you to share the same service instance for the duration
of an HTTP request processing. To enable the scoped lifetime, you need to use the AddScoped() method.
36
References
• https://www.pluralsight.com/courses/using-dependency-injection-on-ramp
• https://auth0.com/blog/dependency-injection-in-dotnet-core/
• https://www.tutorialsteacher.com/core/dependency-injection-in-aspnet-core
Thank You!
Q&A?

Dependency injection presentation