dependency injection presentation

Upload: reza-jafari-panah

Post on 03-Apr-2018

243 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Dependency Injection Presentation

    1/20

    By

    Reza Panah

  • 7/29/2019 Dependency Injection Presentation

    2/20

    Inversion of Control (IoC) Programming to interfaces instead of

    implementations

    Instantiation of the services elsewhere (Notwhere and when they are used)

    Separating Configuration from Use

  • 7/29/2019 Dependency Injection Presentation

    3/20

    Itsa way to structure code so that we never

    have to imperatively ask for DEPENDENCIES.Rather, we force consumers to supply them.

    Dependency Inject in .Net by Mark Seemann

  • 7/29/2019 Dependency Injection Presentation

    4/20

    publicclassUserController:Controller

    {

    [HttpPost]

    publicActionResult Login(string username, string password)

    {

    var userRepository = newNHibernateUserRepository();

    var found = userRepository.Find(userName,password).SingleOrDefault();if (found == null)

    thrownewException("Invalid username or password");

    FormsAuthentication.SetAuthCookie(username, false);

    return Redirect("/User/Profile");

    }

    }

  • 7/29/2019 Dependency Injection Presentation

    5/20

    publicclassUserController:Controller

    {

    publicIUserRepository UserRepository { get; set; }

    [HttpPost]

    publicActionResult Login(string username, string password)

    {

    var found = UserRepository.Find(username, password).SingleOrDefault();if (found == null)

    thrownewException("Invalid username or password");

    FormsAuthentication.SetAuthCookie(username, false);

    return Redirect("/User/Profile");

    }

    }

  • 7/29/2019 Dependency Injection Presentation

    6/20

    publicclassUserController:Controller

    {

    private readonlyIUserRepository userRepository;

    publicUserController(IUserRepository userRepository)

    {

    this.userRepository = userRepository;

    }[HttpPost]

    publicActionResult Login(string username, string password)

    {

    var found = userRepository.Find(username, password).SingleOrDefault();

    if (found == null)

    thrownewException("Invalid username or password");FormsAuthentication.SetAuthCookie(username, false);

    return Redirect("/User/Profile");

    }

    }

  • 7/29/2019 Dependency Injection Presentation

    7/20

    A singlepoint in an application where theentire object graph is composed at once

    It has access to the entire context, whichenables it to make intelligent decisions aboutwhich dependencies go where.

  • 7/29/2019 Dependency Injection Presentation

    8/20

    Building loosely-coupled solutions Clean architecture

    Maintainability (Extending the solution and fixingbugs easily)

    Unit testing the right way Possibility of testing each service individually

    Otherwise they are not called unit tests, they areintegration tests

  • 7/29/2019 Dependency Injection Presentation

    9/20

    Constructor Injectionprivate readonlyIUserRepository userRepository;

    publicUserController(IUserRepository userRepository)

    {

    this.userRepository = userRepository;

    }

    Setter Injection (Property Injection)publicIUserRepository UserRepository { get; set; }

    Autowiring

  • 7/29/2019 Dependency Injection Presentation

    10/20

    Service LocatorpublicclassUserController:Controller

    {

    [HttpPost]

    publicActionResult Login(string username, string password)

    {

    var userRepository = ServiceLocator.Instance.GetObject();

    var found = userRepository.Find(userName,password).SingleOrDefault();if (found == null)

    thrownewException("Invalid username or password");

    FormsAuthentication.SetAuthCookie(username, false);

    return Redirect("/Contractor/Profile");

    }

    }

    ServiceLocator is not Dependency Injection Coupling your consumers to the ServiceLocator Issues with unit testing

  • 7/29/2019 Dependency Injection Presentation

    11/20

    Poor mans Injection (Bastard Injection)private readonlyIUserRepository userRepository;

    publicUserController(IUserRepository userRepository)

    {

    this.userRepository = userRepository;

    }publicUserController()

    {

    this.userRepository = new UserRepository();

    }

    It becomes very easy to violate Liskov SubstitutionPrinciple

    Leads to a tightly-coupled system

  • 7/29/2019 Dependency Injection Presentation

    12/20

  • 7/29/2019 Dependency Injection Presentation

    13/20

    Spring.Net Ninject

    StructureMap

    Castle Windsor Unity

  • 7/29/2019 Dependency Injection Presentation

    14/20

    A port of Java Spring framework Open source

    Spring Source Community (Owned by VMWare)

    Supports

    Constructor injection Property injection Autowiring through constructor and property injection

    (by type or by name)

    XML Configuration (Spring CodeConfig was

    launched in 2010 to allow configuration in code) Support for other framework such as NHibernate,

    Spring.Net Messaging, AOP, etc

  • 7/29/2019 Dependency Injection Presentation

    15/20

  • 7/29/2019 Dependency Injection Presentation

    16/20

    Open source Fluent configuration

    Very much extensible

    Supports both constructor and propertyinjection

    Autowiring as the main approach

  • 7/29/2019 Dependency Injection Presentation

    17/20

  • 7/29/2019 Dependency Injection Presentation

    18/20

  • 7/29/2019 Dependency Injection Presentation

    19/20

    We wont have to make changes in thesolution

    We are creating a small application and wedont need DI

    There is an overhead

    We dont need unit test

    So what if we have to change every class to

    add a minor functionality

  • 7/29/2019 Dependency Injection Presentation

    20/20