Dependency Inversion Principle

2,879 views
2,741 views

Published on

Published in: Technology, Business
3 Comments
4 Likes
Statistics
Notes
  • nice presentation with pictures. But if it is in english thr=en it would be more helpful.if possible could you please upload this video in english
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DI
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dependency Inversion Principle from Marco Mangia on Vimeo.<br /><object type="application/x-shockwave-flash" data="http://vimeo.com/moogaloop.swf?clip_id=6563023&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" width="350" height="288"><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=6563023&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1"></param><embed src="http://vimeo.com/moogaloop.swf?clip_id=6563023&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" width="350" height="288" type="application/x-shockwave-flash"></embed></object>
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
2,879
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
3
Likes
4
Embeds 0
No embeds

No notes for slide
  • Dependency inversion principle
  • Dependency Inversion Principle

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

    ×