1 / 13

Dependency Injection Mechanism

Dependency Injection Mechanism. #09 #. Design by: TEDU Trainer: Bach Ngoc Toan Website: www.tedu.com.vn Facebook: fb.com/ teduchannel. Please like videos and subscribe TEDU Channel to following the next video. Overview. What is Dependency Injection ? Using Framework-Provided Services

thomasp
Download Presentation

Dependency Injection Mechanism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dependency Injection Mechanism #09# Design by: TEDU Trainer: Bach Ngoc Toan Website: www.tedu.com.vn Facebook: fb.com/teduchannel Please likevideos 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

  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.

More Related