Code Monkey home page Code Monkey logo

dip-ioc-talk's Introduction

theme transition highlightTheme logoImg slideNumber title
white
slide
monokai
true
DIP & IoC Talk

DIP & IoC Talk

A breif about DIP & IoC including theory and examples.

slides using Markdown with vscode-reveal

Basic Concepts

  • Dependency is a class and/or interface required by another component to function
  • High Level Module is the one will require the dependency
  • Low Level Module is the one will fulfill the dependency
classDiagram
    HighLevelModule --> LowLovelModule
    
    class LowLovelModule {
       
    }

    class HighLevelModule {
        - LowLovelModule lowLevelModule;
    }

DIP

The 5th principle in SOLID

NDepend - Dependency Inversion Principle

--

DIP's characteristics - 1/2

  • High level modules should not depend on low level modules; both should depend on abstractions.
classDiagram
    HighLevelModule --> Abstraction
    LowLovelModule --|> Abstraction

--

DIP's characteristics - 1/2

// c#
class HighLevelModule {
    private LowLevelModule lowLevelModule;

    public HighLevelModule() {
        lowLevelModule = new LowLevelModule();
    }
}

=>

// c#
class HighLevelModule {
    private Abstraction abstraction;

    public HighLevelModule(Abstraction abstraction) {
        this.abstraction = abstraction;
    }
}

class LowLevelModule : Abstraction {

}
  • Is this the only way?

--

DIP's characteristics - 2/2

  • Abstractions should not depend on details. Details should depend upon abstractions
classDiagram
    HighLevelModule --> Abstraction
    Detail --|> Abstraction
    Detail --> LowLovelModule

    class Abstraction {
        + doSomething(arg)
    }

    class Detail {
        + doSomething(arg)
    }

    class LowLovelModule {
        + doSomething(arg, argX)
    }

--

DIP's characteristics - 2/2

// c#
class HighLevelModule {
    private Abstraction abstraction;

    public HighLevelModule(Abstraction abstraction) {
        this.abstraction = abstraction;
    }
}
abstract class Abstraction {
    public abstract void DoSomething(ArgType arg);
}
class LowLevelModule : Abstraction {
    public override void DoSomething(AnotherArgType arg) {}
}
  • Does it looks familiar to you?

--

DIP's characteristics - 2/2

  • Is this the way to go?
// c#
class HighLevelModule {
    private Abstraction abstraction;

    public HighLevelModule(Abstraction abstraction) {
        this.abstraction = abstraction;
    }
}
abstract class Abstraction {
    public abstract void DoSomething(AnotherArgType arg);
}
class LowLevelModule : Abstraction {
    public override void DoSomething(AnotherArgType arg) {}
}

--

DIP's characteristics - 2/2

class HighLevelModule {
    // thousand lines of code...
}
abstract class Abstraction {
    public abstract void DoSomething(ArgType arg);
}
class Detail : Abstraction {
    private LowLevelModule llm;
    public override void DoSomething(ArgType arg) {
        llm.DoSomething(CreateAnotherArgType(arg));
    }
}
class LowLevelModule {
    public override void DoSomething(AnotherArgType arg) {}
}

A mediator should always be the way to go!!!!


Inversion of Control

--

In software engineering, Inversion of Control (IoC) is a design pattern in which custom-written portions of a computer program receive the flow of control from a generic framework.

--wikipedia--

--

// c# 
class HighLevelModule {
    private LowLevelModule lowLevelModule;
    public HighLevelModule() {
        // I knows how to create my dependency
        lowLevelModule = new LowLevelModule();
    }
}

=>

// c#
class HighLevelModule {
    private Abstraction abstraction;
    public HighLevelModule(Abstraction abstraction) {
        // I know my dependency will be given (injected)
        // by the **generic** framework
        this.abstraction = abstraction;
    }
}
class LowLevelModule : Abstraction {

}

IoC Techniques

  • Service Locator
  • Dependency Injection
    • Constructor Injection
  • Several others

--wikipedia--


Service Locator

  • HighLevelModule only need to know the locator and the abstractions. It will find right implemtations.

--

Service Locator Snippet

class HighLevelModule {
    HighLevelModule() {
        this.abs1 = ServiceLocator.Get<Abs1>();
    }
}
class Impl1 : Abs1 {
    Impl1() {
        this.abs2 = ServiceLocator.Get<Abs2>();
    }
}
class Impl2 : Abs2 {}
// At the time of app boostrap
ServiceLocator.Register<Abs1>(() => new Impl1());
ServiceLocator.Register<Abs2>(() => new Impl2());

--

Service Locator Pros & Cons

+ -
+ Easy to implement - A new dependency introduced
+ Easy to apply for legacy system - Have to register every dependency (automate-able)
+ Easy to do UTs - Hides dependencies
+ Easy to extend - Hard to maintain

--

SERVICE LOCATOR DEMO


Dependency Injection (DI)

  • HighLevelModule only depends on the abstractions. The implementations will be injected by the DI container

--

DI Snippet

class HighLevelModule {
    HighLevelModule(Abs1 abs1) {//Constructor injection
        this.abs1 = abs1;
    }
}
class Impl1 : Abs1 { }
// At the time of app boostrap
DIContainer.Register<Abs1>(() => new Impl1());
DIContainer.Register<HighLevelModule>(
    () => new HighLevelModule(
        DIContainer.Resolve<Abs1>()
    ));

var hlm = DIContainer.Resolve<HighLevelModule>();

--

DI Pros & Cons

+ -
+ All from Service Locator - Not easy for newbie to understand
+ Easy to maintain - Have to register every dependency (automate-able)
+ Easy to get-started
+ Eliminate most of Service locator's cons

--

DI DEMO


Q&A

--

References


Thank you

dip-ioc-talk's People

Contributors

tuyen-vuduc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.