14. 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. 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.
22. 22
Loosely-coupled code using DI steps
• Loose coupling with an interface
• Break coupling with constructor injection
• Snap the loosely-coupled pieces together
33. 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. 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. 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.