Skip to content

tuyen-vuduc/dip-ioc-talk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

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;
    }
Loading

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
Loading

--

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)
    }
Loading

--

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published