140 likes | 261 Vues
Dependency Inversion Principle: Intro. DIP: definition. DEPEND ON ABSTRACTION. DIP: definition. DO NOT DEPEND ON CONCRETE OBJECTS. DIP: definition. IT’S CALLED INVERSION OF CONTROL. .... .... . DIP: definition. OR DEPENDENCY INJECTION TOO. OUT-SOURCING INDIA EFFECTS.
E N D
DIP: definition DEPEND ON ABSTRACTION
DIP: definition DO NOT DEPEND ON CONCRETE OBJECTS
DIP: definition IT’S CALLED INVERSION OF CONTROL .... .... ....
DIP: definition OR DEPENDENCY INJECTION TOO OUT-SOURCING INDIA EFFECTS
DIP: practical rules • No class should derive from a concrete class • No variable should hold a reference to a concrete class • No method should override an implemented • method of any of its base class
DIP: Inversion of what ?? before after Injector Consumer also high level component depends on an abstraction high level component depens on low level components (concrete classes) interface new() new() new() concrete classes depend on an abstraction Dep1 Dep2 Dep2 Dep1 Dep2 Dep2
DIP: a diagram • Take a look at the class diagram • concrete classes depend on an abstraction
DIP: Who This guy is not a preacher or a wizard: Martin Fowler (*) • He gave us three main styles(**) of dependency injection : • Interface Injection • Setter Injection • Constructor Injection • (*) but he is still waiting for a call from hollywood • (**) and many other things...
DIP: Interface Injection (Type 1) publicinterfaceIInjector { voidInjectDependency(IDependentdependent); } publicclassInjector : IInjector { privateIDependent_dependent; publicvoidInjectDependency(IDependentdependent) { _dependent = dependent; } publicvoidDoSomething() { _dependent.DoSomethingInDependent(); } } • With this technique we define and use interfaces • Let’s define an interface to perform injection • Injector implements the interface
DIP: Interface Injection (Type 1) • Get the correnct dependency based on config file • Create our main class and inject the dependency • the method references the dependency, so behaviour depends on the config file IDependentdependency = GetCorrectDependency(); Injectorinjector = newInjector(); injector.InjectDependency(dependency); injector.DoSomething(); Configuration file: <?xml version="1.0"encoding="utf-8" ?> <configuration> <appSettings> <add key="ClassName"value="DIP.InterfaceInjection.DependentClass1" /> </appSettings> </configuration>
DIP: Setter Injection (Type 2) publicclassInjector { privateIDependent_dependent; publicIDependentDependent { get { return_dependent; } set { _dependent = value; } } publicvoidDoSomething() { Dependent.DoSomethingInDependent(); } } • Client class has a property • Create our main class and inject the dependency IDependentdependency = GetCorrectDependency(); Injectorinjector = newInjector(); injector.Dependent = dependency; injector.DoSomething();
DIP: Constructor Injection (Type 3) publicclassInjector { privateIDependent_dependent; publicInjector(IDependentdependent) { _dependent = dependent; } publicvoidDoSomething() { _dependent.DoSomethingInDependent(); } } • Client class has nothins else a constructor to inject dependency • Create our main class and passing dependency • through constructor IDependentdependency = GetCorrectDependency(); Injectorinjector = newInjector(dependency); injector.DoSomething();
DIP: Links and conclusions Inversion of Control Containers and the Dependency Injection pattern Introduction to Dependency Injection by Rich Newman Images from Wacky.com