This bullet list with animations Inversion of Control and Dependency Injection Logging Creating and Using Custom Services Working with Configuration Files Coming Up... This slide is wit
Trang 1@KevinDockx https://www.kevindockx.com
ARCHITECT
KEVIN DOCKX
Working with Services and Dependency Injection
Trang 2This bullet list
with
animations
Inversion of Control and Dependency Injection
Logging Creating and Using Custom Services Working with Configuration Files
Coming Up
Trang 3This slide is
with
animations
Class implementation has to change when
a dependency changes Difficult to test
Class manages the lifetime of the dependency
This is tight coupling
Inversion of Control and Dependency Injection
PointsOfInterestController
MyLogger
OtherService
uses
uses
Trang 4Inversion of Control delegates the function of selecting a concrete implementation type for a class’s dependencies
to an external component.
Inversion of Control
Trang 5Dependency Injection is a specialization of the Inversion
of Control pattern The Dependency Injection pattern
uses an object - the container - to initialize objects and provide the required dependencies to the object.
Dependency Injection
Trang 6public class PointsOfInterestController :
Controller
{
private
ILogger<PointsOfInterestController>
_logger;
public PointsOfInterestController(
ILogger<PointsOfInterestController>
logger)
{
_logger = logger;
}
}
implementation
Trang 7This slide is
with
animations
Dependency Injection is built into ASP.NET Core
ConfigureServices is used to register services with the built-in container
Inversion of Control and Dependency Injection
Trang 8This bullet list
with
animations
Injecting and Using a Logger
Trang 9This bullet list
with
animations
Logging to a File
Trang 10This bullet list
with
animations
Implementing and Using a Custom Service
Trang 11This bullet list
with
animations
Working with Configuration Files
Trang 12This bullet list
with
animations
Scoping Configuration to Environments
Trang 13This bullet list
with
animations
Dependency injection
- Specialization of IoC
- Loose coupling, less code changes, better testability
Summary
Trang 14This bullet list
with
animations
Custom services are registered in ConfigureServices
- Transient
- Scoped
- Singleton
Use configuration files for configuration data, scoped to a specific environment
Summary