Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency Injection & Inversion of Control

Dependency Injection & Inversion of Control

The presentation demystifies the concept of dependency injection and inversion of control using a sample java application using Spring.

Charles Muchene

February 27, 2019
Tweet

More Decks by Charles Muchene

Other Decks in Technology

Transcript

  1. Which is which?
    @charlesmuchene
    IoC DI
    !

    View Slide

  2. Agenda
    • Dependency Injection
    • Inversion of Control

    View Slide

  3. Dependency Injection
    “A 25-dollar term for a 5-cent concept”

    View Slide

  4. Dependency Injection
    “A 25-dollar term for a 5-cent concept”
    a pattern (or process) of providing the
    dependencies to a component

    View Slide

  5. Task

    View Slide

  6. Task
    public class User {
    private String name;
    private int age;
    public User(String name, int age) {
    this.name = name;
    this.age = age;
    }

    View Slide

  7. Task
    User Service
    User Data Access Object

    View Slide

  8. User Service
    public interface UserService {
    void save(User user);
    User findOne(long id);
    }

    View Slide

  9. User DAO
    public interface UserDao {
    void save(User user);
    User findOne(long id);
    }

    View Slide

  10. Main
    User user = new User("Charles", 24);
    System.out.println("******************************");
    User fetchedUser = userService.findOne(-1);
    System.out.println(fetchedUser);
    UserService userService = new UserServiceImpl();
    userService.save(user);

    View Slide

  11. User Service Impl
    public class UserServiceImpl implements UserService {
    @Override
    public void save(User user) {
    userDao.save(user);
    }
    ...
    }
    Dependency
    private UserDao userDao = new UserDaoImpl();

    View Slide

  12. User Service Impl
    public class UserServiceImpl implements UserService {
    private UserDao userDao;
    public UserServiceImpl(UserDao userDao) {
    this.userDao = userDao;
    }
    @Override
    public void save(User user) {
    userDao.save(user);
    }
    ...
    }

    View Slide

  13. Main
    User user = new User("Charles", 24);
    System.out.println("******************************");
    User fetchedUser = userService.findOne(-1);
    System.out.println(fetchedUser);
    UserDao userDao = new UserDaoImpl();
    UserService userService = new UserServiceImpl(userDao);
    userService.save(user);

    View Slide

  14. User Service Impl
    public class UserServiceImpl implements UserService {
    private UserDao userDao;
    public UserServiceImpl(UserDao userDao) {
    this.userDao = userDao;
    }
    @Override
    public void save(User user) {
    userDao.save(user);
    }
    ...
    }

    View Slide

  15. User Service Impl
    @Service
    public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public void save(User user) {
    userDao.save(user);
    }
    ...
    }

    View Slide

  16. Main
    User user = new User("Charles", 24);
    System.out.println("******************************");
    User fetchedUser = userService.findOne(-1);
    System.out.println(fetchedUser);
    UserService userService = new UserServiceImpl();
    userService.save(user);

    View Slide

  17. Main: Spring
    User user = new User("Charles", 24);
    System.out.println("******************************");
    User fetchedUser = userService.findOne(-1);
    System.out.println(fetchedUser);
    String contextLocation = "context/applicationContext.xml";
    ApplicationContext context = new
    ClassPathXmlApplicationContext(contextLocation);
    UserService userService = context.getBean(UserService.class);
    userService.save(user);

    View Slide

  18. IoC Container
    // Load up config
    String contextLocation = "context/applicationContext.xml";
    ApplicationContext context = new
    ClassPathXmlApplicationContext(contextLocation);
    // Ask IoC for instance (bean)
    UserService userService = context.getBean(UserService.class);
    // Use the (wired-up) instance
    userService.save(user);

    View Slide

  19. IoC Container
    • Creates
    • Wires together
    • Manages (creation -> destruction)

    View Slide

  20. Inversion of Control
    Programming principle
    principle of delegating the creation,
    configuration and lifecycle management
    of objects to another component or
    framework

    View Slide

  21. In Spring, Inversion of Control is
    achieved through Dependency Injection.

    View Slide

  22. IoC DI
    !

    View Slide

  23. Resources
    • https://github.com/charlesmuchene/di-spring

    View Slide

  24. charlesmuchene

    View Slide