Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dependency Injection Mechanism

Similar presentations


Presentation on theme: "Dependency Injection Mechanism"— Presentation transcript:

1 Dependency Injection Mechanism
#09# Dependency Injection Mechanism Design by: TEDU Trainer: Bach Ngoc Toan Website: Facebook: fb.com/teduchannel Please like videos and subscribe TEDU Channel to following the next video.

2 Overview What is Dependency Injection?
Using Framework-Provided Services Service Lifetimes and Registration Options Request Services Designing Your Services For Dependency Injection Replacing the default services container Recommendations

3 What is Dependency Injection?
Dependency injection (DI) is a technique for achieving loose coupling between objects and their collaborators, or dependencies.  This follows the Dependency Inversion Principle, which states that "high level modules should not depend on low level modules; both should depend on abstractions.“ Extracting dependencies into interfaces and providing implementations of these interfaces as parameters is also an example of the Strategy design pattern.

4 What is Dependency Injection?

5 What is Dependency Injection?

6 Using Framework-Provided Services
Service Type Lifetime Microsoft.AspNetCore.Hosting.IHostingEnvironment Singleton Microsoft.Extensions.Logging.ILoggerFactory Microsoft.Extensions.Logging.ILogger<T> Microsoft.AspNetCore.Http.IHttpContextFactory Transient

7 Registering Your Own Services
 The collective set of dependencies that must be resolved is typically referred to as a dependency tree or dependency graph. Example

8 Service Lifetimes and Registration Options
Transient Scoped Singleton

9 Request Services The services available within an ASP.NET request from HttpContext are exposed through the RequestServices collection. You shouldn't use these properties directly, preferring instead to request the types your classes you require via your class's constructor, and letting the framework inject these dependencies. Prefer requesting dependencies as constructor parameters to accessing the RequestServices collection.

10 Designing Your Services For Dependency Injection
This means avoiding the use of stateful static method calls What if you find that your classes tend to have way too many dependencies being injected? Keep in mind that your Controller classes should be focused on UI concerns, so business rules and data access implementation details should be kept in classes appropriate to these separate concerns.

11 Disposing of services The container will call Dispose for IDisposable types it creates. However, if you add an instance to the container yourself, it will not be disposed.

12 Replacing the default services container
Developers can replace the built-in container with their preferred container. The ConfigureServices method typically returns void, but if its signature is changed to return IServiceProvider, a different container can be configured and returned. (Autofac, Autofac.Extensions.DependencyInjection)

13 Recommendations DI is for objects that have complex dependencies. Controllers, services, adapters, and repositories are all examples of objects that might be added to DI. Avoid storing data and configuration directly in DI. Avoid static access to services. Avoid service location in your application code. Avoid static access to HttpContext.


Download ppt "Dependency Injection Mechanism"

Similar presentations


Ads by Google