51
IoC in Microsoft Unity Francesco Garavaglia 03/2016

IOC in Unity

Embed Size (px)

Citation preview

Page 1: IOC in Unity

IoC in Microsoft Unity

Francesco Garavaglia

03/2016

Page 2: IOC in Unity

Agenda The Problem

Composition by Examples

Inversion of Control (IoC)

Dependency Injection Microsoft

Unity

2

Page 3: IOC in Unity

What problems

are we trying to

solve?

3

Page 4: IOC in Unity

4

The Problem

We live in an age where writing software to a given set of requirements is no longer enough.

We have to maintain and change existing code.

Code quality ( What’s Bad ) R.Martin 1996

- Rigid (hard to modify)

- Fragile ( errors occur on almost every change)

- Immobile (not reusable)

The Problem

Page 5: IOC in Unity

How?Our solutions should be:

• Modular

• Testable

• Adaptive to change

• Whatever you want

5

Page 6: IOC in Unity

How?Our solutions should be:

• Modular

• Testable

• Adaptive to change

• Whatever you want

6

Page 7: IOC in Unity

7

We need a glossary

Service—An object that performs a well-defined function when called upon

Client—Any consumer of a service; an object that calls upon a service to perform a well-

understood function

Dependency—A specific service that is required by another object to fulfill its function.

Dependent—A client object that needs a dependency (or dependencies) in order to perform its

function.

The Problem

Page 8: IOC in Unity

Composition:

OLD SCHOOL

8

Page 9: IOC in Unity

9

Ex1:CompositionComposition by Examples

Page 10: IOC in Unity

10

Ex1: Composition

public class SpellCheckerService{}

public class TextEditor

{

private SpellCheckerService _spellCheckerService;

public TextEditor()

{

_spellCheckerService = new SpellCheckerService();

}

}

class Program

{

static void Main(string[] args)

{

TextEditor textEditor = new TextEditor();

}

}

TextEditor

SpellChecker

Composition by Examples

Page 11: IOC in Unity

11

Ex1: Composition

• SimpleWhat is Good

• It’s not testable

• It’s hard to maintain/change

What is Bad

Composition by Examples

Page 12: IOC in Unity

Composition:

NEW SCHOOL

12

Page 13: IOC in Unity

13

Better approach

TextEditor

+ CheckSpelling() : bool

SpellCheckerServ ice

+ CheckSpelling() : string

«interface»

ISpellCheckerServ ice

+ CheckSpelling() : string

Dependency Inversion

A.High-level modules should not

depend on low-level modules. Both

should depend on abstractions.

B. Abstractions should not depend

upon details. Details should depend

upon abstractions.

Robert C. Martin 1996

Composition by Examples

Page 14: IOC in Unity

14

Context

Granny

+ Eat() : void

«interface»

IAppleProv ider

+ GetApple() : IApple

RedAppleProv ider

+ GetApple() : IApple

GoldenAppleProv ider

+ GetApple() : IApple

«interface»

IPillProv ider

ConcretePillProv ider

Context

Composition by Examples

Page 15: IOC in Unity

15

Ex2:Loose Coupling

public class TextEditor

{

private readonly ISpellCheckerService _spellCheckerService;

public TextEditor(ISpellCheckerService spellCheckerService)

{

_spellCheckerService = spellCheckerService;

}

public string CheckSpelling()

{

return _spellCheckerService.CheckSpelling();

}

}

Composition by Examples

Page 16: IOC in Unity

16

Ex2: Unit Testing

// Mock

ISpellCheckerService mock = new SpellCheckerServiceMock();

// Instantiate

TextEditor textEditor = new TextEditor(mock);

// Check

Assert.AreEqual(“Mock”, textEditor.CheckSpelling());

Composition by Examples

Page 17: IOC in Unity

17

Ex2: Good vs Bad

• Dependencies are obvious.

• Dependency resolution is not encapsulated.

• Unit Testing is applicable

• Architecture is much better

What is Good

• We are resolving dependencies manually while creating instances of TextEditor.

What is Bad

TextEditor lost its “Sovereignty” and is not able to resolve dependencies by himself.

Composition by Examples

Page 18: IOC in Unity

18

Ex3: Using FactoryComposition by Examples

Page 19: IOC in Unity

19

Page 20: IOC in Unity

20

Page 21: IOC in Unity

21

Page 22: IOC in Unity

22

Page 23: IOC in Unity

23

Page 24: IOC in Unity

24

Page 25: IOC in Unity

25

Factory

TextEditorFactory

+ GetEnglishTextEditor() : TextEditor

+ GetFrenchTextEditor() : TextEditor

TextEditor

FrenchSpellCheckerServ ice

«interface»

ISpellCheckerServ ice

EnglishSpellCheckerServ ice

Composition by Examples

Page 26: IOC in Unity

26

What changed

Any required combination of Text Editor and Spell Checking Service is created by object factory.

•It’s testable

•No manual wiring

What is Good

•You have to maintain factory or service locator

•The more combinations the more methods we have in factory.

•Your code knows about the specific factory or factory interface. This means that infrastructure logic is mixed with business logic.

•Factory may have states.

What is Bad

Composition by Examples

Page 27: IOC in Unity

27

Ex4: Service Locator

Unfortunately, being a kind of Factory, Service Locators suffer from the same problems

of testability and shared state.

Composition by Examples

Page 28: IOC in Unity

28

What are we looking for?Composition by Examples

Page 29: IOC in Unity

Inversion of

Control

HOLLYWOOD PRINCIPLE:

DON’T CALL ME, I’LL CALL YOU

29

Page 30: IOC in Unity

30

Inversion of Control

IoC – is a common characteristic of

frameworks.

According to Martin Fowler the etymology of

the phrase dates back to 1988.

Inversion of Control

Page 31: IOC in Unity

31

Dependency Injection

DI is a kind of IoC

Inversion of Control is too generic a term

DI pattern – describes the approach used to lookup a dependency.

Dependency resolution is moved to Framework.

Inversion of Control

Page 32: IOC in Unity

32

SpellCheckerServ ice

+ CheckSpelling() : bool

TextEditor

+ CheckSpelling() : bool

«interface»

ISpellCheckerServ ice

We have already prepared basis

Loosely

coupled

structure

Inversion of Control

Page 33: IOC in Unity

It’s time to

introduce new

role: Injector

Injector(sometimes referred to as

a provider or container)

33

Page 34: IOC in Unity

Unity

34

Page 35: IOC in Unity

35

Ex5: Unity

using Microsoft.Practices.Unity;

UnityContainer container = new UnityContainer();

container.RegisterType<ISpellCheckerService, SpellCheckingService>();

TextEditor textEditor = container.Resolve<TextEditor>();

DI with Unity

Page 36: IOC in Unity

36

What changed

Unity container now resolves dependencies

•Automated dependency resolution

•Business logic and infrastructure are decoupled.

What is Good

What is Bad

DI with Unity

Page 37: IOC in Unity

37

Injection Types

Interface injection (by Martin Fowler)

Constructor Injection (by Martin Fowler)

Setter injection (by Martin Fowler)

Method call injection (Unity)

Method decorator injection (Guice)

DI with Unity

Page 38: IOC in Unity

38

Unity Configuration

<configSections><section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Pr

actices.Unity.Configuration"/></configSections>

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"><alias alias="ISpellCheckerService" type="Unity.Config.ISpellCheckerService, Unity.Config" /><alias alias="SpellCheckingService" type="Unity.Config.SpellCheckingService, Unity.Config" /><namespace name="Unity.Config" /><assembly name="Unity.Config" />

<container><register type="ISpellCheckerService" mapTo="SpellCheckingService" />

</container></unity>

DI with Unity

Page 39: IOC in Unity

39

Unity Configuration

using Microsoft.Practices.Unity;

using Microsoft.Practices.Unity.Configuration;

UnityContainer container = new UnityContainer();

container.LoadConfiguration();

TextEditor textEditor = container.Resolve<TextEditor>();

DI with Unity

Page 40: IOC in Unity

40

Dependency treeDI with Unity

Page 41: IOC in Unity

41

Dependency tree

public interface IAdditionalDependency{}

public class AdditionalDependency : IAdditionalDependency{}

public class SpellCheckingService: ISpellCheckerService {

public SpellCheckingService( IAdditionalDependency dependency){}

}

UnityContainer container = new UnityContainer();

container.RegisterType<ISpellCheckerService, SpellCheckingService>();

container.RegisterType<IAdditionalDependency, AdditionalDependency>();

TextEditor textEditor = container.Resolve<TextEditor>();

DI with Unity

Page 42: IOC in Unity

42

Dependency tree

SpellCheckerServ ice

+ CheckSpelling() : bool

TextEditor

+ CheckSpelling() : bool

«interface»

ISpellCheckerServ ice

AdditionalDependency

+ CheckSpelling() : bool

«interface»

IAditionalDependenct

DI with Unity

Page 43: IOC in Unity

43

Defining Injection Constructor

public class TextEditor{

private readonly ISpellCheckerService _spellCheckerService;

[InjectionConstructor]public TextEditor(ISpellCheckerService spellCheckerService){

_spellCheckerService = spellCheckerService;}

public TextEditor(ISpellCheckerService spellCheckerService,string name){

_spellCheckerService = spellCheckerService;}

}

DI with Unity

Page 44: IOC in Unity

44

Property Injection

public class TextEditor{

public ISpellCheckerService SpellCheckerService {get; set;}

[Dependency]public ISpellCheckerService YetAnotherSpellcheckerService{get;set;}

}

UnityContainer container = new UnityContainer();container.RegisterType<TextEditor>(new InjectionProperty("SpellCheckerService"));container.RegisterType<ISpellCheckerService, SpellCheckingService>();

TextEditor textEditor = container.Resolve<TextEditor>();

DI with Unity

Page 45: IOC in Unity

45

Method call injection

public class TextEditor{

public ISpellCheckerService SpellcheckerService {get; set;}

public void Initialize (ISpellCheckerService spellcheckerService){

_spellCheckerService = spellcheckerService;}

}

UnityContainer container = new UnityContainer();container.RegisterType<ISpellCheckerService, SpellCheckingService>();

TextEditor textEditor = container.Resolve<TextEditor>();

DI with Unity

Page 46: IOC in Unity

46

Lifetime Managers

TransientLifetimeManagerReturns a new instance of the requested type for each call. (default behavior)

ContainerControlledLifetimeManagerImplements a singleton behavior for objects. The object is disposed of when you dispose of the container.

ExternallyControlledLifetimeManagerImplements a singleton behavior but the container doesn't hold a reference to object which will be disposed of when out of scope.

HierarchicalifetimeManagerImplements a singleton behavior for objects. However, child containers don't share instances with parents.

PerResolveLifetimeManagerImplements a behavior similar to the transient lifetime manager except that instances are reused across build-ups of the object graph.

PerThreadLifetimeManagerImplements a singleton behavior for objects but limited to the current thread.

DI with Unity

Page 47: IOC in Unity

47

Unity Singleton

UnityContainer container = new UnityContainer();

container.RegisterType<ISpellCheckerService, SpellCheckingService>(new

ContainerControlledLifetimeManager());

TextEditor textEditor = container.Resolve<TextEditor>();

DI with Unity

Page 48: IOC in Unity

48

Container HierarchyDI with Unity

Page 49: IOC in Unity

49

Unity Limitations

• When your objects and classes have no dependencies on other objects or classes.

• When your dependencies are very simple and do not require abstraction.

DI with Unity

Page 50: IOC in Unity

50

References

Martin Fowler – Dependency Injection

http://martinfowler.com/articles/injection.html

R. Martin - Dependency Inversion principle

http://www.objectmentor.com/resources/articles/dip.pdf

Developer's Guide to Dependency Injection Using Unity

https://msdn.microsoft.com/en-us/library/dn223671(v=pandp.30).aspx