
Dependency Injection and Inversion of Control Principles
Explore the concepts of Dependency Injection and Inversion of Control, including their importance in software development, key principles, and practical examples. Learn how these principles enhance code reusability and maintainability.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Dependency Injection Andres K ver, IT College 2016/2017 Spring
Dependency Inversion Principle Theory was first formalized by Robert C. Martin, 1996 High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions Example from life myriad of chargers and small scale electronic devices
Dependency Inversion Every module is with its own interface Higher module has to support them all separately Modules are not reusable Higher module Interface Interface Interface Lower module Lower module Lower module
Dependency Inversion Higher module specifies the required interface All modules depend of the same interface Modules are reusable Higher module Interface Lower module Lower module Lower module
Inversion of Control Software development pattern Implements the Dependency Inversion principle Control over how different systems interface with each other Control over program events (command line vs graphical UI) Control over dependency creation and binding
Dependency Injection Implementation of IoC (DIP -> IoC -> DI) Dependency creation and resolving of them is moved outside of dependent class Injector Class Interface Class Interface Dependency Dependency
DI -Caution Internal principles of class behavior will be exposed Dependencies are created before they are actually needed Don t overuse is it needed for everything?
DI Meta example Using constructor IRepo xmlrepo = new XMLRepo(); Kontroller knt = new Kontroller(xmlrepo); public class Kontroller{ private readonly IRepo repo; public Kontroller(Irepo repo){ this.repo = repo; } }
DI Meta example MVC During testing you can supply different implementation of repository In regular use default constructor is used and dependency is created public class PersonController:Controller{ private readonly IRepo _repo; public PersonController (Irepo repo){ _repo = repo ?? new PersonRepository(); } public PersonController() : this.PersonController(null) { } }
Dependency Injection Container Framework Registers interfaces and their implementations Resolves dependencies and provides (injects) correct implementation Lifecycle management of created objects Several implementations, widely used are Ninject Open Source Unity - MS official solution ASP.NET Core has built-in lightweight solution out-of-the-box
Demo console application public class PetOwner { private readonly IDog _dog; public interface IDog { string Walk(); int WalkingCount { get; set; } } public PetOwner(IDog dog) { _dog = dog; } public class DogPoodle : IDog { public string Walk() { WalkingCount++; return "Walking with Poodle "+WalkingCount.ToString()+" times!"; } public int WalkingCount { get; set; } } public int Walkings() { return _dog.WalkingCount; } public void Walk() { Console.WriteLine(_dog.Walk()); } }
Demo console application static void Main(string[] args) { var kernel = new StandardKernel(); kernel.Bind<IDog>().To<DogPoodle>(); var dogKeeper = kernel.Get<PetOwner>(); dogKeeper.Walk(); Console.ReadLine(); }
Demo Asp.NetMVC Nuget Ninject.MVC5 Register your dependencies here \App_Start\NinjectWebCommon.cs private static void RegisterServices(IKernel kernel) { kernel.Bind<IPersonRepository>(). To<PersonRepository>(). WithConstructorArgument("context", new ContactsContext()); }
Demo Asp.NetMVC Controller public class PersonController : Controller { private readonly IPersonRepository _repo; public PersonController(IPersonRepository repo) { _repo = repo; }