Dependency Injection Mechanism

Slides:



Advertisements
Similar presentations
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Advertisements

Chapter 6: Using Design Patterns
© Lethbridge/Laganière 2001 Chapter 9: Architecting and designing software1 Layers Data from IBM-Rational and Craig Larman…
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Chapter 26 Applying Gang of Four Design Patterns 1CS6359 Fall 2012 John Cole.
Intro to Spring CJUG - January What is Spring? “The Spring framework provides central transaction control of various objects.” This means that any.
Dependency Injection and Model-View-Controller. Overview Inversion of Control Model-View-Controller.
Presenter - Donn Felker.  Senior Consultant for Microsoft Gold Certified Partner- Statêra.  8 years of experience in developing and architecting enterprise.
Building SOLID Software with Dependency Injection Jeremy Rosenberg.
Internationalization and the Java Stack Matt Wheeler.
Todd Snyder Development Team Lead Infragistics Experience Design Group.
Abstract Factory Design Pattern making abstract things.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
DEPENDENCY INJECTION & INVERSION OF CONTROL. WHAT’S GOING TO BE COVERED Quick intro to C# for Java developers Dependency Injection Inversion of Control.
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group
Andrew S. Budarevsky Adaptive Application Data Management Overview.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Refactoring for Testability (or how I learned to stop worrying and love failing tests) Presented by Aaron Evans.
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Introduction to Object-Oriented Programming Lesson 2.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
Virtual techdays INDIA │ 9-11 February 2011 SESSION TITLE Kamala Rajan S │ Technical Manager, Marlabs.
L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile
Copyright © Craig Larman All Rights Reserved Large-Scale System Partitioning.
Object Oriented Programming Some Interesting Genes.
Examples (D. Schmidt et al)
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Interface Segregation / Dependency Inversion
Introduction to .NET Florin Olariu
Structure of a web application
.NET Remoting Priyanka Bharatula.
Internationalization
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Mark Seemann - Dependency Injection in .NET
Data Abstraction: The Walls
Delegates and Events 14: Delegates and Events
Dependency Injection Andres Käver, IT College 2016/2017 Spring.
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
object oriented Principles of software design
Chapter 6: Using Design Patterns
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
Multiuser Protection and the Mediator Pattern
Jim Fawcett CSE776 – Design Patterns Summer 2003
Model-View-Controller Patterns and Frameworks
An Introduction to Software Architecture
GoF Design Patterns (Ch. 26)
CS 350 – Software Design Singleton – Chapter 21
Fundaments of Game Design
Dependency Injection in .Net Core
CS 325: Software Engineering
Designing For Testability
European conference.
#01# ASP.NET Core Overview Design by: TEDU Trainer: Bach Ngoc Toan
Development Environment Setup
Concepts in ASP.NET Core App
Migrate ASP.NET Core 1.x to 2.0
Application Startup in ASP.NET Core
Building Your First ASP.NET Core Web Application
Migrate ASP.NET Core 1.x to 2.0
ASP.NET Core Middleware Fundamentals
GoF Patterns Ch. 26.
Presentation transcript:

Dependency Injection Mechanism #09# Dependency Injection Mechanism 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 Service Lifetimes and Registration Options Request Services Designing Your Services For Dependency Injection Replacing the default services container Recommendations

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.

What is Dependency Injection?

What is Dependency Injection?

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

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

Service Lifetimes and Registration Options Transient Scoped Singleton

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.

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.

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.

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)

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.