Top Banner
Lars-Erik Kindblad Senior Consultant Blog: kindblad.com Inversion of Control – Introduction & Best Practice
57

Inversion of Control - Introduction and Best Practice

May 26, 2015

Download

Technology

This presentation goes through what Inversion of Control is, which IOC patterns that exists, which of the patterns you should use and when you should use them.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Inversion of Control - Introduction and Best Practice

Lars-Erik KindbladSenior ConsultantBlog: kindblad.com

Inversion of Control – Introduction & Best Practice

Page 2: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Agenda

Introduction• Inversion of Control• Dependency Injection Patterns• Service Locator Patterns• Inversion of Control Containers

Best Practice• What patterns to use• When to use the patterns

Summary

Page 3: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Inversion of Control

Inversion of Control = IOC Make code loosely coupled Allows for more reuse Make unit testing possible How? Move creation of dependencies outside the class they are

being used in A better name - Inversion of Dependency Creation

Page 4: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Traditional Code

Inversion of Control

Page 5: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Traditional Code

Inversion of Control Code

Page 6: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Traditional Code

Page 7: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection - Constructor Injection

Page 8: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection - Interface Injection

Page 9: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection - Setter Injection

Page 10: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Static Service Locator

Page 11: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Injected Service Locator

Page 12: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Inversion of Control Container

A framework that can automatically create a given type with all the required dependencies

Popular frameworks• Unity• Castle Windsor• Ninject• StructureMap• etc.

Page 13: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Inversion of Control Container

Manual approach

Using an IOC Container

Page 14: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Page 15: Inversion of Control - Introduction and Best Practice

Which patterns to use

Page 16: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Traditional Code

Page 17: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Traditional Code

Page 18: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection - Constructor Injection

Page 19: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection - Constructor Injection

Page 20: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection - Interface Injection

Page 21: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection - Interface Injection

Page 22: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection - Setter Injection

Page 23: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection - Setter Injection

Page 24: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Static Service Locator

Page 25: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Static Service Locator

Page 26: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Injected Service Locator

Page 27: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Injected Service Locator

Page 28: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

When to use which patterns

Page 29: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Always use Dependency Injection – Constructor Injection

...except

Page 30: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

FACADE CLASSES

Page 31: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Facade Classes

Page 32: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection

Page 33: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection

Page 34: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Service Locator

Page 35: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Service Locator

Page 36: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

LOOPS

Page 37: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Loops

Page 38: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection

Page 39: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection

Page 40: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Service Locator

Page 41: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Service Locator

Page 42: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection using Func<>

Page 43: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection using Func<>

Page 44: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

BASE CLASSES

Page 45: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Base Classes

Page 46: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection

Page 47: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Dependency Injection

Page 48: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Service Locator

Page 49: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Service Locator

Page 50: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

UNKNOWN TYPES AT COMPILE TIME

Page 51: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Unknown types at compile time

Page 52: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Unknown types at compile time

Page 53: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Service Locator

Page 54: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Service Locator

Page 55: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

Summary

Constructor Injection Injected Service Locator

Facade classes(WCF Services,MVC Controllers)

X

Loops X (Func<>) X

Base classes X

Unknown types at compile time

X

All other scenarios X

Page 56: Inversion of Control - Introduction and Best Practice

| Sector, Alliance, Offering

QUESTIONS?

Page 57: Inversion of Control - Introduction and Best Practice

www.capgemini.com

The information contained in this presentation is proprietary. ©2010 Capgemini. All rights reserved