Introduction to the<br />Inversion of Control<br />
Agenda<br /><ul><li>What is a Dependency?
Example Application High Level Architecture
Inversion of Control
Dependency Injection
Service Locator
Dependency Injection Pros/Cons
Unity Container
Questions</li></li></ul><li>What is a Dependency?<br />Some common dependencies include: <br />Application Layers<br />Dat...
Dependencies at a Very High Level<br />BROKEN BUILD!<br />CI <br />Server<br />
The problem<br />
Code is tightly coupled<br />Difficult to isolate when testing<br />Difficult to maintain<br />If I change ComponentX how ...
Inversion Of Control<br />Don’t call me, I’ll call you<br />
Dependency Injection is an implementation of IoC<br />The ability to supply (inject) an external dependency into a softwar...
What is Dependency Injection? <br />
Dependency  Injection<br /><ul><li>In DI we have three ways of implementing the same:
Constructor way
Setter way
Method way</li></li></ul><li>Constructor Injection<br />
Setter Way<br />
Method Way<br />
Upcoming SlideShare
Loading in …5
×

Inversion of control

2,930
-1

Published on

Inversion of control design pattern and its implementation, dependency injection and service locator.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,930
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
102
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • Constructor:In this methodology we pass the object reference in the constructor itself.
  • IoC pattern is used to enable extensibility in a class or framework.You want to decouple your classes from their dependencies so that the dependencies can be replaced or updated with minimal or no changes to your classes&apos; source code.You want to test your classes in isolation, without using the dependencies.You want to decouple your classes from being responsible for locating and managing the lifetime of dependencies.(IoC) is an old principle in object oriented programming, but it’s gotten a lot of buzz the last couple of years because of the proliferation of Dependency Injection (DI) tools and articles. 
  • A form of the IoC pattern, Dependency Injection (DI), Any of the following conditions justifies using the solution described in this pattern:You want to decouple your classes from their dependencies so that these dependencies can be replaced or updated with minimal or no changes to your classes&apos; source code.You want to be able to write classes that depend on classes whose concrete implementation is not known at compile time.You want to be able to test your classes in isolation, without using the dependencies.You want to decouple your classes from being responsible for locating and managing the lifetime of dependencies.Do not instantiate the dependencies explicitly in your class. Instead, declaratively express dependencies in your class definition. Use a Builder object to obtain valid instances of your object&apos;s dependencies and pass them to your object during the object&apos;s creation and/or initialization. This better supports testability and growth of a system over time. you are free to choose whichever DI container you want, but you must provide an adapter for it to the IContainerFacade interface.
  • Constructor:In this methodology we pass the object reference in the constructor itself.Interface: In this methodology we implement an interface from the IOC framework. IOC framework will use the interface method to inject the object in the main class.Public Class Person : IAddressDI{//setaddress comes from IaddressDISetAddress(Iaddress a ){}}Service locator Next slide
  • Constructor:In this methodology we pass the object reference in the constructor itself.
  • Setter and Getter: The dependent objects are exposed through set/get methods of classes.The bad point is because the objects are publicly exposed it breaks the encapsulation rule of object oriented programming.
  • Interface: In this methodology we implement an interface from the IOC framework. IOC framework will use the interface method to inject the object in the main class.Public Class Person : IAddressDI{//setaddress comes from IaddressDISetAddress(Iaddress a ){}}
  • The other way to inject dependency is by using service locator. Your main class which will aggregate the child object will use the service locator to obtain instance of the address object. The service locator class does not create instances of the address object, it provides a methodology to register and find the services which will help in creating objects.
  • Constructor:In this methodology we pass the object reference in the constructor itself.
  • Applications based on the Composite Application Library are composites that potentially consist of many loosely coupled modules. They need to interact with the shell to contribute content and receive notifications based on user actions. Because they are loosely coupled, they need a way to interact and communicate with one another to deliver the required business functionality. To tie together these various modules, applications based on the Composite Application Library rely on a dependency injection container. Dependency injection containers can reduce the dependency coupling between objects by providing the facility to instantiate instances of classes and manage their lifetime based on the configuration of the container. The modules communicate using events but they will access other modules. How? Using containers who inject the dependecies
  • A container is an abstraction responsible for object management, instantiation and configuration. So you can configure the objects using the container rather than writing client code like factory patterns to implement object management. There are many containers available which can help us manage dependency injection with ease. So rather than writing huge factory codes container identifies the object dependencies and creates and injects them in appropriate objects.  
  • Inversion of control

    1. 1. Introduction to the<br />Inversion of Control<br />
    2. 2. Agenda<br /><ul><li>What is a Dependency?
    3. 3. Example Application High Level Architecture
    4. 4. Inversion of Control
    5. 5. Dependency Injection
    6. 6. Service Locator
    7. 7. Dependency Injection Pros/Cons
    8. 8. Unity Container
    9. 9. Questions</li></li></ul><li>What is a Dependency?<br />Some common dependencies include: <br />Application Layers<br />Data Access Layer & Databases<br />Business Layer<br />External services & Components<br />Web Services<br />Third Party Components<br />.NET Framework Components<br />File Objects (File.Delete(…), Directory.Exists(…))<br />Web Objects (HttpContext, Session, Request, etc)<br />
    10. 10. Dependencies at a Very High Level<br />BROKEN BUILD!<br />CI <br />Server<br />
    11. 11. The problem<br />
    12. 12. Code is tightly coupled<br />Difficult to isolate when testing<br />Difficult to maintain<br />If I change ComponentX how do I know what else it will affect? Did I break anything? <br />If tests are in place they can be your safety net<br />What problems do dependencies create? <br />
    13. 13. Inversion Of Control<br />Don’t call me, I’ll call you<br />
    14. 14. Dependency Injection is an implementation of IoC<br />The ability to supply (inject) an external dependency into a software component. <br />Types of Dependency Injection: <br />Constructor (Most popular)<br />Setter <br />Method<br />What is Dependency Injection? <br />
    15. 15. What is Dependency Injection? <br />
    16. 16. Dependency Injection<br /><ul><li>In DI we have three ways of implementing the same:
    17. 17. Constructor way
    18. 18. Setter way
    19. 19. Method way</li></li></ul><li>Constructor Injection<br />
    20. 20. Setter Way<br />
    21. 21. Method Way<br />
    22. 22. Demonstration<br />Lets See Some Code…<br />
    23. 23. Pros<br />Loosely Coupled<br />Increases Testability (A LOT!)<br />Separates components cleanly<br />Allows for use of Inversion of Control Container<br />Cons<br />Increases code complexity<br />Some Jr. Developers find it difficult to understand at First<br />Can Complicate Debugging at First<br />Complicates following Code Flow <br />Dependency Injection Pros & Cons<br />
    24. 24. Service Locator <br /><ul><li>Form of the Inversion of Control pattern
    25. 25. It allows classes to locate specific services they are interested in without needing to know who implements the service. </li></li></ul><li>Service Locator <br />
    26. 26. Who will manage the Dependency Injection?<br />Containers !<br />
    27. 27. Container<br />A container is an abstraction responsible for object management, instantiation and configuration. <br />you can configure the objects using the container rather than writing client code like factory patterns to implement object management.  <br />
    28. 28. Container<br />
    29. 29. Thanks For Listening.<br />Practical<br />“You should be pragmatic with any pattern,<br />it's not necessary to be applicable for every situation”<br />Jaffal Hasan<br /><ul><li>Mail: hasanjaffal@hotmail.com
    30. 30. Blog: http://www.jaftalks.com/
    31. 31. Twitter: http://twitter.com/jaftalks</li>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×