Dependency Injection in .NET

13
Dependency Injection in .NET Remigiusz Koczapski http://blog.koczapski.com

Transcript of Dependency Injection in .NET

Page 1: Dependency Injection in .NET

Dependency Injection

in .NETRemigiusz Koczapski

http://blog.koczapski.com

Page 2: Dependency Injection in .NET

Java and Spring Container

Page 3: Dependency Injection in .NET

Dependency injection

• Dependency Injection

• Poor Man's DI

• Inversion of Control

Page 4: Dependency Injection in .NET

Props of DI

• Loose coupling

• Setup problem solving

• Lifetime management

• Interception and cross-cutting concerns

Page 5: Dependency Injection in .NET

Dependencies

• What to inject

• Seams

• Volatile and Stable Dependencies

• Where to inject

• Composition Root

• How to inject

• Register Resolve Release (3-call Pattern)

Page 6: Dependency Injection in .NET

DI Container Configuration

Code: ManualXML

Code: Auto-wiring

Page 7: Dependency Injection in .NET

DI Patterns

Method InjectionConstructor Injection

Property Injection Ambient Context

Page 8: Dependency Injection in .NET

DI Patterns - which to choose

© Mark Seeman – Dependency Injection in .NET

Page 9: Dependency Injection in .NET

DI Anti-Patterns

Bastard InjectionControl Freak

Constrained Construction Service Locator

Page 10: Dependency Injection in .NET

DI in ASP.NET MVC

• Custom Controller Factory – Ideal composition root

• Dependency Resolver - Service Locator Anti-Pattern

(with no Release method)

Page 11: Dependency Injection in .NET

Lifetime Management

• Transient

• Singleton

• Thread

• Request

Page 12: Dependency Injection in .NET

Interception

• Surrounding invoked method

• Cross-cutting Concerns

• Decorator Pattern

• SRP & OCP

• Aspect Oriented Programming

• Dynamic Interception

© Mark Seeman – Dependency Injection in .NET

Page 13: Dependency Injection in .NET

DI Containers

• Castle Windsor

• Sprint.NET

• StructureMap

• Autofac

• Ninject

• Mef

• Unity

• Differences

• Registration order

• Lifetime Management names and

options

• Configuration support

• Implementation Binding

• Sequence Binding

• Named Instances