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 full-size slide

  2. Agenda
    • Dependency Injection
    • Inversion of Control

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

  6. Task
    User Service
    User Data Access Object

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  9. 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 full-size slide

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

    View full-size slide

  11. 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 full-size slide

  12. 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 full-size slide

  13. 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 full-size slide

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

    View full-size slide

  15. 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 full-size slide

  16. 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 full-size slide

  17. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  22. charlesmuchene

    View full-size slide