13. Watch for Volatility
new StringBuilder() App specific
new List<T>() new User, Product, Order
System.Xml Non-deterministic
DateTime.Now, File, Data
Slow
Remote service
14. Composition Root
• Where the app “starts”
• Classes are composed here
• Keeps app unaware of the IoC container
• Unique techniques for different techs
15. Composition Root
Technology Composition Root
Console App Main method
ASP.NET MVC Call custom IControllerFactory in Global.asax
WCF ServiceHostFactory and a few other tricks
ASP.NET Web Forms Humble object or Service Locator in each aspx.cs
20. Config Styles
Mapping Advantages Disadvantages
XML Config Late binding No compile-time checks
Control Verbose
Code Code Compile-time checks Must recompile
Control
Intellisense
Auto-Registration Rules Late binding Partial compile-time checks
Automatic = Less work Less control
Reinforces conventions Magic
21. Config Style Support
Style Castle Windsor StructureMap Spring.Net Autofac Unity
XML X X X X X
Code X X X X
Auto-registration X X X
22. Explicit
XML Code as
Configuration
Late Binding Early Binding
Auto
Registration
Implicit
23. Lifestyles
Lifestyle Description Usage
Singleton Once on App startup. Most Thread safe, stateless,
efficient. immutable, circuit breaker, in-
memory cache
Transient Per Request. Least efficient. Default (most)
Per Graph One instance of each Anywhere thread that resolves is
dependency per thread the only consumer of the graph.
Efficient alt to transient.
Web Request One instance per web request Web app requests when
Context singleton won’t work
Thread Context Per thread
Session Context Per session
Pooled Pool of ready objects Expensive to create
Scoped
Custom
24. Lifestyle Support
Lifestyle Castle StructureMap Spring.Net Autofac Unity
Windsor
Singleton X X X X X
Transient X X X X X
Per Graph X X
Web X X (X) X
Request
Context
Thread X X X
Context
Session X (X)
Context
Pooled X
Scoped X X
Custom X X X
28. Control Freak: Solutions
• Factory – moves • Constructor or property
problem to another injection
class
• Static factory that calls
config – No easy way to
inject mock for unit
testing.
33. Bastard Injection
1. Overloaded constructor = ambiguity
2. Often introduces foreign dependency
3. Hinders parallel development
• Fix
– Foreign default: Constructor injection
– Local default: Property injection
34. Other Anti-Patterns
• Factory that references config
• Constructor over injection
• MVC’s Idependency Resolver
• Abstracting away your container
• Web Forms
38. PostSharp
Normal PostSharp .Net
Attributes
Compilation Compilation Assembly
39. AOP via attributes
1. Modify compilation
• PostSharp
2. Custom runtime host
• WCF
• ASP.Net MVC
• Unit testing frameworks
40.
41. AOP via attributes
• No special design effort • Code runs different from
• Easy to spot since inline the code you wrote
• Explicit since in context • Vendor lock-in
• Attributes compiled with
code = Tightly coupled.
• Can’t vary the aspect
independently of the
implementation
• Can’t use conventions
42. How do I choose a container?
Performance
Lifetimes
Documentation
API
Support
Dynamic proxies
43. Summary
1. Compose at composition root
2. Prefer constructor injection, auto-registration
3. Volatility? Think smoke detectors vs sockets
47. To Do
• Remove references to data layer(s) in MVC
project. Surprising that ploeh did it in his, but
clearly a risky issue since it means others
might tightly couple
• Add checks in project to protect from bad refs.
Hinweis der Redaktion
Ioc: Broad term. Prog style where framework or runtime control the program flow. For example, ASP.Net lifecycleDI – coined by Martin Folweris a set of software design principles and patterns that enable us to develop loosely coupled code. See pg. 42NOTE: Ioc nicely describes exactly what we’re trying to do.
See table page 16Late binding – replace services without recompile
SRP - an object should have only a single responsibilityOpen/Closed - open for extension, but closed for modificationLiskovSubstution Principle - objects in a program should be replaceable with instances of their subtypesInterface segregation principle - many client-specific interfaces are better than one general-purpose interface.Dependency Inversion principle - Depend upon Abstractions. Do not depend upon concretions.
Abstract class or interface. I’ll use the word interface for simplicity, but either will do.
Note, only the enemy when a volatile dependency. .Net BCL dependencies aren’t. new StringBuilder() is fine, unless for some reason it’s not. The amount of coupling in an application is loosely correlated to the number of times you see the new keyword in the codebase.
Pg 82 Each app should only have a single reference to each of the above methods. Why, config should be considered a single atomic action. Once resolved, consider the container read only. Be sure to release what you resolve. Naming convention from Castle Windsor.
Property: (aka setter – use when a good local default, but want to allow callers to provide alternative assemblies)
benefits on pg 68
TightlyCoupled project – HomeController.cs
HomeController alternative Index method
Common when attempting to make class testable without understanding DIBastard Injection 146