Inversion of control with Autofac slides and demos
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

author: @ScottWeinstein title: IoC crash course footer: @ScottWeinstein subfooter: Lab49 presdate: 4/19/2011


  • Why IoC
  • Before and After
  • Lifetime management

Why IoC

  • IoC containers were create to solve problems with the classic application architectures

Classic App Architecture

Problems with the Classic App Architecture

  • Sharing configuration info

Classic App Architecture

Problems (2) with the Classic App Architecture

  • The containment model means that IDisposability infects everthing

Classic App Architecture

Problems (3) with the Classic App Architecture

  • Static global state
    • The singleton anti-pattern
  • Implicit shared static global state
    • The config
  • High coupling
  • Hard to manage late bindings
  • Plethora of custom factory classes to address the above

Sample Classic Code

With Ioc

  1. A bit more setup code, but in return, much simpler implementations
  2. Built-in flexibility to send in replacement implementations
  3. No more shared global state

Ioc Arch

With Ioc Code

Using Autofac...

Anonymous factories

builder.Register<DBEntities>(ctx => new DBEntities(ctx.Resolve<Config>().ConnectionString));
builder.Register<HttpContextBase>(ctx => FakeHttpContext());

Lifetime management and Creating more than one

When does my stuff get cleaned up?

With MVC apps, at the end of each request (by default) Can override in many ways, .SingleInstance() and Owned<T> are two methods.

Let's look at AdvancedModel and LifetimeManagementModel

Unit testing w/ Autofac


Integration with non Web apps

  • WCF - similar model to MVC
  • WPF/Silverlight - fine w/ ViewModel first frameworks, such as Caliburn.* If you're coding View-first, integrating IoC can be hard


  • Setups can get complex
  • Resolution failure is at runtime, exceptions messages can be... obscure

Where's the code and stuff?

How to build the presentation

  1. Install pandoc
  2. msbuild Demos.sln
  3. pandoc -t slidy -s .\ -o pres.html