<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="/rss.xsl"?><rss version="2.0"><channel><title>simpleinjector Wiki Rss Feed</title><link>http://simpleinjector.codeplex.com/wikipage</link><description>simpleinjector Wiki Rss Description</description><item><title>Updated Wiki: Self Hosted Web API Integration</title><link>https://simpleinjector.codeplex.com/wikipage?title=Self Hosted Web API Integration&amp;version=4</link><description>&lt;div class="wikidoc"&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;WARNING:&lt;/b&gt; Unfortunately the implementation below is flawed and can&amp;#39;t be used in a self hosted Web API. Please see &lt;a href="http://simpleinjector.codeplex.com/discussions/430603"&gt;here&lt;/a&gt; for more information.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h1&gt;ASP.NET Web API Integration Guide for Self Hosted Environments&lt;/h1&gt;When running ASP.NET Web API in a self hosted environment, there is no HttpContext, which means that &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Self%20Hosted%20Web%20API%20Integration&amp;ANCHOR#PerWebRequest"&gt;Per Request lifestyle&lt;/a&gt; lifestyles can not be used. In that case the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Self%20Hosted%20Web%20API%20Integration&amp;ANCHOR#PerLifetimeScope"&gt;Per Lifetime Scope lifestyle&lt;/a&gt; can be used instead, but this requires a &lt;b&gt;IDependencyResolver&lt;/b&gt; that uses scoping (since Per Lifetime Scope is thread-specific, it can&amp;#39;t be wrapped around a HTTP request, since ASP.NET can finish the request on a different thread than where it was started).&lt;br /&gt;&lt;br /&gt;The following code snippet defines a &lt;b&gt;SelfHostedSimpleInjectorWebApiDependencyResolver&lt;/b&gt; class for a self hosted ASP.NET Web API application. You can create a new C# file in your Web API project and copy paste the code below into it.&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Web.Http.Dependencies;

&lt;span style="color:Blue;"&gt;using&lt;/span&gt; SimpleInjector;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; SimpleInjector.Extensions.LifetimeScoping;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; SelfHostedSimpleInjectorWebApiDependencyResolver
    : IDependencyResolver
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Container container;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; LifetimeScope lifetimeScope;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; SelfHostedSimpleInjectorWebApiDependencyResolver(
        Container container)
        : &lt;span style="color:Blue;"&gt;this&lt;/span&gt;(container, &lt;span style="color:Blue;"&gt;false&lt;/span&gt;)
    {
    }

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; SelfHostedSimpleInjectorWebApiDependencyResolver(
        Container container, &lt;span style="color:Blue;"&gt;bool&lt;/span&gt; createScope)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;

        &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (createScope)
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.lifetimeScope = container.BeginLifetimeScope();
        }
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IDependencyScope BeginScope()
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; SelfHostedSimpleInjectorWebApiDependencyResolver(
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container, &lt;span style="color:Blue;"&gt;true&lt;/span&gt;);
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;object&lt;/span&gt; GetService(Type serviceType)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; ((IServiceProvider)&lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container)
            .GetService(serviceType);
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color:Blue;"&gt;object&lt;/span&gt;&amp;gt; GetServices(Type serviceType)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container.GetAllInstances(serviceType);
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Dispose()
    {
        &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (&lt;span style="color:Blue;"&gt;this&lt;/span&gt;.lifetimeScope != &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.lifetimeScope.Dispose();
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;The following code snippet shows how to use the &lt;b&gt;SelfHostedSimpleInjectorWebApiDependencyResolver&lt;/b&gt; extension method.&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Create the container as usual.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

&lt;span style="color:Green;"&gt;// Register your types, for instance:&lt;/span&gt;
container.Register&amp;lt;IUserRepository, SqlUserRepository&amp;gt;();
container.RegisterLifetimeScope&amp;lt;IUnitOfWork, EfUnitOfWork&amp;gt;();

&lt;span style="color:Green;"&gt;// Create your self-host configuration&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; configuration = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; HttpSelfHostConfiguration(uriBuilder.Uri);

configuration.Routes.MapHttpRoute(
	name: &lt;span style="color:#A31515;"&gt;&amp;quot;Default Route&amp;quot;&lt;/span&gt;,
	routeTemplate: &lt;span style="color:#A31515;"&gt;&amp;quot;api/{controller}/{id}&amp;quot;&lt;/span&gt;,
	defaults: &lt;span style="color:Blue;"&gt;new&lt;/span&gt; { id = RouteParameter.Optional });

configuration.DependencyResolver =
	&lt;span style="color:Blue;"&gt;new&lt;/span&gt; SelfHostedSimpleInjectorWebApiDependencyResolver(container);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; server = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; HttpSelfHostServer(configuration);

&lt;span style="color:Green;"&gt;// Start the server etc...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Tue, 21 May 2013 12:45:58 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Self Hosted Web API Integration 20130521124558P</guid></item><item><title>New Comment on "Self Hosted Web API Integration"</title><link>https://simpleinjector.codeplex.com/wikipage?title=Self Hosted Web API Integration&amp;ANCHOR#C27541</link><description>You can set the DependencyResolver property on the created HttpSelfHostConfiguration instance.</description><author>dot_NET_Junkie</author><pubDate>Tue, 21 May 2013 12:42:42 GMT</pubDate><guid isPermaLink="false">New Comment on "Self Hosted Web API Integration" 20130521124242P</guid></item><item><title>New Comment on "Self Hosted Web API Integration"</title><link>https://simpleinjector.codeplex.com/wikipage?title=Self Hosted Web API Integration&amp;ANCHOR#C27524</link><description>Hey. The &amp;#96;GlobalConfiguration&amp;#96; does not exist in &amp;#96;Microsoft.AspNet.WebApi.SelfHost&amp;#96;. So, what should we do&amp;#63; Adding a reference to &amp;#96;Microsoft.AspNet.WebApi.WebHost&amp;#96; or there is another way to do this&amp;#63;&amp;#10;Thanks in advance.</description><author>kavand</author><pubDate>Sun, 19 May 2013 12:01:48 GMT</pubDate><guid isPermaLink="false">New Comment on "Self Hosted Web API Integration" 20130519120148P</guid></item><item><title>Updated Wiki: How-to</title><link>https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;version=32</link><description>&lt;div class="wikidoc"&gt;&lt;h1&gt;How To...&lt;/h1&gt;
&lt;ul&gt;&lt;li&gt;How to &lt;a href="#Register-Factory-Delegates"&gt;Register factory delegates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Resolve-Instances-By-Key"&gt;Resolve instances by key&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Resolve-Arrays-And-Lists"&gt;Resolve array and lists&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Register-Multiple-Interfaces-With-The-Same-Implementation"&gt;Register multiple interfaces with the same implementation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Override-Existing-Registrations"&gt;Override existing registrations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Verifying-Configuration"&gt;Verify the container&amp;#39;s configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Multi-Threaded-Applications"&gt;Work with dependency injection in multi-threaded applications&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;h2&gt;Register factory delegates&lt;a name="Register-Factory-Delegates"&gt;&lt;/a&gt;&lt;a name="Register_Factory_Delegates"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;b&gt;Simple Injector&lt;/b&gt; allows you to register a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate for the creation of an instance. This is especially useful in scenarios where it is impossible for the container to create the instance. There are overloads of the &lt;i&gt;Register&lt;/i&gt; method available that accept an &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; argument:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IMyService&amp;gt;(() =&amp;gt; SomeSubSystem.CreateMyService());
&lt;/pre&gt;&lt;/div&gt;In situations where a service needs to create multiple instances of a certain dependencies, or needs to explicitly control the lifetime of such dependency, abstract factories can be used. Instead of injecting an &lt;i&gt;IMyService&lt;/i&gt;, you should inject an &lt;i&gt;IMyServiceFactory&lt;/i&gt; that allows services to create new instances of &lt;i&gt;IMyService&lt;/i&gt;:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IMyServiceFactory
{
    IMyService CreateNew();
}

&lt;span style="color:Green;"&gt;// Implementation&lt;/span&gt;
&lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ServiceFactory : IMyServiceFactory
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IMyService CreateNew()
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; MyServiceImpl();
    }
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterSingle&amp;lt;IMyServiceFactory, ServiceFactory&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IMyServiceFactory factory;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(IMyServiceFactory factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service1 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.CreateNew())
        {
            &lt;span style="color:Green;"&gt;// use service 1&lt;/span&gt;
        }

        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service2 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.CreateNew())
        {
            &lt;span style="color:Green;"&gt;// use service 2&lt;/span&gt;
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;Instead of creating specific interfaces for your factories, you can also choose to inject &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates into your services:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterSingle&amp;lt;Func&amp;lt;IMyService&amp;gt;&amp;gt;(
    () =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; MyServiceImpl());

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;IMyService&amp;gt; factory;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(Func&amp;lt;IMyService&amp;gt; factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service1 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.Invoke())
        {
            &lt;span style="color:Green;"&gt;// use service 1&lt;/span&gt;
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;This saves you from having to define a new interface and implementation per factory.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: On the downside however, this communicates less clearly the intend of your code and as a result might make your code harder to grasp for other developers.&lt;/i&gt;&lt;/blockquote&gt;
When you choose &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates over specific factory interfaces, you can define the following extension method that makes it easier to register &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; factories:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Extension method&lt;/span&gt;
container.RegisterFuncFactory&amp;lt;TService, TImplementation&amp;gt;(
    &lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container,
    Lifestyle lifestyle = &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;
{
    lifestyle = lifestyle ?? Lifestyle.Transient;

    &lt;span style="color:Green;"&gt;// Register the type explicitly to keep the configuration&lt;/span&gt;
    &lt;span style="color:Green;"&gt;// verifiable.&lt;/span&gt;
    container.Register&amp;lt;TService, TImplementation&amp;gt;(lifestyle);
    
    &lt;span style="color:Green;"&gt;// Register the Func&amp;lt;T&amp;gt; that resolves that instance.&lt;/span&gt;
    container.RegisterSingle&amp;lt;Func&amp;lt;TService&amp;gt;&amp;gt;(
        () =&amp;gt; container.GetInstance&amp;lt;TService&amp;gt;());
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterFuncFactory&amp;lt;IMyService, RealService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;The previous extension method allowed registration of a single factory, but won&amp;#39;t be maintainable when you want all registrations to be resolvable using &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates by default. &lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: We personally think that allowing to register Func&amp;lt;T&amp;gt; delegates by default is a design smell. The use of Func&amp;lt;T&amp;gt; delegates makes your design harder to follow and your system harder to maintain and test. If you have many constructors in your system that depend on a Func&amp;lt;T&amp;gt;, please take a good look at your dependency strategy. If in doubt, please ask us here on CodePlex or on Stackoverflow.&lt;/i&gt;&lt;/blockquote&gt;
The following extension method allows Simple Injector to resolve all types using a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate by default:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; AllowResolvingFuncFactories(
    &lt;span style="color:Blue;"&gt;this&lt;/span&gt; ContainerOptions options)
{
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = options.Container;

    container.ResolveUnregisteredType += (sender, e) =&amp;gt;
    {
        &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (e.UnregisteredServiceType.IsGenericType &amp;amp;&amp;amp;
            e.UnregisteredServiceType.GetGenericTypeDefinition() ==
                &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Func&amp;lt;&amp;gt;))
        {
            Type serviceType = e.UnregisteredServiceType
                .GetGenericArguments().First();

            InstanceProducer registration =
                container.GetRegistration(serviceType);

            &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (registration != &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
            {
                Type funcType =
                    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Func&amp;lt;&amp;gt;).MakeGenericType(serviceType);

                &lt;span style="color:Blue;"&gt;var&lt;/span&gt; factoryDelegate = 
                    Expression.Lambda(funcType,
                        registration.BuildExpression())
                    .Compile();

                e.Register(Expression.Constant(factoryDelegate));
            }
        }
    };
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.Options.AllowResolvingFuncFactories();
&lt;/pre&gt;&lt;/div&gt;After calling the &lt;b&gt;AllowResolvingFuncFactories&lt;/b&gt; extension method, the container allows resolving &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates.&lt;br /&gt;&lt;br /&gt;&lt;a name="lazy"&gt;&lt;/a&gt;Just like &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates can be injected, &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; instances can be injected into services. &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; is useful in situations where the creation of a service is time consuming, but is not always used. Thus &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; allows you to postpone the creation of that service until the moment that it is really needed:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Extension method&lt;/span&gt;
container.RegisterLazy&amp;lt;T&amp;gt;(&lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;
{
    Func&amp;lt;T&amp;gt; factory = () =&amp;gt; container.GetInstance&amp;lt;T&amp;gt;();

    container.Register&amp;lt;Lazy&amp;lt;T&amp;gt;&amp;gt;(() =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Lazy&amp;lt;T&amp;gt;(factory));
}

&lt;span style="color:Green;"&gt;// Registration    &lt;/span&gt;
container.RegisterLazy&amp;lt;IMyService&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Lazy&amp;lt;IMyService&amp;gt; myService;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(Lazy&amp;lt;IMyService&amp;gt; myService)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.myService = myService;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (someCondition)
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.myService.Value.Operate();
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;Note that instead of polluting the API of your application with &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; dependencies, it is usually cleaner to hide the &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; behind a proxy:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Proxy definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyLazyServiceProxy : IMyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Lazy&amp;lt;IMyService&amp;gt; wrapped;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyLazyServiceProxy(Lazy&amp;lt;IMyService&amp;gt; wrapped)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.wrapped = wrapped;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Operate()
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.wrapped.Value.Operate();
    }
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterLazy&amp;lt;IMyService&amp;gt;();
container.Register&amp;lt;IMyService, MyLazyServiceProxy&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;This way the application can simply depend on &lt;i&gt;IMyService&lt;/i&gt; instead of &lt;i&gt;Lazy&amp;lt;IMyService&amp;gt;&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Warning&lt;/b&gt;: The same warning applies to the use of Lazy&amp;lt;T&amp;gt; as it does for the use of Func&amp;lt;T&amp;gt; delegates. For more information about creating an application and container configuration that can be successfully verified, please read the &lt;a href="#Verifying-Configuration"&gt;How To Verify the container&amp;#8217;s configuration&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;&lt;h2&gt;Resolve instances by key&lt;a name="Resolve-Instances-By-Key"&gt;&lt;/a&gt;&lt;a name="Resolve_Instances_By_Key"&gt;&lt;/a&gt;&lt;/h2&gt;Resolving instances by a key is a feature that is deliberately left out of the &lt;b&gt;Simple Injector&lt;/b&gt;, because we feel it steers developers to a design were the application tends to have a dependency on the DI container itself. For resolving a keyed instance, you will often need to call into the container itself, which leads to the &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator anti-pattern&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;This doesn’t mean however, that resolving instances by a key is never useful. Resolving instances by a key however, is much more a job for a specific factory than for a container itself. This makes the design much cleaner, saves you from having to take a dependency on the DI framework, and enables many scenarios that the DI container writer just didn’t consider.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The need for keyed registration can be an indication of ambiguity in the application design. Take a good look if each keyed registration shouldn&amp;#39;t have its own unique interface, or perhaps each registration should implement each own version of a generic interface.&lt;/i&gt;&lt;/blockquote&gt;
Take a look at the following scenario, where we want to retrieve instances of type &lt;i&gt;IRequestHandler&lt;/i&gt; by a string key. There are of course several ways to achieve this, but here is a simple but effective way, by defining an &lt;i&gt;IRequestHandlerFactory&lt;/i&gt;:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IRequestHandlerFactory
{
    IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name);
}

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; factory =
    container.GetInstance&amp;lt;IRequestHandlerFactory&amp;gt;();
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = factory.CreateNew(&lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;);
handler.Handle(requestContext);
&lt;/pre&gt;&lt;/div&gt;By inheriting from the BCL’s &lt;i&gt;Dictionary&amp;lt;TKey, TValue&amp;gt;&lt;/i&gt;, creating an &lt;i&gt;IRequestHandlerFactory&lt;/i&gt; implementation, is almost a one-liner:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory
    : Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, Func&amp;lt;IRequestHandler&amp;gt;&amp;gt;,
    IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;[name]();
    }
}
&lt;/pre&gt;&lt;/div&gt;With this class, we can register &lt;i&gt;Func&amp;lt;IRequestHandler&amp;gt;&lt;/i&gt; factory methods by a key. With this in place the registration of keyed instances is a breeze:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();
 
container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory
{
    { &lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;DefaultRequestHandler&amp;gt;() },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;OrdersRequestHandler&amp;gt;() },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;CustomersRequestHandler&amp;gt;() },
});
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: this design will work with almost all DI containers, making this not only a design that is easy to follow, but making it very easy to migrate to another DI implementation.&lt;/i&gt;&lt;/blockquote&gt;
If you don’t like a design that uses &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates this way, it can easily be changed to be a &lt;b&gt;Dictionary&amp;lt;string, Type&amp;gt;&lt;/b&gt; instead. The &lt;i&gt;RequestHandlerFactory&lt;/i&gt; can be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory
    : Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, Type&amp;gt;, IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Container container;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; RequestHandlerFactory(Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container.GetInstance(&lt;span style="color:Blue;"&gt;this&lt;/span&gt;[name]);
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; (IRequestHandler)handler;
    }
}
&lt;/pre&gt;&lt;/div&gt;The registration will then look as follows:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory(container)
{
    { &lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DefaultRequestHandler) },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(OrdersRequestHandler) },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CustomersRequestHandler) },
});
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Please remember the previous note about ambiguity in the application design. In the given example the design would probably be better af by using a generic &lt;b&gt;IRequestHandler&amp;lt;TRequest&amp;gt;&lt;/b&gt; interface. This would allow the implementations to be &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Batch_Registration"&gt;batch registered using a single line of code&lt;/a&gt;, saves you from using keys, and results in a configuration the is &lt;a href="#Verifying-Configuration"&gt;verifiable by the container&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;
A last option to do keyed registrations is to manually create registrations and store them in a dictionary. The following example shows previous &lt;i&gt;RequestHandlerFactory&lt;/i&gt; with this approach:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory : IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, InstanceProducer&amp;gt; producers =
        &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, InstanceProducer&amp;gt;(
            StringComparer.OrdinalIgnoreCase);

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Container container;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; RequestHandlerFactory(Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }

    IRequestHandler IRequestHandlerFactory.CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.producers[name].GetInstance();
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; (IRequestHandler)handler;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Register&amp;lt;TImplementation&amp;gt;(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name,
        Lifestyle lifestyle = &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
        &lt;span style="color:Blue;"&gt;where&lt;/span&gt; TImplementation : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;, IRequestHandler
    {
        lifestyle = lifestyle ?? Lifestyle.Transient;

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; registration = lifestyle
            .CreateRegistration&amp;lt;IRequestHandler, TImplementation&amp;gt;(container);

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; producer =
            &lt;span style="color:Blue;"&gt;new&lt;/span&gt; InstanceProducer(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IRequestHandler), registration);

        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.producers.Add(name, producer);
    }
}
&lt;/pre&gt;&lt;/div&gt;The registration will then look as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; factory = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory(container);

factory.Register&amp;lt;DefaultRequestHandler&amp;gt;(&lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;);
factory.Register&amp;lt;OrdersRequestHandler&amp;gt;(&lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;);
factory.Register&amp;lt;CustomersRequestHandler&amp;gt;(&lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;);

container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(factory);
&lt;/pre&gt;&lt;/div&gt;The advantage of this method is that it completely integrates with the container. For instance, decorators can be applied to individual returned instances and types can be registered multiple times if needed.&lt;br /&gt;
&lt;h2&gt;Resolve array and lists&lt;a name="Resolve-Arrays-And-Lists"&gt;&lt;/a&gt;&lt;a name="Resolve_Arrays_And_Lists"&gt;&lt;/a&gt;&lt;/h2&gt;The &lt;b&gt;Simple Injector&lt;/b&gt; allows registration of collections of elements using the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Container_RegisterAll.htm"&gt;RegisterAll&lt;/a&gt; method overloads. Collections can be resolved by using one of the &lt;b&gt;GetAllInstances&amp;lt;T&amp;gt;()&lt;/b&gt; method, by calling &lt;b&gt;GetInstance&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt;()&lt;/b&gt;, or by defining an &lt;i&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/i&gt; parameter in the constructor of a type that is created using automatic constructor injection.&lt;br /&gt;&lt;br /&gt;Injection of other collection types, such as &lt;i&gt;arrays of T&lt;/i&gt; or &lt;i&gt;IList&amp;lt;T&amp;gt;&lt;/i&gt; into constructors is not supported out of the box. By hooking onto the unregistered type resolution event however, this functionality can be added. Look &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=CollectionRegistrationExtensions&amp;referringTitle=How-to"&gt;here&lt;/a&gt; for an example extension method that allows this behavior for &lt;i&gt;T[]&lt;/i&gt; types.&lt;br /&gt;&lt;br /&gt;Please take a look at your design if you think you need to work with a collection of items. Often you can succeed by creating a composite type that can be injected. Take the following interface for instance:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; ILogger
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message);
}
&lt;/pre&gt;&lt;/div&gt;Instead of injecting a collection of dependencies, the consumer might not really be interested in the collection, but simply wishes to operate on all elements. In that scenario you can configure your container to inject a composite of that particular type. That composite might look as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CompositeLogger : ILogger
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger[] loggers;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; CompositeLogger(&lt;span style="color:Blue;"&gt;params&lt;/span&gt; ILogger[] loggers)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers = loggers ?? &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ILogger[0];
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; logger &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers)
        {
            logger.Log(message);
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;A composite allows you to remove this boilerplate iteration logic from the application, which makes the application cleaner and when changes have to be made to the way the collection of loggers is processed, only the composite has to be changed.
&lt;h2&gt;Register multiple interfaces with the same implementation&lt;a name="Register-Multiple-Interfaces-With-The-Same-Implementation"&gt;&lt;/a&gt;&lt;a name="Register_Multiple_Interfaces_With_The_Same_Implementation"&gt;&lt;/a&gt;&lt;/h2&gt;
To adhere to the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Interface%20Segregation%20Principle&amp;referringTitle=How-to"&gt;Interface Segregation Principle&lt;/a&gt;, it is important to keep interfaces narrow. Although in most situations implementations implement a single interface, it can sometimes be beneficial to have multiple interfaces on a single implementation. Here is an example of how to register this:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Impl implements IInterface1, IInterface2 and IInterface3.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; registration =
    Lifestyle.Singleton.CreateRegistration&amp;lt;Impl, Impl&amp;gt;(container);

container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface1), registration);
container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface2), registration);
container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface3), registration);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; a = container.GetInstance&amp;lt;IInterface1&amp;gt;();
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; b = container.GetInstance&amp;lt;IInterface2&amp;gt;();

&lt;span style="color:Green;"&gt;// Since Impl is a singleton, both requests return the same instance.&lt;/span&gt;
Assert.AreEqual(a, b);
&lt;/pre&gt;&lt;/div&gt;The first line creates a &lt;i&gt;Registration&lt;/i&gt; instance for the &lt;i&gt;Impl&lt;/i&gt;, in this case with a singleton lifestyle. The other lines add this registration to the container, once for each interface. This maps multiple service types to the exact same registration.
&lt;h2&gt;Override existing registrations&lt;a name="Override-Existing-Registrations"&gt;&lt;/a&gt;&lt;a name="Override_Existing_Registrations"&gt;&lt;/a&gt;&lt;/h2&gt;The default behavior of the &lt;b&gt;Simple Injector&lt;/b&gt; is to fail when a service is registered for a second time. Most of the time the developer didn&amp;#39;t intend to override a previous registration and allowing this would lead to a configuration that would pass the container&amp;#39;s verification, but doesn&amp;#39;t behave as expected.&lt;br /&gt;&lt;br /&gt;There are certain scenarios however where overriding is useful. An example of such is a bootstrapper project for a Business layer that is reused in multiple applications (in both a web application, web service, and Windows service for instance). Not having a business layer specific bootstrapper project would mean the complete DI configuration would be duplicated in the startup path of each application, which would lead to code duplication. In that situation the applications would roughly have the same configuration, with a few adjustments.&lt;br /&gt;&lt;br /&gt;Best is to start of by configuring all possible dependencies in the BL bootstrapper and leave out the service registrations where the implementation differs for each application. In other words, the BL bootstrapper would result in an incomplete configuration. After that, each application can finish the configuration by registering the missing dependencies. This way you still don&amp;#39;t need to override the existing configuration.&lt;br /&gt;&lt;br /&gt;In certain scenarios it can be beneficial to allow an application override an existing configuration. The container can be configured to allow overriding as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.Options.AllowOverridingRegistrations = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;

&lt;span style="color:Green;"&gt;// Register IUserService.&lt;/span&gt;
container.Register&amp;lt;IUserService, FakeUserService&amp;gt;();

&lt;span style="color:Green;"&gt;// Replaces the previous registration&lt;/span&gt;
container.Register&amp;lt;IUserService, RealUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;The previous example created a &lt;i&gt;Container&lt;/i&gt; instance that allows overriding. It is also possible to enable overriding half way the registration process:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Create a container with overriding disabled&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

&lt;span style="color:Green;"&gt;// Pass container to the business layer.&lt;/span&gt;
BusinessLayer.Bootstrapper.Bootstrap(container);

&lt;span style="color:Green;"&gt;// Enable overriding&lt;/span&gt;
container.Options.AllowOverridingRegistrations = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;

&lt;span style="color:Green;"&gt;// Replaces the previous registration&lt;/span&gt;
container.Register&amp;lt;IUserService, RealUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;h2&gt;Verify the container’s configuration&lt;a name="Verifying-Configuration"&gt;&lt;/a&gt;&lt;a name="Verifying_Configuration"&gt;&lt;/a&gt;&lt;/h2&gt;Dependency Injection promotes the concept of programming against abstractions. This makes your code much easier to test, easier to change and more maintainable. However, since the code itself isn&amp;#39;t responsible of maintaining the dependencies between implementations, the compiler will not be able to verify whether the dependency graph is correct.&lt;br /&gt;&lt;br /&gt;When starting to use a Dependency Injection container, many developers see their application fail when it is deployed in staging or sometimes even production, because of container misconfigurations. This makes developers often conclude that dependency injection is bad, since the dependency graph cannot be verified. This conclusion however, is incorrect. Although it is impossible for the compiler to verify the dependency graph, verifying the dependency graph is still possible and advisable.&lt;br /&gt;&lt;br /&gt;Simple Injector contains a &lt;b&gt;Verify()&lt;/b&gt; method, that will simply iterate over all registrations and resolve an instance for each registration. Calling this method directly after configuring the container, allows the application to fail during start-up, when the configuration is invalid.&lt;br /&gt;&lt;br /&gt;Calling the &lt;b&gt;Verify()&lt;/b&gt; method however, is just part of the story. It is very easy to create a configuration that passes any verification, but still fails at runtime. Here are some tips to help building a verifiable configuration:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Stay away from &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Implicit_Property_Injection"&gt;implicit property injection&lt;/a&gt;, where the container is allowed to skip injecting the property if a corresponding or correctly registered dependency can&amp;#39;t be found. This will disallow your application to fail fast and will result in &lt;b&gt;NullReferenceException&lt;/b&gt;s later on. Only use implicit property injection when the property is truly optional, omitting the dependency still keeps the configuration valid, and the application still runs correctly without that dependency. Truly optional dependencies should be very rare though, since most of the time you should prefer injecting empty implementations (a.k.a. the &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object pattern&lt;/a&gt;) instead of allowing dependencies to be a null reference. &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=How-to&amp;ANCHOR#Configuring_Property_Injection"&gt;Explicit property injection&lt;/a&gt; on the other hand is fine. With explicit property injection you force the container to inject a property and it will fail when it can&amp;#39;t succeed. However, you should prefer constructor injection whenever possible. Note that the need for property injection is often an indication of problems in the design. If you revert to property injection because you otherwise have too many constructor arguments, you&amp;#39;re probably violating the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Register all root objects explicitly if possible. For instance, register all ASP.NET MVC Controller instances explicitly in the container (Controller instances are requested directly and are therefore called &amp;#39;root objects&amp;#39;). This way the container can check the complete dependency graph starting from the root object when you call &lt;b&gt;Verify()&lt;/b&gt;. Prefer registering all root objects in an automated fashion, for instance by using reflection to find all root types. The &lt;a href="http://nuget.org/packages/SimpleInjector.Integration.Web.Mvc"&gt;Simple Injector ASP.NET MVC Integration NuGet Package&lt;/a&gt; for instance, contains a &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/M_SimpleInjector_SimpleInjectorMvcExtensions_RegisterMvcControllers.htm"&gt;RegisterMvcControllers&lt;/a&gt; extension method that will do this for you and the &lt;a href="http://nuget.org/packages/SimpleInjector.Integration.Wcf"&gt;WCF Integration NuGet Package&lt;/a&gt; contains a &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary.v2/?topic=html/M_SimpleInjector_SimpleInjectorWcfExtensions_RegisterWcfServices.htm"&gt;RegisterWcfServices&lt;/a&gt; extension method for this purpose.&lt;/li&gt;
&lt;li&gt;If registering root objects is not possible or feasible, test the creation of each root object manually during start-up. With ASP.NET Web Form Page classes for instance, you will probably call the container from within their constructor (since Page classes must unfortunately have a default constructor). The key here again is finding them all in once using reflection. By finding all Page classes using reflection and instantiating them, you&amp;#39;ll find out (during app start-up or through automated testing) whether there is a problem with your DI configuration or not. The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Web%20Forms%20Integration&amp;referringTitle=How-to"&gt;Web Forms Integration&lt;/a&gt; guide contains an example of how to verify page classes.&lt;/li&gt;
&lt;li&gt;There are scenarios where some dependencies cannot yet be created during application start-up. To ensure that the application can be started normally and the rest of the DI configuration can still be verified, abstract those dependencies behind a proxy or abstract factory. Try to keep those unverifiable dependencies to a minimum and keep good track of them, because you will probably have to test them manually.&lt;/li&gt;
&lt;li&gt;But even when all registrations can be resolved succesfully by the container, that still doesn&amp;#39;t mean your configuration is correct. It is very easy to accidentally misconfigure the container in a way that only shows up late in the development process. &lt;b&gt;Simple Injector&lt;/b&gt; contains &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=How-to"&gt;Debugger Diagnostics&lt;/a&gt; to help you spot common configuration mistakes. Use these diagnostic services and use them often.&lt;/li&gt;&lt;/ol&gt;
&lt;h2&gt;Work with dependency injection in multi-threaded applications&lt;a name="Multi-Threaded-Applications"&gt;&lt;/a&gt;&lt;a name="Multi_Threaded_Applications"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note:&lt;/b&gt; Simple Injector is designed for use in highly-concurrent applications. Its lock-free design allows it to scale linearly with the number of threads and processors in your system.&lt;/i&gt;&lt;/blockquote&gt;
Many applications and application frameworks are inherently multi-threaded. Working in multi-threaded applications forces developers to take special care. It is easy for a less experienced developer to introduce a race condition in the code. Even although some frameworks such as ASP.NET make it easy to write thread-safe code, introducing a simple static field could break thread-safety.&lt;br /&gt;&lt;br /&gt;This same holds when working with DI containers in multi-threaded applications. The developer that configures the container, should be aware of the risks of shared state. &lt;b&gt;Not knowing which configured services are thread-safe is a sin.&lt;/b&gt; Registering a service that is not thread-safe as singleton, will eventually lead to concurrency bugs, that usually only appear in production code. Those bugs are often hard to reproduce and hard to find, making them costly to fix. And even when you correctly configured a service with the correct lifestyle, when another component that depends on it accidentally as a longer lifetime, the service might be kept alive much longer and might even be accessible from other threads.&lt;br /&gt;&lt;br /&gt;Dependency injection however, can actually help in writing multi-threaded applications. Dependency injection forces you to wire all dependencies together in a single place in the application, the &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot/"&gt;Composition Root&lt;/a&gt;. This means that there is a single place in the application that knows about how services behave, whether they are thread-safe, and how they should be wired. Without this centralization, this knowledge would be scattered throughout the code base, making it very hard to change the behavior of a service.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: Take a close look at the &amp;#39;Potential Lifestyle Mismatches&amp;#39; warnings in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=How-to"&gt;Debugger Diagnostics&lt;/a&gt;. Lifestyle mismatches are a source of concurrency bugs.&lt;/i&gt;&lt;/blockquote&gt;
In a multi-threaded application, each thread should get its own object graph. This means that you should typically call &lt;b&gt;container.GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; once at the beginning of the thread to get the root object for processing that thread (or request). The container will build an object graph with all root object&amp;#39;s dependencies. Some of those dependencies will be singletons; shared between all threads. Other dependencies might be transient; a new instance is created per dependency. Other dependencies might be thread-specific, request-specific, or with some other lifestyle. The application code itself is unaware of the way the dependencies are registered and that&amp;#39;s the way it is supposed to be.&lt;br /&gt;&lt;br /&gt;For web applications, you typically call &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; at the beginning of the web request. In an ASP.NET MVC application for instance, one Controller instance will be requested from the container (by the Controller Factory) per web request. When using one of the integration packages, such as the &lt;a href="https://nuget.org/packages/SimpleInjector.MVC3"&gt;Simple Injector MVC Integration Quick Start NuGet package&lt;/a&gt; for instance, you don&amp;#39;t have to call &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; yourself, the package will ensure this is done for you. Still, &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; is typically called once per request.&lt;br /&gt;&lt;br /&gt;The advice of building a new object graph (calling &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt;) at the beginning of a thread, also holds when manually starting a new thread. Although you can pass on data to other threads, you should not pass on container controlled dependencies to other threads. On each new thread, you should ask the container again for the dependencies. When you start passing dependencies from one thread to the other, those parts of the code have to know whether it is safe to pass those dependencies on. For instance, are those dependencies thread-safe? This might be trivial to analyze in some situations, but prevents you to change those dependencies with other implementations, since now you have to remember that there is a place in your code where this is happening and you need to know which dependencies are passed on. You are decentralizing this knowledge again, making it harder to reason about the correctness of your DI configuration and making it easier to misconfigure the container in a way that causes those concurrency problems.&lt;br /&gt;&lt;br /&gt;Running code on a new thread can be done by adding a little bit of infrastructural code. Take for instance the following example where we want to send e-mail messages asynchronously. Instead of letting the caller implement this logic, it is better to hide the logic for asynchronicity behind an abstraction; a proxy. This ensures that this logic is centralized to a single place, and by placing this proxy inside the composition root, we prevent the application code to take a dependency on the container itself (which should be prevented).&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Synchronous implementation of IMailSender&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RealMailSender : IMailSender
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IMailFormatter formatter;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RealMailSender(IMailFormatter formatter)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.formatter = formatter;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IMailSender.SendMail(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// format mail&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// send mail&lt;/span&gt;
    }
}

&lt;span style="color:Green;"&gt;// Proxy for executing IMailSender asynchronously.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AsyncMailSenderProxy : IMailSender
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;IMailSender&amp;gt; mailSenderFactory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AsyncMailSenderProxy(ILogger logger,
        Func&amp;lt;IMailSender&amp;gt; mailSenderFactory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = logger;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.mailSenderFactory = mailSenderFactory;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IMailSender.SendMail(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// Run on a new thread&lt;/span&gt;
        Task.Factory.StartNew(() =&amp;gt;
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.SendMailAsync(to, message);
        });        
    }

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SendMailAsync(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// Here we run on a different thread and the&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// services should be requested on this thread.&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; mailSender = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.mailSenderFactory();

        &lt;span style="color:Blue;"&gt;try&lt;/span&gt;
        {
            mailSender.SendMail(to, message);
        }
        &lt;span style="color:Blue;"&gt;catch&lt;/span&gt; (Exception ex)
        {
            &lt;span style="color:Green;"&gt;// logging is important, since we run on a&lt;/span&gt;
            &lt;span style="color:Green;"&gt;// different thread.&lt;/span&gt;
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger.Log(ex);
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;In the Composition Root, instead of registering the &lt;i&gt;MailSender&lt;/i&gt;, we register the &lt;i&gt;AsyncMailSenderProxy&lt;/i&gt; as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;ILogger, FileLogger&amp;gt;(Lifestyle.Singleton);
container.Register&amp;lt;IMailSender, RealMailSender&amp;gt;();
container.RegisterSingleDecorator(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IMailSender),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncMailSenderProxy));
&lt;/pre&gt;&lt;/div&gt;In this case the container will ensure that when an &lt;i&gt;IMailSender&lt;/i&gt; is requested, a single &lt;i&gt;AsyncMailSenderProxy&lt;/i&gt; is returned with a &lt;i&gt;Func&amp;lt;IMailSender&amp;gt;&lt;/i&gt; delegate that will create a new &lt;i&gt;RealMailSender&lt;/i&gt; when requested. The &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterDecorator.htm"&gt;RegisterDecorator&lt;/a&gt; and &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterSingleDecorator.htm"&gt;RegisterSingleDecorator&lt;/a&gt; overloads natively understand how to handle &lt;i&gt;Func&amp;lt;Decoratee&amp;gt;&lt;/i&gt; dependencies. The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Generic_Decorators"&gt;Decorators&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to"&gt;Advanced-scenarios&lt;/a&gt; wiki page explains more about registering decorators.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Fri, 17 May 2013 10:49:55 GMT</pubDate><guid isPermaLink="false">Updated Wiki: How-to 20130517104955A</guid></item><item><title>Updated Wiki: Home</title><link>https://simpleinjector.codeplex.com/wikipage?version=99</link><description>&lt;div class="wikidoc"&gt;&lt;h1&gt;Welcome to the Simple Injector project&lt;/h1&gt;The Simple Injector is an easy-to-use Inversion of Control library for .NET and Silverlight.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http&amp;#58;&amp;#47;&amp;#47;nuget.org&amp;#47;packages&amp;#63;q&amp;#61;simpleinjector&amp;#38;sortOrder&amp;#61;package-download-count"&gt;&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=simpleinjector&amp;DownloadId=305103" alt="Download&amp;#32;us&amp;#32;from&amp;#32;Nuget" title="Download&amp;#32;us&amp;#32;from&amp;#32;Nuget" /&gt;&lt;/a&gt;     &lt;a href="http&amp;#58;&amp;#47;&amp;#47;twitter.com&amp;#47;simpleinjector"&gt;&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=simpleinjector&amp;DownloadId=305102" alt="Follow&amp;#32;us&amp;#32;on&amp;#32;twitter" title="Follow&amp;#32;us&amp;#32;on&amp;#32;twitter" /&gt;&lt;/a&gt;
&lt;h2&gt;Overview&lt;a name="Overview"&gt;&lt;/a&gt;&lt;/h2&gt;The goal of Simple Injector is to provide .NET application developers with an easy, flexible, and fast dependency injection framework, that uses best practices to steers developers towards the pit of success.&lt;br /&gt;&lt;br /&gt;Many of the existing IoC frameworks have a big complicated legacy API or are are new, immature, and lack the features do be used in large developement projects. &lt;b&gt;Simple Injector&lt;/b&gt; solves this problem by supplying a simple implementation with a very limited set of carefully chosen features in its core library. For example, file- and attribute-based configuration methods (that result in brittle and maintenance heavy applications) have been abandoned, favoring simple code-based configuration instead. This is enough for most applications, requiring only that configuration be performed at the start of the program. Although the API is limited, the core library does contain many features and allows almost any &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Home"&gt;advanced scenario&lt;/a&gt;.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;You will need &lt;b&gt;.NET 4.0&lt;/b&gt; or up to use the &lt;b&gt;Simple Injector 2&lt;/b&gt; library. (For .NET 3.5 projects you can use the &lt;a href="https://simpleinjector.codeplex.com/releases/view/98963"&gt;Simple Injector v1.x&lt;/a&gt; releases).&lt;/li&gt;
&lt;li&gt;Simple Injector is carefully designed to run in &lt;i&gt;&lt;b&gt;partial trust&lt;/b&gt;&lt;/i&gt;.&lt;/li&gt;
&lt;li&gt;Simple Injector runs in &lt;i&gt;&lt;b&gt;Silverlight 4&lt;/b&gt;&lt;/i&gt; and up (Silverlight for Windows Phone is &lt;i&gt;&lt;b&gt;&lt;a href="https://simpleinjector.codeplex.com/discussions/285015"&gt;not&lt;/a&gt;&lt;/b&gt;&lt;/i&gt; supported).&lt;/li&gt;
&lt;li&gt;Simple Injector 2 runs on &lt;b&gt;Mono&lt;/b&gt;.&lt;/li&gt;
&lt;li&gt;It is fast; &lt;a href="http://simpleinjector.codeplex.com/discussions/326621"&gt;blazingly fast&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;

&lt;h2&gt;New Diagnostic Services in Simple Injector 2&lt;/h2&gt;Simple Injector version 2 adds the ability to warn about common misconfigurations while debugging the application. Many Simple Injector users have already used this feature to diagnose their configuration and have successfully fixed problems that would be very hard to find otherwise. Here&amp;#39;s a quick example of how this might look in the debugger:&lt;br /&gt;&lt;br /&gt;&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=simpleinjector&amp;DownloadId=635982" alt="Diagnostics&amp;#32;debugger&amp;#32;view&amp;#32;context&amp;#32;menu" title="Diagnostics&amp;#32;debugger&amp;#32;view&amp;#32;context&amp;#32;menu" /&gt;&lt;br /&gt;&lt;br /&gt;See the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Home"&gt;Debug Diagnostic Services&lt;/a&gt; page for more information.&lt;br /&gt;
&lt;h2&gt;Getting started&lt;a name="Getting_started"&gt;&lt;/a&gt;&lt;/h2&gt;If you&amp;#39;re using &lt;a href="http://www.nuget.org/"&gt;NuGet&lt;/a&gt;, there are NuGet packages available. Take a look &lt;a href="http://nuget.org/packages?q=simpleinjector&amp;amp;sortOrder=package-download-count"&gt;here&lt;/a&gt;. Otherwise, follow the steps below, to start using the &lt;b&gt;Simple Injector&lt;/b&gt;:
&lt;ol&gt;&lt;li&gt;Go to the &lt;a href="http://simpleinjector.codeplex.com/releases/"&gt;Downloads&lt;/a&gt; tab and download the latest &lt;i&gt;runtime library&lt;/i&gt;;&lt;/li&gt;
&lt;li&gt;Unpack the downloaded .zip file;&lt;/li&gt;
&lt;li&gt;Add the &lt;i&gt;SimpleInjector.dll&lt;/i&gt; to your start-up project by right-clicking on a project in the Visual Studio solution explorer and selecting &amp;#39;Add Reference...&amp;#39;.&lt;/li&gt;
&lt;li&gt;Add the &lt;i&gt;using SimpleInjector;&lt;/i&gt; using directive on the top of the code file where you wish to configure the application.&lt;/li&gt;
&lt;li&gt;Look at the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=Home"&gt;Usage&lt;/a&gt; section in the documentation on how to configure and use the &lt;b&gt;Simple Injector&lt;/b&gt;.&lt;/li&gt;
&lt;li&gt;Look at the &lt;a href="#More_information"&gt;More information&lt;/a&gt; section if you want to learn more or have any questions.&lt;/li&gt;&lt;/ol&gt;

&lt;h2&gt;A Quick Example&lt;a name="Example"&gt;&lt;/a&gt;&lt;/h2&gt;The general idea behind the &lt;b&gt;Simple Injector&lt;/b&gt; (or any DI framework for that matter) is that you design your application around loosely coupled components using the &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;dependency injection pattern&lt;/a&gt;. Take for instance the following &lt;i&gt;UserController&lt;/i&gt; class in the context of an ASP.NET MVC application:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: &lt;b&gt;Simple Injector&lt;/b&gt; works for many different technologies, not only for MVC. Please look at the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Integration%20Guide&amp;referringTitle=Home"&gt;Integration Guide&lt;/a&gt; for help using the &lt;b&gt;Simple Injector&lt;/b&gt; with your technology of choice.&lt;/i&gt;&lt;/blockquote&gt;
&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; UserController : Controller {
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IUserRepository repository;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;

    &lt;span style="color:Green;"&gt;// Use constructor injection for the dependencies&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; UserService(IUserRepository rep, ILogger logger) {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.repository = rep;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = logger;
    }

    &lt;span style="color:Green;"&gt;// implement UserController methods here:&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ActionResult Index() {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger.Log(&lt;span style="color:#A31515;"&gt;&amp;quot;Index called&amp;quot;&lt;/span&gt;);
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; View(&lt;span style="color:Blue;"&gt;this&lt;/span&gt;.rep.GetAll());
    }	
}
&lt;/pre&gt;&lt;/div&gt;The &lt;i&gt;UserController &lt;/i&gt; class depends on the &lt;i&gt;IUserRepository&lt;/i&gt; and &lt;i&gt;ILogger&lt;/i&gt; interfaces. By not depending on concrete implementations, we can test &lt;i&gt;UserController &lt;/i&gt; in isolation. Ease of testing is however, just the start of what Dependency Injection will accomplish. It allows us to design highly flexible systems that can completely be composed at a single place (often the startup path) in the application.&lt;br /&gt;&lt;br /&gt;Using the &lt;b&gt;Simple Injector&lt;/b&gt;, the configuration of the application using the &lt;i&gt;UserController&lt;/i&gt; class, could look as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;protected&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Application_Start(&lt;span style="color:Blue;"&gt;object&lt;/span&gt; sender, EventArgs e) {
    &lt;span style="color:Green;"&gt;// 1. Create a new Simple Injector container&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

    &lt;span style="color:Green;"&gt;// 2. Configure the container (register)&lt;/span&gt;
    container.Register&amp;lt;IUserRepository, SqlUserRepository&amp;gt;();

    container.RegisterSingle&amp;lt;ILogger&amp;gt;(() =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; CompositeLogger(
        container.GetInstance&amp;lt;DatabaseLogger&amp;gt;(),
        container.GetInstance&amp;lt;MailLogger&amp;gt;()
    ));

    &lt;span style="color:Green;"&gt;// 3. Optionally verify the container&amp;#39;s configuration.&lt;/span&gt;
    container.Verify();

    &lt;span style="color:Green;"&gt;// 4. Register the container as MVC3 IDependencyResolver.&lt;/span&gt;
    DependencyResolver.SetResolver(&lt;span style="color:Blue;"&gt;new&lt;/span&gt; SimpleInjectorDependencyResolver(container));
}
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: If you start with a MVC application, use the &lt;a href="http://nuget.org/packages/SimpleInjector.MVC3"&gt;NuGet Simple Injector MVC Integration Quick Start package&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;
The given configuration registers implementations for the &lt;i&gt;IUserRepository&lt;/i&gt; and &lt;i&gt;ILogger&lt;/i&gt; interfaces. The code snippet shows a few interesting things. First of all, you can map concrete instances (such as &lt;i&gt;SqlUserRepository&lt;/i&gt;) to an interface or base type. In the given example, every time you ask the container for an &lt;i&gt;IUserRepository&lt;/i&gt;, it will create a new &lt;i&gt;SqlUserRepository&lt;/i&gt; on your behalf (in DI terminology: an object with a &lt;i&gt;Transient&lt;/i&gt; lifestyle).&lt;br /&gt;&lt;br /&gt;The registration of the &lt;i&gt;ILogger&lt;/i&gt; is a bit more complex though. It registers a delegate that knows how to create a new &lt;i&gt;ILogger&lt;/i&gt; implementation, in this case &lt;i&gt;CompositeLogger&lt;/i&gt; (which is an implementation of &lt;i&gt;ILogger&lt;/i&gt;). The delegate itself calls back into the container and this allows the container to create the concrete &lt;i&gt;DatabaseLogger&lt;/i&gt; and &lt;i&gt;MailLogger&lt;/i&gt; and inject them into the &lt;i&gt;CompositeLogger&lt;/i&gt;. While the type of registration that we’ve seen with the &lt;i&gt;IUserRepository&lt;/i&gt; is much more common, the use of delegates allows many interesting scenarios.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: We did not register the &lt;/i&gt;UserController&lt;i&gt;, because the &lt;/i&gt;UserController&lt;i&gt; is a concrete type, &lt;b&gt;Simple Injector&lt;/b&gt; can implicitly create it (as long as its dependencies can be resolved).&lt;/i&gt;&lt;/blockquote&gt;
This is in fact all it takes to start using the &lt;b&gt;Simple Injector&lt;/b&gt;. Design your classes around the dependency injection principle (which is actually the hard part) and configure them in the top of your application. Some frameworks (such as ASP.NET MVC) will do the rest for you. Other frameworks (like ASP.NET Web Forms) will need a little bit more work to get this done. See the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Integration%20Guide&amp;referringTitle=Home"&gt;Integration Guide&lt;/a&gt; for examples of your framework of choice.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;Please go to the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=Home"&gt;Using the Simple Injector&lt;/a&gt; section in the documentation to see more examples.&lt;/i&gt;&lt;/blockquote&gt;&lt;h2&gt;More information&lt;a name="More_information"&gt;&lt;/a&gt;&lt;/h2&gt;For more information about the &lt;b&gt;Simple Injector&lt;/b&gt; please visit the following links: 
&lt;ul&gt;&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=Home"&gt;Using the Simple Injector&lt;/a&gt; will guide you through the &lt;b&gt;Simple Injector&lt;/b&gt; basics.&lt;/li&gt;
&lt;li&gt;The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Home"&gt;Simple Injector and object lifetime management&lt;/a&gt; page explains how to configure lifestyles such as &lt;i&gt;transient&lt;/i&gt;, &lt;i&gt;singleton&lt;/i&gt;, and many others.&lt;/li&gt;
&lt;li&gt;See the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/"&gt;Reference library&lt;/a&gt; for the complete API documentation.&lt;/li&gt;
&lt;li&gt;See the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Integration%20Guide&amp;referringTitle=Home"&gt;Integration Guide&lt;/a&gt; for more information about how to integrate the &lt;b&gt;Simple Injector&lt;/b&gt; into your specific application framework.&lt;/li&gt;
&lt;li&gt;For more information about &lt;i&gt;dependency injection&lt;/i&gt; in general, please visit &lt;a href="http://stackoverflow.com/tags/dependency-injection/info"&gt;this page on Stackoverflow&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;If you have any questions about how to use the &lt;b&gt;Simple Injector&lt;/b&gt; or about &lt;i&gt;dependency injection&lt;/i&gt; in general, the experts at &lt;a href="http://stackoverflow.com/questions/ask?tags=simple-injector ioc-container dependency-injection .net c%23&amp;amp;title=Simple%20Injector:"&gt;Stackoverflow.com&lt;/a&gt; are waiting for you.&lt;/li&gt;
&lt;li&gt;For all other &lt;b&gt;Simple Injector&lt;/b&gt; related question and discussions, such as bug reports and feature requests, the &lt;a href="http://simpleinjector.codeplex.com/discussions"&gt;Simple Injector discussion forum&lt;/a&gt; will be the place to start.&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Happy injecting!&lt;/h3&gt;
&lt;h2&gt;This project is supported by&lt;/h2&gt;&lt;a href="http&amp;#58;&amp;#47;&amp;#47;www.ncover.com"&gt;&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=simpleinjector&amp;DownloadId=99602" alt="NCover" title="NCover" /&gt;&lt;/a&gt; &lt;a href="http&amp;#58;&amp;#47;&amp;#47;www.ndepend.com"&gt;&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=simpleinjector&amp;DownloadId=604581" alt="NDepend" title="NDepend" /&gt;&lt;/a&gt;
&lt;h2&gt;Simple Injector Twitter Feed&lt;a name="Twitter_Feed"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;sub&gt;&lt;div class="rss"&gt;&lt;div class="accentbar"&gt;&lt;span class="left"&gt;&amp;nbsp;&lt;/span&gt;Twitter / simpleinjector News Feed&lt;span class="right"&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class="entry"&gt;&lt;div class="title"&gt;&lt;a href="http://twitter.com/simpleinjector/statuses/250305928434442241"&gt;simpleinjector: Hi @rossipedia. Please post a question on the http://t.co/9Q0ED2Yk discussion forum or stackoverflow http://t.co/L1eTCQoL with more info.&lt;/a&gt;&lt;/div&gt;&lt;div class="moreinfo"&gt;&lt;span class="date"&gt;Monday, September 24, 2012&lt;/span&gt; &amp;nbsp;|&amp;nbsp; &lt;span class="source"&gt;From &lt;a target="_blank" href="https://twitter.com/statuses/user_timeline/simpleinjector.rss"&gt;Twitter / simpleinjector&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="entry"&gt;&lt;div class="title"&gt;&lt;a href="http://twitter.com/simpleinjector/statuses/249961931920793600"&gt;simpleinjector: RT @NuGetLatest: SimpleInjector.Integration.Wcf 1.5.0.12267 http://t.co/N4vkPAbs&lt;/a&gt;&lt;/div&gt;&lt;div class="moreinfo"&gt;&lt;span class="date"&gt;Sunday, September 23, 2012&lt;/span&gt; &amp;nbsp;|&amp;nbsp; &lt;span class="source"&gt;From &lt;a target="_blank" href="https://twitter.com/statuses/user_timeline/simpleinjector.rss"&gt;Twitter / simpleinjector&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="entry"&gt;&lt;div class="title"&gt;&lt;a href="http://twitter.com/simpleinjector/statuses/249942026206998529"&gt;simpleinjector: #simpleinjector #WCF Integration Guide: http://t.co/5qSVQu1Z and #NuGet package: https://t.co/ceDMj7YI&lt;/a&gt;&lt;/div&gt;&lt;div class="moreinfo"&gt;&lt;span class="date"&gt;Sunday, September 23, 2012&lt;/span&gt; &amp;nbsp;|&amp;nbsp; &lt;span class="source"&gt;From &lt;a target="_blank" href="https://twitter.com/statuses/user_timeline/simpleinjector.rss"&gt;Twitter / simpleinjector&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="entry"&gt;&lt;div class="title"&gt;&lt;a href="http://twitter.com/simpleinjector/statuses/249574764052611072"&gt;simpleinjector: #simpleinjector Integration Guide for http://t.co/s4u4dx9N Web API http://t.co/tGxmP95H&lt;/a&gt;&lt;/div&gt;&lt;div class="moreinfo"&gt;&lt;span class="date"&gt;Saturday, September 22, 2012&lt;/span&gt; &amp;nbsp;|&amp;nbsp; &lt;span class="source"&gt;From &lt;a target="_blank" href="https://twitter.com/statuses/user_timeline/simpleinjector.rss"&gt;Twitter / simpleinjector&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="entry"&gt;&lt;div class="title"&gt;&lt;a href="http://twitter.com/simpleinjector/statuses/239480728390340608"&gt;simpleinjector: Just published a new patch release of  #SimpleInjector Extensions to #NuGet http://t.co/VTw9P23A Fixes a few bugs with RegisterOpenGeneric&lt;/a&gt;&lt;/div&gt;&lt;div class="moreinfo"&gt;&lt;span class="date"&gt;Saturday, August 25, 2012&lt;/span&gt; &amp;nbsp;|&amp;nbsp; &lt;span class="source"&gt;From &lt;a target="_blank" href="https://twitter.com/statuses/user_timeline/simpleinjector.rss"&gt;Twitter / simpleinjector&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="accentbar"&gt;&lt;span class="left"&gt;&amp;nbsp;&lt;/span&gt;Twitter / simpleinjector News Feed&lt;span class="right"&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/sub&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Sun, 12 May 2013 10:53:30 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20130512105330A</guid></item><item><title>Updated Wiki: How-to</title><link>https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;version=31</link><description>&lt;div class="wikidoc"&gt;&lt;h1&gt;How To...&lt;/h1&gt;
&lt;ul&gt;&lt;li&gt;How to &lt;a href="#Register-Factory-Delegates"&gt;Register factory delegates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Resolve-Instances-By-Key"&gt;Resolve instances by key&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Resolve-Arrays-And-Lists"&gt;Resolve array and lists&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Register-Multiple-Interfaces-With-The-Same-Implementation"&gt;Register multiple interfaces with the same implementation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Override-Existing-Registrations"&gt;Override existing registrations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Verifying-Configuration"&gt;Verify the container&amp;#39;s configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Multi-Threaded-Applications"&gt;Work with dependency injection in multi-threaded applications&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;h2&gt;Register factory delegates&lt;a name="Register-Factory-Delegates"&gt;&lt;/a&gt;&lt;a name="Register_Factory_Delegates"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;b&gt;Simple Injector&lt;/b&gt; allows you to register a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate for the creation of an instance. This is especially useful in scenarios where it is impossible for the container to create the instance. There are overloads of the &lt;i&gt;Register&lt;/i&gt; method available that accept an &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; argument:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IMyService&amp;gt;(() =&amp;gt; SomeSubSystem.CreateMyService());
&lt;/pre&gt;&lt;/div&gt;In situations where a service needs to create multiple instances of a certain dependencies, or needs to explicitly control the lifetime of such dependency, abstract factories can be used. Instead of injecting an &lt;i&gt;IMyService&lt;/i&gt;, you should inject an &lt;i&gt;IMyServiceFactory&lt;/i&gt; that allows services to create new instances of &lt;i&gt;IMyService&lt;/i&gt;:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IMyServiceFactory
{
    IMyService CreateNew();
}

&lt;span style="color:Green;"&gt;// Implementation&lt;/span&gt;
&lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ServiceFactory : IMyServiceFactory
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IMyService CreateNew()
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; MyServiceImpl();
    }
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterSingle&amp;lt;IMyServiceFactory, ServiceFactory&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IMyServiceFactory factory;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(IMyServiceFactory factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service1 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.CreateNew())
    	{
            &lt;span style="color:Green;"&gt;// use service 1&lt;/span&gt;
        }

        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service2 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.CreateNew())
    	{
            &lt;span style="color:Green;"&gt;// use service 2&lt;/span&gt;
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;Instead of creating specific interfaces for your factories, you can also choose to inject &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates into your services:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterSingle&amp;lt;Func&amp;lt;IMyService&amp;gt;&amp;gt;(
    () =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; MyServiceImpl());

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;IMyService&amp;gt; factory;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(Func&amp;lt;IMyService&amp;gt; factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service1 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.Invoke())
    	{
            &lt;span style="color:Green;"&gt;// use service 1&lt;/span&gt;
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;This saves you from having to define a new interface and implementation per factory.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: On the downside however, this communicates less clearly the intend of your code and as a result might make your code harder to grasp for other developers.&lt;/i&gt;&lt;/blockquote&gt;
When you choose &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates over specific factory interfaces, you can define the following extension method that makes it easier to register &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; factories:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Extension method&lt;/span&gt;
container.RegisterFuncFactory&amp;lt;TService, TImplementation&amp;gt;(
    &lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container,
    Lifestyle lifestyle = &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;
{
    lifestyle = lifestyle ?? Lifestyle.Transient;

    &lt;span style="color:Green;"&gt;// Register the type explicitly to keep the configuration&lt;/span&gt;
    &lt;span style="color:Green;"&gt;// verifiable.&lt;/span&gt;
    container.Register&amp;lt;TService, TImplementation&amp;gt;(lifestyle);
	
    &lt;span style="color:Green;"&gt;// Register the Func&amp;lt;T&amp;gt; that resolves that instance.&lt;/span&gt;
    container.RegisterSingle&amp;lt;Func&amp;lt;TService&amp;gt;&amp;gt;(
        () =&amp;gt; container.GetInstance&amp;lt;TService&amp;gt;());
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterFuncFactory&amp;lt;IMyService, RealService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;The previous extension method allowed registration of a single factory, but won&amp;#39;t be maintainable when you want all registrations to be resolvable using &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates by default. &lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: We personally think that allowing to register Func&amp;lt;T&amp;gt; delegates by default is a design smell. The use of Func&amp;lt;T&amp;gt; delegates makes your design harder to follow and your system harder to maintain and test. If you have many constructors in your system that depend on a Func&amp;lt;T&amp;gt;, please take a good look at your dependency strategy. If in doubt, please ask us here on CodePlex or on Stackoverflow.&lt;/i&gt;&lt;/blockquote&gt;
The following extension method allows Simple Injector to resolve all types using a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate by default:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; AllowResolvingFuncFactories(
    &lt;span style="color:Blue;"&gt;this&lt;/span&gt; ContainerOptions options)
{
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = options.Container;

    container.ResolveUnregisteredType += (sender, e) =&amp;gt;
    {
        &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (e.UnregisteredServiceType.IsGenericType &amp;amp;&amp;amp;
            e.UnregisteredServiceType.GetGenericTypeDefinition() ==
                &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Func&amp;lt;&amp;gt;))
        {
            Type serviceType = e.UnregisteredServiceType
                .GetGenericArguments().First();

            InstanceProducer registration =
                container.GetRegistration(serviceType);

            &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (registration != &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
            {
                Type funcType =
                    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Func&amp;lt;&amp;gt;).MakeGenericType(serviceType);

                &lt;span style="color:Blue;"&gt;var&lt;/span&gt; factoryDelegate = 
                    Expression.Lambda(funcType,
                        registration.BuildExpression())
                    .Compile();

                e.Register(Expression.Constant(factoryDelegate));
            }
        }
    };
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.Options.AllowResolvingFuncFactories();
&lt;/pre&gt;&lt;/div&gt;After calling the &lt;b&gt;AllowResolvingFuncFactories&lt;/b&gt; extension method, the container allows resolving &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates.&lt;br /&gt;&lt;br /&gt;&lt;a name="lazy"&gt;&lt;/a&gt;Just like &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates can be injected, &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; instances can be injected into services. &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; is useful in situations where the creation of a service is time consuming, but is not always used. Thus &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; allows you to postpone the creation of that service until the moment that it is really needed:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Extension method&lt;/span&gt;
container.RegisterLazy&amp;lt;T&amp;gt;(&lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;
{
    Func&amp;lt;T&amp;gt; factory = () =&amp;gt; container.GetInstance&amp;lt;T&amp;gt;();

    container.Register&amp;lt;Lazy&amp;lt;T&amp;gt;&amp;gt;(() =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Lazy&amp;lt;T&amp;gt;(factory));
}

&lt;span style="color:Green;"&gt;// Registration    &lt;/span&gt;
container.RegisterLazy&amp;lt;IMyService&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Lazy&amp;lt;IMyService&amp;gt; myService;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(Lazy&amp;lt;IMyService&amp;gt; myService)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.myService = myService;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (someCondition)
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.myService.Value.Operate();
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;Note that instead of polluting the API of your application with &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; dependencies, it is usually cleaner to hide the &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; behind a proxy:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Proxy definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyLazyServiceProxy : IMyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Lazy&amp;lt;IMyService&amp;gt; wrapped;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyLazyServiceProxy(Lazy&amp;lt;IMyService&amp;gt; wrapped)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.wrapped = wrapped;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Operate()
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.wrapped.Value.Operate();
    }
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterLazy&amp;lt;IMyService&amp;gt;();
container.Register&amp;lt;IMyService, MyLazyServiceProxy&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;This way the application can simply depend on &lt;i&gt;IMyService&lt;/i&gt; instead of &lt;i&gt;Lazy&amp;lt;IMyService&amp;gt;&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Warning&lt;/b&gt;: The same warning applies to the use of Lazy&amp;lt;T&amp;gt; as it does for the use of Func&amp;lt;T&amp;gt; delegates. For more information about creating an application and container configuration that can be successfully verified, please read the &lt;a href="#Verifying-Configuration"&gt;How To Verify the container&amp;#8217;s configuration&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;&lt;h2&gt;Resolve instances by key&lt;a name="Resolve-Instances-By-Key"&gt;&lt;/a&gt;&lt;a name="Resolve_Instances_By_Key"&gt;&lt;/a&gt;&lt;/h2&gt;Resolving instances by a key is a feature that is deliberately left out of the &lt;b&gt;Simple Injector&lt;/b&gt;, because we feel it steers developers to a design were the application tends to have a dependency on the DI container itself. For resolving a keyed instance, you will often need to call into the container itself, which leads to the &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator anti-pattern&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;This doesn’t mean however, that resolving instances by a key is never useful. Resolving instances by a key however, is much more a job for a specific factory than for a container itself. This makes the design much cleaner, saves you from having to take a dependency on the DI framework, and enables many scenarios that the DI container writer just didn’t consider.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The need for keyed registration can be an indication of ambiguity in the application design. Take a good look if each keyed registration shouldn&amp;#39;t have its own unique interface, or perhaps each registration should implement each own version of a generic interface.&lt;/i&gt;&lt;/blockquote&gt;
Take a look at the following scenario, where we want to retrieve instances of type &lt;i&gt;IRequestHandler&lt;/i&gt; by a string key. There are of course several ways to achieve this, but here is a simple but effective way, by defining an &lt;i&gt;IRequestHandlerFactory&lt;/i&gt;:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IRequestHandlerFactory
{
    IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name);
}

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; factory =
    container.GetInstance&amp;lt;IRequestHandlerFactory&amp;gt;();
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = factory.CreateNew(&lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;);
handler.Handle(requestContext);
&lt;/pre&gt;&lt;/div&gt;By inheriting from the BCL’s &lt;i&gt;Dictionary&amp;lt;TKey, TValue&amp;gt;&lt;/i&gt;, creating an &lt;i&gt;IRequestHandlerFactory&lt;/i&gt; implementation, is almost a one-liner:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory
    : Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, Func&amp;lt;IRequestHandler&amp;gt;&amp;gt;,
    IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;[name]();
    }
}
&lt;/pre&gt;&lt;/div&gt;With this class, we can register &lt;i&gt;Func&amp;lt;IRequestHandler&amp;gt;&lt;/i&gt; factory methods by a key. With this in place the registration of keyed instances is a breeze:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();
 
container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory
{
    { &lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;DefaultRequestHandler&amp;gt;() },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;OrdersRequestHandler&amp;gt;() },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;CustomersRequestHandler&amp;gt;() },
});
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: this design will work with almost all DI containers, making this not only a design that is easy to follow, but making it very easy to migrate to another DI implementation.&lt;/i&gt;&lt;/blockquote&gt;
If you don’t like a design that uses &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates this way, it can easily be changed to be a &lt;b&gt;Dictionary&amp;lt;string, Type&amp;gt;&lt;/b&gt; instead. The &lt;i&gt;RequestHandlerFactory&lt;/i&gt; can be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory
    : Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, Type&amp;gt;, IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Container container;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; RequestHandlerFactory(Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container.GetInstance(&lt;span style="color:Blue;"&gt;this&lt;/span&gt;[name]);
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; (IRequestHandler)handler;
    }
}
&lt;/pre&gt;&lt;/div&gt;The registration will then look as follows:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory(container)
{
    { &lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DefaultRequestHandler) },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(OrdersRequestHandler) },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CustomersRequestHandler) },
});
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Please remember the previous note about ambiguity in the application design. In the given example the design would probably be better af by using a generic &lt;b&gt;IRequestHandler&amp;lt;TRequest&amp;gt;&lt;/b&gt; interface. This would allow the implementations to be &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Batch_Registration"&gt;batch registered using a single line of code&lt;/a&gt;, saves you from using keys, and results in a configuration the is &lt;a href="#Verifying-Configuration"&gt;verifiable by the container&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;&lt;h2&gt;Resolve array and lists&lt;a name="Resolve-Arrays-And-Lists"&gt;&lt;/a&gt;&lt;a name="Resolve_Arrays_And_Lists"&gt;&lt;/a&gt;&lt;/h2&gt;The &lt;b&gt;Simple Injector&lt;/b&gt; allows registration of collections of elements using the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Container_RegisterAll.htm"&gt;RegisterAll&lt;/a&gt; method overloads. Collections can be resolved by using one of the &lt;b&gt;GetAllInstances&amp;lt;T&amp;gt;()&lt;/b&gt; method, by calling &lt;b&gt;GetInstance&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt;()&lt;/b&gt;, or by defining an &lt;i&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/i&gt; parameter in the constructor of a type that is created using automatic constructor injection.&lt;br /&gt;&lt;br /&gt;Injection of other collection types, such as &lt;i&gt;arrays of T&lt;/i&gt; or &lt;i&gt;IList&amp;lt;T&amp;gt;&lt;/i&gt; into constructors is not supported out of the box. By hooking onto the unregistered type resolution event however, this functionality can be added. Look &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=CollectionRegistrationExtensions&amp;referringTitle=How-to"&gt;here&lt;/a&gt; for an example extension method that allows this behavior for &lt;i&gt;T[]&lt;/i&gt; types.&lt;br /&gt;&lt;br /&gt;Please take a look at your design if you think you need to work with a collection of items. Often you can succeed by creating a composite type that can be injected. Take the following interface for instance:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; ILogger
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message);
}
&lt;/pre&gt;&lt;/div&gt;Instead of injecting a collection of dependencies, the consumer might not really be interested in the collection, but simply wishes to operate on all elements. In that scenario you can configure your container to inject a composite of that particular type. That composite might look as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CompositeLogger : ILogger
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger[] loggers;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; CompositeLogger(&lt;span style="color:Blue;"&gt;params&lt;/span&gt; ILogger[] loggers)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers = loggers ?? &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ILogger[0];
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; logger &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers)
        {
            logger.Log(message);
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;A composite allows you to remove this boilerplate iteration logic from the application, which makes the application cleaner and when changes have to be made to the way the collection of loggers is processed, only the composite has to be changed.
&lt;h2&gt;Register multiple interfaces with the same implementation&lt;a name="Register-Multiple-Interfaces-With-The-Same-Implementation"&gt;&lt;/a&gt;&lt;a name="Register_Multiple_Interfaces_With_The_Same_Implementation"&gt;&lt;/a&gt;&lt;/h2&gt;
To adhere to the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Interface%20Segregation%20Principle&amp;referringTitle=How-to"&gt;Interface Segregation Principle&lt;/a&gt;, it is important to keep interfaces narrow. Although in most situations implementations implement a single interface, it can sometimes be beneficial to have multiple interfaces on a single implementation. Here is an example of how to register this:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Impl implements IInterface1, IInterface2 and IInterface3.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; registration =
    Lifestyle.Singleton.CreateRegistration&amp;lt;Impl, Impl&amp;gt;(container);

container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface1), registration);
container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface2), registration);
container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface3), registration);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; a = container.GetInstance&amp;lt;IInterface1&amp;gt;();
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; b = container.GetInstance&amp;lt;IInterface2&amp;gt;();

&lt;span style="color:Green;"&gt;// Since Impl is a singleton, both requests return the same instance.&lt;/span&gt;
Assert.AreEqual(a, b);
&lt;/pre&gt;&lt;/div&gt;The first line creates a &lt;i&gt;Registration&lt;/i&gt; instance for the &lt;i&gt;Impl&lt;/i&gt;, in this case with a singleton lifestyle. The other lines add this registration to the container, once for each interface. This maps multiple service types to the exact same registration.
&lt;h2&gt;Override existing registrations&lt;a name="Override-Existing-Registrations"&gt;&lt;/a&gt;&lt;a name="Override_Existing_Registrations"&gt;&lt;/a&gt;&lt;/h2&gt;The default behavior of the &lt;b&gt;Simple Injector&lt;/b&gt; is to fail when a service is registered for a second time. Most of the time the developer didn&amp;#39;t intend to override a previous registration and allowing this would lead to a configuration that would pass the container&amp;#39;s verification, but doesn&amp;#39;t behave as expected.&lt;br /&gt;&lt;br /&gt;There are certain scenarios however where overriding is useful. An example of such is a bootstrapper project for a Business layer that is reused in multiple applications (in both a web application, web service, and Windows service for instance). Not having a business layer specific bootstrapper project would mean the complete DI configuration would be duplicated in the startup path of each application, which would lead to code duplication. In that situation the applications would roughly have the same configuration, with a few adjustments.&lt;br /&gt;&lt;br /&gt;Best is to start of by configuring all possible dependencies in the BL bootstrapper and leave out the service registrations where the implementation differs for each application. In other words, the BL bootstrapper would result in an incomplete configuration. After that, each application can finish the configuration by registering the missing dependencies. This way you still don&amp;#39;t need to override the existing configuration.&lt;br /&gt;&lt;br /&gt;In certain scenarios it can be beneficial to allow an application override an existing configuration. The container can be configured to allow overriding as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.Options.AllowOverridingRegistrations = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;

&lt;span style="color:Green;"&gt;// Register IUserService.&lt;/span&gt;
container.Register&amp;lt;IUserService, FakeUserService&amp;gt;();

&lt;span style="color:Green;"&gt;// Replaces the previous registration&lt;/span&gt;
container.Register&amp;lt;IUserService, RealUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;The previous example created a &lt;i&gt;Container&lt;/i&gt; instance that allows overriding. It is also possible to enable overriding half way the registration process:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Create a container with overriding disabled&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

&lt;span style="color:Green;"&gt;// Pass container to the business layer.&lt;/span&gt;
BusinessLayer.Bootstrapper.Bootstrap(container);

&lt;span style="color:Green;"&gt;// Enable overriding&lt;/span&gt;
container.Options.AllowOverridingRegistrations = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;

&lt;span style="color:Green;"&gt;// Replaces the previous registration&lt;/span&gt;
container.Register&amp;lt;IUserService, RealUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;h2&gt;Verify the container’s configuration&lt;a name="Verifying-Configuration"&gt;&lt;/a&gt;&lt;a name="Verifying_Configuration"&gt;&lt;/a&gt;&lt;/h2&gt;Dependency Injection promotes the concept of programming against abstractions. This makes your code much easier to test, easier to change and more maintainable. However, since the code itself isn&amp;#39;t responsible of maintaining the dependencies between implementations, the compiler will not be able to verify whether the dependency graph is correct.&lt;br /&gt;&lt;br /&gt;When starting to use a Dependency Injection container, many developers see their application fail when it is deployed in staging or sometimes even production, because of container misconfigurations. This makes developers often conclude that dependency injection is bad, since the dependency graph cannot be verified. This conclusion however, is incorrect. Although it is impossible for the compiler to verify the dependency graph, verifying the dependency graph is still possible and advisable.&lt;br /&gt;&lt;br /&gt;Simple Injector contains a &lt;b&gt;Verify()&lt;/b&gt; method, that will simply iterate over all registrations and resolve an instance for each registration. Calling this method directly after configuring the container, allows the application to fail during start-up, when the configuration is invalid.&lt;br /&gt;&lt;br /&gt;Calling the &lt;b&gt;Verify()&lt;/b&gt; method however, is just part of the story. It is very easy to create a configuration that passes any verification, but still fails at runtime. Here are some tips to help building a verifiable configuration:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Stay away from &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Implicit_Property_Injection"&gt;implicit property injection&lt;/a&gt;, where the container is allowed to skip injecting the property if a corresponding or correctly registered dependency can&amp;#39;t be found. This will disallow your application to fail fast and will result in &lt;b&gt;NullReferenceException&lt;/b&gt;s later on. Only use implicit property injection when the property is truly optional, omitting the dependency still keeps the configuration valid, and the application still runs correctly without that dependency. Truly optional dependencies should be very rare though, since most of the time you should prefer injecting empty implementations (a.k.a. the &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object pattern&lt;/a&gt;) instead of allowing dependencies to be a null reference. &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=How-to&amp;ANCHOR#Configuring_Property_Injection"&gt;Explicit property injection&lt;/a&gt; on the other hand is fine. With explicit property injection you force the container to inject a property and it will fail when it can&amp;#39;t succeed. However, you should prefer constructor injection whenever possible. Note that the need for property injection is often an indication of problems in the design. If you revert to property injection because you otherwise have too many constructor arguments, you&amp;#39;re probably violating the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Register all root objects explicitly if possible. For instance, register all ASP.NET MVC Controller instances explicitly in the container (Controller instances are requested directly and are therefore called &amp;#39;root objects&amp;#39;). This way the container can check the complete dependency graph starting from the root object when you call &lt;b&gt;Verify()&lt;/b&gt;. Prefer registering all root objects in an automated fashion, for instance by using reflection to find all root types. The &lt;a href="http://nuget.org/packages/SimpleInjector.Integration.Web.Mvc"&gt;Simple Injector ASP.NET MVC Integration NuGet Package&lt;/a&gt; for instance, contains a &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/M_SimpleInjector_SimpleInjectorMvcExtensions_RegisterMvcControllers.htm"&gt;RegisterMvcControllers&lt;/a&gt; extension method that will do this for you and the &lt;a href="http://nuget.org/packages/SimpleInjector.Integration.Wcf"&gt;WCF Integration NuGet Package&lt;/a&gt; contains a &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary.v2/?topic=html/M_SimpleInjector_SimpleInjectorWcfExtensions_RegisterWcfServices.htm"&gt;RegisterWcfServices&lt;/a&gt; extension method for this purpose.&lt;/li&gt;
&lt;li&gt;If registering root objects is not possible or feasible, test the creation of each root object manually during start-up. With ASP.NET Web Form Page classes for instance, you will probably call the container from within their constructor (since Page classes must unfortunately have a default constructor). The key here again is finding them all in once using reflection. By finding all Page classes using reflection and instantiating them, you&amp;#39;ll find out (during app start-up or through automated testing) whether there is a problem with your DI configuration or not. The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Web%20Forms%20Integration&amp;referringTitle=How-to"&gt;Web Forms Integration&lt;/a&gt; guide contains an example of how to verify page classes.&lt;/li&gt;
&lt;li&gt;There are scenarios where some dependencies cannot yet be created during application start-up. To ensure that the application can be started normally and the rest of the DI configuration can still be verified, abstract those dependencies behind a proxy or abstract factory. Try to keep those unverifiable dependencies to a minimum and keep good track of them, because you will probably have to test them manually.&lt;/li&gt;
&lt;li&gt;But even when all registrations can be resolved succesfully by the container, that still doesn&amp;#39;t mean your configuration is correct. It is very easy to accidentally misconfigure the container in a way that only shows up late in the development process. &lt;b&gt;Simple Injector&lt;/b&gt; contains &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=How-to"&gt;Debugger Diagnostics&lt;/a&gt; to help you spot common configuration mistakes. Use these diagnostic services and use them often.&lt;/li&gt;&lt;/ol&gt;
&lt;h2&gt;Work with dependency injection in multi-threaded applications&lt;a name="Multi-Threaded-Applications"&gt;&lt;/a&gt;&lt;a name="Multi_Threaded_Applications"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note:&lt;/b&gt; Simple Injector is designed for use in highly-concurrent applications. Its lock-free design allows it to scale linearly with the number of threads and processors in your system.&lt;/i&gt;&lt;/blockquote&gt;
Many applications and application frameworks are inherently multi-threaded. Working in multi-threaded applications forces developers to take special care. It is easy for a less experienced developer to introduce a race condition in the code. Even although some frameworks such as ASP.NET make it easy to write thread-safe code, introducing a simple static field could break thread-safety.&lt;br /&gt;&lt;br /&gt;This same holds when working with DI containers in multi-threaded applications. The developer that configures the container, should be aware of the risks of shared state. &lt;b&gt;Not knowing which configured services are thread-safe is a sin.&lt;/b&gt; Registering a service that is not thread-safe as singleton, will eventually lead to concurrency bugs, that usually only appear in production code. Those bugs are often hard to reproduce and hard to find, making them costly to fix. And even when you correctly configured a service with the correct lifestyle, when another component that depends on it accidentally as a longer lifetime, the service might be kept alive much longer and might even be accessible from other threads.&lt;br /&gt;&lt;br /&gt;Dependency injection however, can actually help in writing multi-threaded applications. Dependency injection forces you to wire all dependencies together in a single place in the application, the &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot/"&gt;Composition Root&lt;/a&gt;. This means that there is a single place in the application that knows about how services behave, whether they are thread-safe, and how they should be wired. Without this centralization, this knowledge would be scattered throughout the code base, making it very hard to change the behavior of a service.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: Take a close look at the &amp;#39;Potential Lifestyle Mismatches&amp;#39; warnings in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=How-to"&gt;Debugger Diagnostics&lt;/a&gt;. Lifestyle mismatches are a source of concurrency bugs.&lt;/i&gt;&lt;/blockquote&gt;
In a multi-threaded application, each thread should get its own object graph. This means that you should typically call &lt;b&gt;container.GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; once at the beginning of the thread to get the root object for processing that thread (or request). The container will build an object graph with all root object&amp;#39;s dependencies. Some of those dependencies will be singletons; shared between all threads. Other dependencies might be transient; a new instance is created per dependency. Other dependencies might be thread-specific, request-specific, or with some other lifestyle. The application code itself is unaware of the way the dependencies are registered and that&amp;#39;s the way it is supposed to be.&lt;br /&gt;&lt;br /&gt;For web applications, you typically call &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; at the beginning of the web request. In an ASP.NET MVC application for instance, one Controller instance will be requested from the container (by the Controller Factory) per web request. When using one of the integration packages, such as the &lt;a href="https://nuget.org/packages/SimpleInjector.MVC3"&gt;Simple Injector MVC Integration Quick Start NuGet package&lt;/a&gt; for instance, you don&amp;#39;t have to call &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; yourself, the package will ensure this is done for you. Still, &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; is typically called once per request.&lt;br /&gt;&lt;br /&gt;The advice of building a new object graph (calling &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt;) at the beginning of a thread, also holds when manually starting a new thread. Although you can pass on data to other threads, you should not pass on container controlled dependencies to other threads. On each new thread, you should ask the container again for the dependencies. When you start passing dependencies from one thread to the other, those parts of the code have to know whether it is safe to pass those dependencies on. For instance, are those dependencies thread-safe? This might be trivial to analyze in some situations, but prevents you to change those dependencies with other implementations, since now you have to remember that there is a place in your code where this is happening and you need to know which dependencies are passed on. You are decentralizing this knowledge again, making it harder to reason about the correctness of your DI configuration and making it easier to misconfigure the container in a way that causes those concurrency problems.&lt;br /&gt;&lt;br /&gt;Running code on a new thread can be done by adding a little bit of infrastructural code. Take for instance the following example where we want to send e-mail messages asynchronously. Instead of letting the caller implement this logic, it is better to hide the logic for asynchronicity behind an abstraction; a proxy. This ensures that this logic is centralized to a single place, and by placing this proxy inside the composition root, we prevent the application code to take a dependency on the container itself (which should be prevented).&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Synchronous implementation of IMailSender&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RealMailSender : IMailSender
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IMailFormatter formatter;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RealMailSender(IMailFormatter formatter)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.formatter = formatter;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IMailSender.SendMail(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// format mail&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// send mail&lt;/span&gt;
    }
}

&lt;span style="color:Green;"&gt;// Proxy for executing IMailSender asynchronously.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AsyncMailSenderProxy : IMailSender
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;IMailSender&amp;gt; mailSenderFactory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AsyncMailSenderProxy(ILogger logger,
        Func&amp;lt;IMailSender&amp;gt; mailSenderFactory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = logger;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.mailSenderFactory = mailSenderFactory;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IMailSender.SendMail(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// Run on a new thread&lt;/span&gt;
        Task.Factory.StartNew(() =&amp;gt;
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.SendMailAsync(to, message);
        });        
    }

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SendMailAsync(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// Here we run on a different thread and the&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// services should be requested on this thread.&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; mailSender = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.mailSenderFactory();

        &lt;span style="color:Blue;"&gt;try&lt;/span&gt;
        {
            mailSender.SendMail(to, message);
        }
        &lt;span style="color:Blue;"&gt;catch&lt;/span&gt; (Exception ex)
        {
            &lt;span style="color:Green;"&gt;// logging is important, since we run on a&lt;/span&gt;
            &lt;span style="color:Green;"&gt;// different thread.&lt;/span&gt;
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger.Log(ex);
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;In the Composition Root, instead of registering the &lt;i&gt;MailSender&lt;/i&gt;, we register the &lt;i&gt;AsyncMailSenderProxy&lt;/i&gt; as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;ILogger, FileLogger&amp;gt;(Lifestyle.Singleton);
container.Register&amp;lt;IMailSender, RealMailSender&amp;gt;();
container.RegisterSingleDecorator(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IMailSender),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncMailSenderProxy));
&lt;/pre&gt;&lt;/div&gt;In this case the container will ensure that when an &lt;i&gt;IMailSender&lt;/i&gt; is requested, a single &lt;i&gt;AsyncMailSenderProxy&lt;/i&gt; is returned with a &lt;i&gt;Func&amp;lt;IMailSender&amp;gt;&lt;/i&gt; delegate that will create a new &lt;i&gt;RealMailSender&lt;/i&gt; when requested. The &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterDecorator.htm"&gt;RegisterDecorator&lt;/a&gt; and &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterSingleDecorator.htm"&gt;RegisterSingleDecorator&lt;/a&gt; overloads natively understand how to handle &lt;i&gt;Func&amp;lt;Decoratee&amp;gt;&lt;/i&gt; dependencies. The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Generic_Decorators"&gt;Decorators&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to"&gt;Advanced-scenarios&lt;/a&gt; wiki page explains more about registering decorators.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Fri, 10 May 2013 16:23:27 GMT</pubDate><guid isPermaLink="false">Updated Wiki: How-to 20130510042327P</guid></item><item><title>Updated Wiki: PotentialLifestyleMismatches</title><link>https://simpleinjector.codeplex.com/wikipage?title=PotentialLifestyleMismatches&amp;version=6</link><description>&lt;div class="wikidoc"&gt;&lt;h1&gt;Diagnostic Warning - Potential Lifestyle Mismatches&lt;/h1&gt;
&lt;h2&gt;Cause&lt;a name="Cause"&gt;&lt;/a&gt;&lt;/h2&gt;The component depends on a service with a lifestyle that is shorter than that of the component.&lt;br /&gt;
&lt;h2&gt;Warning Description&lt;a name="WarningDescription"&gt;&lt;/a&gt;&lt;/h2&gt;In general, components should only depend on other components that are configured to live at least as long. In other words, it is safe for a transient component to depend on a singleton, but not the other way around. Since components store a reference to their dependencies in (private) instance fields, those dependencies are kept alive for the lifetime of that component. This means that dependencies that are configured with a shorter lifetime than their consumer, accidentally live longer than intended. This can lead to all sorts of bugs, such as hard to debug multi-threading issues.&lt;br /&gt;&lt;br /&gt;The &lt;b&gt;Diagnostic Services&lt;/b&gt; detect this kind of misconfiguration and report it. The container will be able to compare all built-in lifestyles (and sometimes even custom lifestyles). Here is an overview of the built-in lifestyles ordered by their length:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=PotentialLifestyleMismatches&amp;ANCHOR#Transient"&gt;Transient&lt;/a&gt; (shortest)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=PotentialLifestyleMismatches&amp;ANCHOR#PerLifetimeScope"&gt;Lifetime Scope&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=PotentialLifestyleMismatches&amp;ANCHOR#PerWcfOperation"&gt;WCF Operation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=PotentialLifestyleMismatches&amp;ANCHOR#PerWebRequest"&gt;Web Request&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=PotentialLifestyleMismatches&amp;ANCHOR#Singleton"&gt;Singleton&lt;/a&gt; (longest)&lt;/li&gt;&lt;/ul&gt;

&lt;h2&gt;How to Fix Violations&lt;a name="HowToFixViolations"&gt;&lt;/a&gt;&lt;/h2&gt;There are multiple ways to fix this violation:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Change the lifestyle of the component to a lifestyle that is as short or shorter than that of the dependency.&lt;/li&gt;
&lt;li&gt;Change the lifestyle of the dependency to a lifestyle as long or longer than that of the component.&lt;/li&gt;
&lt;li&gt;Instead of injecting the dependency, inject a factory for the creation of that dependency and call that factory every time an instance is required.&lt;/li&gt;&lt;/ul&gt;

&lt;h2&gt;When to Ignore Warnings&lt;a name="WhenToIgnoreWarnings"&gt;&lt;/a&gt;&lt;/h2&gt;Do not ignore these warnings. False positives for this warning are rare and even when they occur, the registration or the application design can always be changed in a way that the warning disappears.&lt;br /&gt;
&lt;h2&gt;Example&lt;a name="Example"&gt;&lt;/a&gt;&lt;/h2&gt;The following example shows a configuration that will trigger the warning:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.Register&amp;lt;IUserRepository, InMemoryUserRepository&amp;gt;(Lifestyle.Transient);

&lt;span style="color:Green;"&gt;// RealUserService depends on IUserRepository&lt;/span&gt;
container.RegisterSingle&amp;lt;RealUserService&amp;gt;();

&lt;span style="color:Green;"&gt;// FakeUserService depends on IUserRepository&lt;/span&gt;
container.RegisterSingle&amp;lt;FakeUserService&amp;gt;();

container.Verify();
&lt;/pre&gt;&lt;/div&gt;The &lt;i&gt;RealUserService&lt;/i&gt; component is registered as &lt;b&gt;Singleton&lt;/b&gt; but it depends on &lt;i&gt;IUserRepository&lt;/i&gt; which is configured with the shorter &lt;b&gt;Transient&lt;/b&gt; lifestyle. Below is an image that shows the output for this configuration in a watch window. The watch window shows two mismatches and one of the warnings is unfolded.&lt;br /&gt;&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=simpleinjector&amp;DownloadId=629612" alt="Diagnostics&amp;#32;debugger&amp;#32;view&amp;#32;watch&amp;#32;window&amp;#32;with&amp;#32;lifestyle&amp;#32;mismatches" title="Diagnostics&amp;#32;debugger&amp;#32;view&amp;#32;watch&amp;#32;window&amp;#32;with&amp;#32;lifestyle&amp;#32;mismatches" /&gt;&lt;br /&gt;
&lt;h3&gt;What about Hybrid lifestyles?&lt;/h3&gt;A &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=PotentialLifestyleMismatches&amp;ANCHOR#Hybrid"&gt;Hybrid&lt;/a&gt; lifestyle is a mix between two or more other lifestyles. Here is an example of a custom lifestyle that mixes the &lt;b&gt;Transient&lt;/b&gt; and &lt;b&gt;Singleton&lt;/b&gt; lifestyles together:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; hybrid = Lifestyle.CreateHybrid(
    lifestyleSelector: () =&amp;gt; someCondition,
    trueLifestyle: Lifestyle.Transient,
    falseLifestyle: Lifestyle.Singleton);
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt; that this example is quite bizarre, since it is a very unlikely combination of lifestyles to mix together, but it serves us well for the purpose of this explanation.&lt;/i&gt;&lt;/blockquote&gt;
As explained, components should only depend on longer lived components. But how long does a component with this hybrid lifestyle live? For components that are configured with the lifestyle defined above, it depends on the implementation of `someCondition`. But without taking this condition into consideration, we can say that it will at most live as long as the longest wrapped lifestyle (Singleton in this case) and at least live as long as shortest wrapped lifestyle (in this case Transient).&lt;br /&gt;&lt;br /&gt;From the &lt;b&gt;Diagnostic Services&lt;/b&gt;&amp;#39; perspective, a component can only safely depend on a hybrid lifestyled service if the consuming component&amp;#39;s lifestyle is shorter than or equal the shortest lifestyle the hybrid is composed of. On the other hand, a hybrid lifestyled component can only safely depend on another service when the longest lifestyle of the hybrid is shorter than or equal to the lifestyle of the dependency. Thus, when a relationship between a component and its dependency is evaluated by the &lt;b&gt;Diagnostic Services&lt;/b&gt;, the &lt;u&gt;longest&lt;/u&gt; lifestyle is used in the comparison when the hybrid is part of the consuming component, and the &lt;u&gt;shortest&lt;/u&gt; lifestyle is used when the hybrid is part of the dependency.&lt;br /&gt;&lt;br /&gt;This does imply that two components with the same hybrid lifestyle can&amp;#39;t safely depend on each other. This is true since in theory the supplied predicate could change results in each call. In practice however, those components would usually be able safely relate, since it is normally unlikely that the predicate changes lifestyles within a single object graph. This is an exception the &lt;b&gt;Diagnostic Services&lt;/b&gt; can make pretty safely. From the &lt;b&gt;Diagnostic Services&lt;/b&gt;&amp;#39; perspective, components can safely be related when both share the exact same lifestyle instance and no warning will be displayed in this case. This does mean however, that you should be very careful using predicates that change the lifestyle during the object graph.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Thu, 02 May 2013 21:34:03 GMT</pubDate><guid isPermaLink="false">Updated Wiki: PotentialLifestyleMismatches 20130502093403P</guid></item><item><title>Updated Wiki: How-to</title><link>https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;version=30</link><description>&lt;div class="wikidoc"&gt;&lt;h1&gt;How To...&lt;/h1&gt;
&lt;ul&gt;&lt;li&gt;How to &lt;a href="#Register-Factory-Delegates"&gt;Register factory delegates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Resolve-Instances-By-Key"&gt;Resolve instances by key&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Resolve-Arrays-And-Lists"&gt;Resolve array and lists&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Register-Multiple-Interfaces-With-The-Same-Implementation"&gt;Register multiple interfaces with the same implementation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Override-Existing-Registrations"&gt;Override existing registrations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Verifying-Configuration"&gt;Verify the container&amp;#39;s configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Multi-Threaded-Applications"&gt;Work with dependency injection in multi-threaded applications&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;h2&gt;Register factory delegates&lt;a name="Register-Factory-Delegates"&gt;&lt;/a&gt;&lt;a name="Register_Factory_Delegates"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;b&gt;Simple Injector&lt;/b&gt; allows you to register a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate for the creation of an instance. This is especially useful in scenarios where it is impossible for the container to create the instance. There are overloads of the &lt;i&gt;Register&lt;/i&gt; method available that accept an &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; argument:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IMyService&amp;gt;(() =&amp;gt; SomeSubSystem.CreateMyService());
&lt;/pre&gt;&lt;/div&gt;In situations where a service needs to create multiple instances of a certain dependencies, or needs to explicitly control the lifetime of such dependency, abstract factories can be used. Instead of injecting an &lt;i&gt;IMyService&lt;/i&gt;, you should inject an &lt;i&gt;IMyServiceFactory&lt;/i&gt; that allows services to create new instances of &lt;i&gt;IMyService&lt;/i&gt;:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IMyServiceFactory
{
    IMyService CreateNew();
}

&lt;span style="color:Green;"&gt;// Implementation&lt;/span&gt;
&lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ServiceFactory : IMyServiceFactory
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IMyService CreateNew()
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; MyServiceImpl();
    }
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterSingle&amp;lt;IMyServiceFactory, ServiceFactory&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IMyServiceFactory factory;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(IMyServiceFactory factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service1 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.CreateNew())
    	{
            &lt;span style="color:Green;"&gt;// use service 1&lt;/span&gt;
        }

        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service2 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.CreateNew())
    	{
            &lt;span style="color:Green;"&gt;// use service 2&lt;/span&gt;
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;Instead of creating specific interfaces for your factories, you can also choose to inject &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates into your services:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterSingle&amp;lt;Func&amp;lt;IMyService&amp;gt;&amp;gt;(
    () =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; MyServiceImpl());

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;IMyService&amp;gt; factory;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(Func&amp;lt;IMyService&amp;gt; factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service1 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.Invoke())
    	{
            &lt;span style="color:Green;"&gt;// use service 1&lt;/span&gt;
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;This saves you from having to define a new interface and implementation per factory.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: On the downside however, this communicates less clearly the intend of your code and as a result might make your code harder to grasp for other developers.&lt;/i&gt;&lt;/blockquote&gt;
When you choose &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates over specific factory interfaces, you can define the following extension method that makes it easier to register &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; factories:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Extension method&lt;/span&gt;
container.RegisterFuncFactory&amp;lt;T&amp;gt;(&lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;
{
    container.RegisterSingle&amp;lt;Func&amp;lt;T&amp;gt;&amp;gt;(
        () =&amp;gt; container.GetInstance&amp;lt;T&amp;gt;());
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterFuncFactory&amp;lt;IMyService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Warning&lt;/b&gt;: Although the use of factories can be useful, they do pose a thread in the maintainability of your application. Because the creation of instances is postponed to a later moment in time, their creation is not tested by the container when calling &lt;b&gt;Verify&lt;/b&gt; or when requesting an object that depends on the Func&amp;lt;T&amp;gt;. When there is a misconfiguration, your application could break much later in time, instead of when starting (or unit testing) your application. For more information about creating an application and container configuration that can be successfully verified, please read the &lt;a href="#Verifying-Configuration"&gt;How To Verify the container&amp;#8217;s configuration&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;
&lt;a name="lazy"&gt;&lt;/a&gt;Just like &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates can be injected, &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; instances can be injected into services. &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; is useful in situations where the creation of a service is time consuming, but is not always used. Thus &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; allows you to postpone the creation of that service until the moment that it is really needed:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Extension method&lt;/span&gt;
container.RegisterLazy&amp;lt;T&amp;gt;(&lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;
{
    Func&amp;lt;T&amp;gt; factory = () =&amp;gt; container.GetInstance&amp;lt;T&amp;gt;();

    container.Register&amp;lt;Lazy&amp;lt;T&amp;gt;&amp;gt;(() =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Lazy&amp;lt;T&amp;gt;(factory));
}

&lt;span style="color:Green;"&gt;// Registration	&lt;/span&gt;
container.RegisterLazy&amp;lt;IMyService&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Lazy&amp;lt;IMyService&amp;gt; myService;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(Lazy&amp;lt;IMyService&amp;gt; myService)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.myService = myService;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (someCondition)
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.myService.Value.Operate();
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;Note that instead of polluting the API of your application with &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; dependencies, it is usually cleaner to hide the &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; behind a proxy:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Proxy definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyLazyServiceProxy : IMyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Lazy&amp;lt;IMyService&amp;gt; wrapped;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyLazyServiceProxy(Lazy&amp;lt;IMyService&amp;gt; wrapped)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.wrapped = wrapped;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Operate()
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.wrapped.Value.Operate();
    }
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterLazy&amp;lt;IMyService&amp;gt;();
container.Register&amp;lt;IMyService, MyLazyServiceProxy&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;This way the application can simply depend on &lt;i&gt;IMyService&lt;/i&gt; instead of &lt;i&gt;Lazy&amp;lt;IMyService&amp;gt;&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Warning&lt;/b&gt;: The same warning applies to the use of Lazy&amp;lt;T&amp;gt; as it does for the use of Func&amp;lt;T&amp;gt; delegates. For more information about creating an application and container configuration that can be successfully verified, please read the &lt;a href="#Verifying-Configuration"&gt;How To Verify the container&amp;#8217;s configuration&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;&lt;h2&gt;Resolve instances by key&lt;a name="Resolve-Instances-By-Key"&gt;&lt;/a&gt;&lt;a name="Resolve_Instances_By_Key"&gt;&lt;/a&gt;&lt;/h2&gt;Resolving instances by a key is a feature that is deliberately left out of the &lt;b&gt;Simple Injector&lt;/b&gt;, because we feel it steers developers to a design were the application tends to have a dependency on the DI container itself. For resolving a keyed instance, you will often need to call into the container itself, which leads to the &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator anti-pattern&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;This doesn’t mean however, that resolving instances by a key is never useful. Resolving instances by a key however, is much more a job for a specific factory than for a container itself. This makes the design much cleaner, saves you from having to take a dependency on the DI framework, and enables many scenarios that the DI container writer just didn’t consider.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The need for keyed registration can be an indication of ambiguity in the application design. Take a good look if each keyed registration shouldn&amp;#39;t have its own unique interface, or perhaps each registration should implement each own version of a generic interface.&lt;/i&gt;&lt;/blockquote&gt;
Take a look at the following scenario, where we want to retrieve instances of type &lt;i&gt;IRequestHandler&lt;/i&gt; by a string key. There are of course several ways to achieve this, but here is a simple but effective way, by defining an &lt;i&gt;IRequestHandlerFactory&lt;/i&gt;:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IRequestHandlerFactory
{
    IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name);
}

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; factory =
    container.GetInstance&amp;lt;IRequestHandlerFactory&amp;gt;();
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = factory.CreateNew(&lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;);
handler.Handle(requestContext);
&lt;/pre&gt;&lt;/div&gt;By inheriting from the BCL’s &lt;i&gt;Dictionary&amp;lt;TKey, TValue&amp;gt;&lt;/i&gt;, creating an &lt;i&gt;IRequestHandlerFactory&lt;/i&gt; implementation, is almost a one-liner:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory
    : Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, Func&amp;lt;IRequestHandler&amp;gt;&amp;gt;,
    IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;[name]();
    }
}
&lt;/pre&gt;&lt;/div&gt;With this class, we can register &lt;i&gt;Func&amp;lt;IRequestHandler&amp;gt;&lt;/i&gt; factory methods by a key. With this in place the registration of keyed instances is a breeze:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();
 
container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory
{
    { &lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;DefaultRequestHandler&amp;gt;() },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;OrdersRequestHandler&amp;gt;() },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;CustomersRequestHandler&amp;gt;() },
});
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: this design will work with almost all DI containers, making this not only a design that is easy to follow, but making it very easy to migrate to another DI implementation.&lt;/i&gt;&lt;/blockquote&gt;
If you don’t like a design that uses &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates this way, it can easily be changed to be a &lt;b&gt;Dictionary&amp;lt;string, Type&amp;gt;&lt;/b&gt; instead. The &lt;i&gt;RequestHandlerFactory&lt;/i&gt; can be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory
    : Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, Type&amp;gt;, IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Container container;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; RequestHandlerFactory(Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container.GetInstance(&lt;span style="color:Blue;"&gt;this&lt;/span&gt;[name]);
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; (IRequestHandler)handler;
    }
}
&lt;/pre&gt;&lt;/div&gt;The registration will then look as follows:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory(container)
{
    { &lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DefaultRequestHandler) },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(OrdersRequestHandler) },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CustomersRequestHandler) },
});
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Please remember the previous note about ambiguity in the application design. In the given example the design would probably be better af by using a generic &lt;b&gt;IRequestHandler&amp;lt;TRequest&amp;gt;&lt;/b&gt; interface. This would allow the implementations to be &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Batch_Registration"&gt;batch registered using a single line of code&lt;/a&gt;, saves you from using keys, and results in a configuration the is &lt;a href="#Verifying-Configuration"&gt;verifiable by the container&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;&lt;h2&gt;Resolve array and lists&lt;a name="Resolve-Arrays-And-Lists"&gt;&lt;/a&gt;&lt;a name="Resolve_Arrays_And_Lists"&gt;&lt;/a&gt;&lt;/h2&gt;The &lt;b&gt;Simple Injector&lt;/b&gt; allows registration of collections of elements using the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Container_RegisterAll.htm"&gt;RegisterAll&lt;/a&gt; method overloads. Collections can be resolved by using one of the &lt;b&gt;GetAllInstances&amp;lt;T&amp;gt;()&lt;/b&gt; method, by calling &lt;b&gt;GetInstance&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt;()&lt;/b&gt;, or by defining an &lt;i&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/i&gt; parameter in the constructor of a type that is created using automatic constructor injection.&lt;br /&gt;&lt;br /&gt;Injection of other collection types, such as &lt;i&gt;arrays of T&lt;/i&gt; or &lt;i&gt;IList&amp;lt;T&amp;gt;&lt;/i&gt; into constructors is not supported out of the box. By hooking onto the unregistered type resolution event however, this functionality can be added. Look &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=CollectionRegistrationExtensions&amp;referringTitle=How-to"&gt;here&lt;/a&gt; for an example extension method that allows this behavior for &lt;i&gt;T[]&lt;/i&gt; types.&lt;br /&gt;&lt;br /&gt;Please take a look at your design if you think you need to work with a collection of items. Often you can succeed by creating a composite type that can be injected. Take the following interface for instance:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; ILogger
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message);
}
&lt;/pre&gt;&lt;/div&gt;Instead of injecting a collection of dependencies, the consumer might not really be interested in the collection, but simply wishes to operate on all elements. In that scenario you can configure your container to inject a composite of that particular type. That composite might look as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CompositeLogger : ILogger
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger[] loggers;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; CompositeLogger(&lt;span style="color:Blue;"&gt;params&lt;/span&gt; ILogger[] loggers)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers = loggers ?? &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ILogger[0];
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; logger &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers)
        {
            logger.Log(message);
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;A composite allows you to remove this boilerplate iteration logic from the application, which makes the application cleaner and when changes have to be made to the way the collection of loggers is processed, only the composite has to be changed.
&lt;h2&gt;Register multiple interfaces with the same implementation&lt;a name="Register-Multiple-Interfaces-With-The-Same-Implementation"&gt;&lt;/a&gt;&lt;a name="Register_Multiple_Interfaces_With_The_Same_Implementation"&gt;&lt;/a&gt;&lt;/h2&gt;
To adhere to the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Interface%20Segregation%20Principle&amp;referringTitle=How-to"&gt;Interface Segregation Principle&lt;/a&gt;, it is important to keep interfaces narrow. Although in most situations implementations implement a single interface, it can sometimes be beneficial to have multiple interfaces on a single implementation. Here is an example of how to register this:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Impl implements IInterface1, IInterface2 and IInterface3.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; registration =
    Lifestyle.Singleton.CreateRegistration&amp;lt;Impl, Impl&amp;gt;(container);

container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface1), registration);
container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface2), registration);
container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface3), registration);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; a = container.GetInstance&amp;lt;IInterface1&amp;gt;();
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; b = container.GetInstance&amp;lt;IInterface2&amp;gt;();

&lt;span style="color:Green;"&gt;// Since Impl is a singleton, both requests return the same instance.&lt;/span&gt;
Assert.AreEqual(a, b);
&lt;/pre&gt;&lt;/div&gt;The first line creates a &lt;i&gt;Registration&lt;/i&gt; instance for the &lt;i&gt;Impl&lt;/i&gt;, in this case with a singleton lifestyle. The other lines add this registration to the container, once for each interface. This maps multiple service types to the exact same registration.
&lt;h2&gt;Override existing registrations&lt;a name="Override-Existing-Registrations"&gt;&lt;/a&gt;&lt;a name="Override_Existing_Registrations"&gt;&lt;/a&gt;&lt;/h2&gt;The default behavior of the &lt;b&gt;Simple Injector&lt;/b&gt; is to fail when a service is registered for a second time. Most of the time the developer didn&amp;#39;t intend to override a previous registration and allowing this would lead to a configuration that would pass the container&amp;#39;s verification, but doesn&amp;#39;t behave as expected.&lt;br /&gt;&lt;br /&gt;There are certain scenarios however where overriding is useful. An example of such is a bootstrapper project for a Business layer that is reused in multiple applications (in both a web application, web service, and Windows service for instance). Not having a business layer specific bootstrapper project would mean the complete DI configuration would be duplicated in the startup path of each application, which would lead to code duplication. In that situation the applications would roughly have the same configuration, with a few adjustments.&lt;br /&gt;&lt;br /&gt;Best is to start of by configuring all possible dependencies in the BL bootstrapper and leave out the service registrations where the implementation differs for each application. In other words, the BL bootstrapper would result in an incomplete configuration. After that, each application can finish the configuration by registering the missing dependencies. This way you still don&amp;#39;t need to override the existing configuration.&lt;br /&gt;&lt;br /&gt;In certain scenarios it can be beneficial to allow an application override an existing configuration. The container can be configured to allow overriding as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.Options.AllowOverridingRegistrations = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;

&lt;span style="color:Green;"&gt;// Register IUserService.&lt;/span&gt;
container.Register&amp;lt;IUserService, FakeUserService&amp;gt;();

&lt;span style="color:Green;"&gt;// Replaces the previous registration&lt;/span&gt;
container.Register&amp;lt;IUserService, RealUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;The previous example created a &lt;i&gt;Container&lt;/i&gt; instance that allows overriding. It is also possible to enable overriding half way the registration process:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Create a container with overriding disabled&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

&lt;span style="color:Green;"&gt;// Pass container to the business layer.&lt;/span&gt;
BusinessLayer.Bootstrapper.Bootstrap(container);

&lt;span style="color:Green;"&gt;// Enable overriding&lt;/span&gt;
container.Options.AllowOverridingRegistrations = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;

&lt;span style="color:Green;"&gt;// Replaces the previous registration&lt;/span&gt;
container.Register&amp;lt;IUserService, RealUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;h2&gt;Verify the container’s configuration&lt;a name="Verifying-Configuration"&gt;&lt;/a&gt;&lt;a name="Verifying_Configuration"&gt;&lt;/a&gt;&lt;/h2&gt;Dependency Injection promotes the concept of programming against abstractions. This makes your code much easier to test, easier to change and more maintainable. However, since the code itself isn&amp;#39;t responsible of maintaining the dependencies between implementations, the compiler will not be able to verify whether the dependency graph is correct.&lt;br /&gt;&lt;br /&gt;When starting to use a Dependency Injection container, many developers see their application fail when it is deployed in staging or sometimes even production, because of container misconfigurations. This makes developers often conclude that dependency injection is bad, since the dependency graph cannot be verified. This conclusion however, is incorrect. Although it is impossible for the compiler to verify the dependency graph, verifying the dependency graph is still possible and advisable.&lt;br /&gt;&lt;br /&gt;Simple Injector contains a &lt;b&gt;Verify()&lt;/b&gt; method, that will simply iterate over all registrations and resolve an instance for each registration. Calling this method directly after configuring the container, allows the application to fail during start-up, when the configuration is invalid.&lt;br /&gt;&lt;br /&gt;Calling the &lt;b&gt;Verify()&lt;/b&gt; method however, is just part of the story. It is very easy to create a configuration that passes any verification, but still fails at runtime. Here are some tips to help building a verifiable configuration:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Stay away from &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Implicit_Property_Injection"&gt;implicit property injection&lt;/a&gt;, where the container is allowed to skip injecting the property if a corresponding or correctly registered dependency can&amp;#39;t be found. This will disallow your application to fail fast and will result in &lt;b&gt;NullReferenceException&lt;/b&gt;s later on. Only use implicit property injection when the property is truly optional, omitting the dependency still keeps the configuration valid, and the application still runs correctly without that dependency. Truly optional dependencies should be very rare though, since most of the time you should prefer injecting empty implementations (a.k.a. the &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object pattern&lt;/a&gt;) instead of allowing dependencies to be a null reference. &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=How-to&amp;ANCHOR#Configuring_Property_Injection"&gt;Explicit property injection&lt;/a&gt; on the other hand is fine. With explicit property injection you force the container to inject a property and it will fail when it can&amp;#39;t succeed. However, you should prefer constructor injection whenever possible. Note that the need for property injection is often an indication of problems in the design. If you revert to property injection because you otherwise have too many constructor arguments, you&amp;#39;re probably violating the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Register all root objects explicitly if possible. For instance, register all ASP.NET MVC Controller instances explicitly in the container (Controller instances are requested directly and are therefore called &amp;#39;root objects&amp;#39;). This way the container can check the complete dependency graph starting from the root object when you call &lt;b&gt;Verify()&lt;/b&gt;. Prefer registering all root objects in an automated fashion, for instance by using reflection to find all root types. The &lt;a href="http://nuget.org/packages/SimpleInjector.Integration.Web.Mvc"&gt;Simple Injector ASP.NET MVC Integration NuGet Package&lt;/a&gt; for instance, contains a &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/M_SimpleInjector_SimpleInjectorMvcExtensions_RegisterMvcControllers.htm"&gt;RegisterMvcControllers&lt;/a&gt; extension method that will do this for you and the &lt;a href="http://nuget.org/packages/SimpleInjector.Integration.Wcf"&gt;WCF Integration NuGet Package&lt;/a&gt; contains a &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary.v2/?topic=html/M_SimpleInjector_SimpleInjectorWcfExtensions_RegisterWcfServices.htm"&gt;RegisterWcfServices&lt;/a&gt; extension method for this purpose.&lt;/li&gt;
&lt;li&gt;If registering root objects is not possible or feasible, test the creation of each root object manually during start-up. With ASP.NET Web Form Page classes for instance, you will probably call the container from within their constructor (since Page classes must unfortunately have a default constructor). The key here again is finding them all in once using reflection. By finding all Page classes using reflection and instantiating them, you&amp;#39;ll find out (during app start-up or through automated testing) whether there is a problem with your DI configuration or not. The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Web%20Forms%20Integration&amp;referringTitle=How-to"&gt;Web Forms Integration&lt;/a&gt; guide contains an example of how to verify page classes.&lt;/li&gt;
&lt;li&gt;There are scenarios where some dependencies cannot yet be created during application start-up. To ensure that the application can be started normally and the rest of the DI configuration can still be verified, abstract those dependencies behind a proxy or abstract factory. Try to keep those unverifiable dependencies to a minimum and keep good track of them, because you will probably have to test them manually.&lt;/li&gt;
&lt;li&gt;But even when all registrations can be resolved succesfully by the container, that still doesn&amp;#39;t mean your configuration is correct. It is very easy to accidentally misconfigure the container in a way that only shows up late in the development process. &lt;b&gt;Simple Injector&lt;/b&gt; contains &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=How-to"&gt;Debugger Diagnostics&lt;/a&gt; to help you spot common configuration mistakes. Use these diagnostic services and use them often.&lt;/li&gt;&lt;/ol&gt;
&lt;h2&gt;Work with dependency injection in multi-threaded applications&lt;a name="Multi-Threaded-Applications"&gt;&lt;/a&gt;&lt;a name="Multi_Threaded_Applications"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note:&lt;/b&gt; Simple Injector is designed for use in highly-concurrent applications. Its lock-free design allows it to scale linearly with the number of threads and processors in your system.&lt;/i&gt;&lt;/blockquote&gt;
Many applications and application frameworks are inherently multi-threaded. Working in multi-threaded applications forces developers to take special care. It is easy for a less experienced developer to introduce a race condition in the code. Even although some frameworks such as ASP.NET make it easy to write thread-safe code, introducing a simple static field could break thread-safety.&lt;br /&gt;&lt;br /&gt;This same holds when working with DI containers in multi-threaded applications. The developer that configures the container, should be aware of the risks of shared state. &lt;b&gt;Not knowing which configured services are thread-safe is a sin.&lt;/b&gt; Registering a service that is not thread-safe as singleton, will eventually lead to concurrency bugs, that usually only appear in production code. Those bugs are often hard to reproduce and hard to find, making them costly to fix. And even when you correctly configured a service with the correct lifestyle, when another component that depends on it accidentally as a longer lifetime, the service might be kept alive much longer and might even be accessible from other threads.&lt;br /&gt;&lt;br /&gt;Dependency injection however, can actually help in writing multi-threaded applications. Dependency injection forces you to wire all dependencies together in a single place in the application, the &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot/"&gt;Composition Root&lt;/a&gt;. This means that there is a single place in the application that knows about how services behave, whether they are thread-safe, and how they should be wired. Without this centralization, this knowledge would be scattered throughout the code base, making it very hard to change the behavior of a service.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: Take a close look at the &amp;#39;Potential Lifestyle Mismatches&amp;#39; warnings in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=How-to"&gt;Debugger Diagnostics&lt;/a&gt;. Lifestyle mismatches are a source of concurrency bugs.&lt;/i&gt;&lt;/blockquote&gt;
In a multi-threaded application, each thread should get its own object graph. This means that you should typically call &lt;b&gt;container.GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; once at the beginning of the thread to get the root object for processing that thread (or request). The container will build an object graph with all root object&amp;#39;s dependencies. Some of those dependencies will be singletons; shared between all threads. Other dependencies might be transient; a new instance is created per dependency. Other dependencies might be thread-specific, request-specific, or with some other lifestyle. The application code itself is unaware of the way the dependencies are registered and that&amp;#39;s the way it is supposed to be.&lt;br /&gt;&lt;br /&gt;For web applications, you typically call &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; at the beginning of the web request. In an ASP.NET MVC application for instance, one Controller instance will be requested from the container (by the Controller Factory) per web request. When using one of the integration packages, such as the &lt;a href="https://nuget.org/packages/SimpleInjector.MVC3"&gt;Simple Injector MVC Integration Quick Start NuGet package&lt;/a&gt; for instance, you don&amp;#39;t have to call &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; yourself, the package will ensure this is done for you. Still, &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; is typically called once per request.&lt;br /&gt;&lt;br /&gt;The advice of building a new object graph (calling &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt;) at the beginning of a thread, also holds when manually starting a new thread. Although you can pass on data to other threads, you should not pass on container controlled dependencies to other threads. On each new thread, you should ask the container again for the dependencies. When you start passing dependencies from one thread to the other, those parts of the code have to know whether it is safe to pass those dependencies on. For instance, are those dependencies thread-safe? This might be trivial to analyze in some situations, but prevents you to change those dependencies with other implementations, since now you have to remember that there is a place in your code where this is happening and you need to know which dependencies are passed on. You are decentralizing this knowledge again, making it harder to reason about the correctness of your DI configuration and making it easier to misconfigure the container in a way that causes those concurrency problems.&lt;br /&gt;&lt;br /&gt;Running code on a new thread can be done by adding a little bit of infrastructural code. Take for instance the following example where we want to send e-mail messages asynchronously. Instead of letting the caller implement this logic, it is better to hide the logic for asynchronicity behind an abstraction; a proxy. This ensures that this logic is centralized to a single place, and by placing this proxy inside the composition root, we prevent the application code to take a dependency on the container itself (which should be prevented).&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Synchronous implementation of IMailSender&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RealMailSender : IMailSender
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IMailFormatter formatter;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RealMailSender(IMailFormatter formatter)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.formatter = formatter;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IMailSender.SendMail(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// format mail&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// send mail&lt;/span&gt;
    }
}

&lt;span style="color:Green;"&gt;// Proxy for executing IMailSender asynchronously.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AsyncMailSenderProxy : IMailSender
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;IMailSender&amp;gt; mailSenderFactory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AsyncMailSenderProxy(ILogger logger,
        Func&amp;lt;IMailSender&amp;gt; mailSenderFactory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = logger;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.mailSenderFactory = mailSenderFactory;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IMailSender.SendMail(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// Run on a new thread&lt;/span&gt;
        Task.Factory.StartNew(() =&amp;gt;
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.SendMailAsync(to, message);
        });        
    }

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SendMailAsync(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// Here we run on a different thread and the&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// services should be requested on this thread.&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; mailSender = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.mailSenderFactory();

        &lt;span style="color:Blue;"&gt;try&lt;/span&gt;
        {
            mailSender.SendMail(to, message);
        }
        &lt;span style="color:Blue;"&gt;catch&lt;/span&gt; (Exception ex)
        {
            &lt;span style="color:Green;"&gt;// logging is important, since we run on a&lt;/span&gt;
            &lt;span style="color:Green;"&gt;// different thread.&lt;/span&gt;
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger.Log(ex);
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;In the Composition Root, instead of registering the &lt;i&gt;MailSender&lt;/i&gt;, we register the &lt;i&gt;AsyncMailSenderProxy&lt;/i&gt; as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;ILogger, FileLogger&amp;gt;(Lifestyle.Singleton);
container.Register&amp;lt;IMailSender, RealMailSender&amp;gt;();
container.RegisterSingleDecorator(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IMailSender),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncMailSenderProxy));
&lt;/pre&gt;&lt;/div&gt;In this case the container will ensure that when an &lt;i&gt;IMailSender&lt;/i&gt; is requested, a single &lt;i&gt;AsyncMailSenderProxy&lt;/i&gt; is returned with a &lt;i&gt;Func&amp;lt;IMailSender&amp;gt;&lt;/i&gt; delegate that will create a new &lt;i&gt;RealMailSender&lt;/i&gt; when requested. The &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterDecorator.htm"&gt;RegisterDecorator&lt;/a&gt; and &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterSingleDecorator.htm"&gt;RegisterSingleDecorator&lt;/a&gt; overloads natively understand how to handle &lt;i&gt;Func&amp;lt;Decoratee&amp;gt;&lt;/i&gt; dependencies. The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Generic_Decorators"&gt;Decorators&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to"&gt;Advanced-scenarios&lt;/a&gt; wiki page explains more about registering decorators.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Thu, 02 May 2013 20:58:13 GMT</pubDate><guid isPermaLink="false">Updated Wiki: How-to 20130502085813P</guid></item><item><title>Updated Wiki: Using the Simple Injector</title><link>https://simpleinjector.codeplex.com/wikipage?title=Using the Simple Injector&amp;version=21</link><description>&lt;div class="wikidoc"&gt;&lt;blockquote&gt;&lt;i&gt;Note: this documentation is specific for &lt;b&gt;Simple Injector version 2.0&lt;/b&gt; and up. Look &lt;a href="http://simpleinjector.codeplex.com/wikipage?title=Using the Simple Injector&amp;amp;version=19"&gt;here&lt;/a&gt; for 1.x specific documentation.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h2&gt;Using the Simple Injector&lt;/h2&gt;This section will walk you through the basics of the &lt;b&gt;Simple Injector&lt;/b&gt;. After reading this section, you will have a good idea how to use the &lt;b&gt;Simple Injector&lt;/b&gt;.&lt;br /&gt;
&lt;h3&gt;Using the Simple Injector&lt;a name="Using_Simple_Injector"&gt;&lt;/a&gt;&lt;/h3&gt;A good practice is to minimize the dependency between your application and the IoC framework. This increases the testability and the flexibility of your application, results in cleaner code, and makes it easier to migrate to another IoC framework if ever required. Minimizing can be achieved by designing the types in your application around the constructor injection pattern. Define all dependencies of a class in the single public constructor of that type. Do this for all service types that need to be resolved and resolve only the top most types in the application directly (let the container build up the complete graph of dependent objects for you).&lt;br /&gt;&lt;br /&gt;The &lt;b&gt;Simple Injector&lt;/b&gt;&amp;#39;s main type is the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/T_SimpleInjector_Container.htm"&gt;Container&lt;/a&gt; class. An instance of &lt;i&gt;Container&lt;/i&gt; is used to register mappings between an abstraction (service) and implementation (component). Your application code should depend on abstractions, and it is the role of the &lt;i&gt;Container&lt;/i&gt; to supply the application with the right implementation. The easiest way to look at a &lt;i&gt;Container&lt;/i&gt; is as a big dictionary where the type of the abstraction is used as key, and its value is the definition of how to create the implementation. When the application requests for a service, it is looked up in the dictionary, and the correct implementation is returned.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: You should typically create a single Container instance for the whole application (one instance per app domain); Container instances are thread-safe.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Warning&lt;/b&gt;: Do not create an infinite number of Container instances (such as one instance per request). This could drain the performance of your application. The container is optimized for using a limited number of &lt;/i&gt;Container&lt;i&gt; instances. Creating and initializing a container instance has a lot of overhead, but is extremely fast once initialized.&lt;/i&gt;&lt;/blockquote&gt;
Creating a new container is done by newing up a new instance and start calling the &lt;b&gt;RegisterXXX&lt;/b&gt; overloads to register new services:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
Container container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; SimpleInjector.Container();

&lt;span style="color:Green;"&gt;// Registrations here&lt;/span&gt;
container.Register&amp;lt;ILogger, FileLogger&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Ideally, the only place in an application that should directly reference and use the &lt;b&gt;Simple Injector&lt;/b&gt; is the startup path. For an ASP.NET Web Forms or MVC application this will usually be the &lt;i&gt;Application_OnStart&lt;/i&gt; event in the &lt;a href="http://msdn.microsoft.com/en-us/library/1xaas8a2%28VS.71%29.aspx"&gt;Global.asax&lt;/a&gt; page of the web application project. For a Windows Forms or console application this will be the &lt;i&gt;Main&lt;/i&gt; method in the application assembly.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;For more information about usage of the &lt;b&gt;Simple Injector&lt;/b&gt; for a specific technology, please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Integration%20Guide&amp;referringTitle=Using%20the%20Simple%20Injector"&gt;Integration Guide&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;
The usage of the &lt;b&gt;Simple Injector&lt;/b&gt; consists of four or five simple steps: 
&lt;ol&gt;&lt;li&gt;Create a new container&lt;/li&gt;
&lt;li&gt;Configure the container (register)&lt;/li&gt;
&lt;li&gt;Optionally verify the container&lt;/li&gt;
&lt;li&gt;Store the container for use by the application&lt;/li&gt;
&lt;li&gt;Retrieve instances from the container (resolve)&lt;/li&gt;&lt;/ol&gt;
While the first three steps are platform agnostic,  performing the last two steps depends on your own taste and which presentation framework you use, but below is an example for an ASP.NET Web Forms application:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; SimpleInjector;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Global : System.Web.HttpApplication 
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; Container Container { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }

    &lt;span style="color:Blue;"&gt;protected&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Application_Start(&lt;span style="color:Blue;"&gt;object&lt;/span&gt; sender, EventArgs e) 
    {
        &lt;span style="color:Green;"&gt;// 1. Create a new Simple Injector container&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

        &lt;span style="color:Green;"&gt;// 2. Configure the container (register)&lt;/span&gt;
        container.Register&amp;lt;IUserService, UserService&amp;gt;(Lifestyle.Transient);
        container.Register&amp;lt;IUserRepository, SqlUserRepository&amp;gt;(Lifestyle.Singleton);

        &lt;span style="color:Green;"&gt;// See below for more configuration examples&lt;/span&gt;

        &lt;span style="color:Green;"&gt;// 3. Optionally verify the container&amp;#39;s configuration.&lt;/span&gt;
        container.Verify();

        &lt;span style="color:Green;"&gt;// 4. Store the container for use by Page classes.&lt;/span&gt;
        Global.Container = container;
    }
}
&lt;/pre&gt;&lt;/div&gt;The example below shows the fifth step inside a ASP.NET Web Page:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;partial&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; User : BasePage
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IUserRepository repository;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; User()
    {
        &lt;span style="color:Green;"&gt;// 5. Retrieve instances from the container (resolve)&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.repository = Global.Container.GetInstance&amp;lt;IUserRepository&amp;gt;();
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = Global.Container.GetInstance&amp;lt;ILogger&amp;gt;();
    }

    &lt;span style="color:Blue;"&gt;protected&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color:Blue;"&gt;object&lt;/span&gt; sender, EventArgs e)
    {
        &lt;span style="color:Green;"&gt;// Use repository and logger here.&lt;/span&gt;
    }
}
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Calling the &lt;b&gt;GetInstance&lt;/b&gt; method in the constructor is suboptimal and should be avoided whenever possible. With ASP.NET Web Forms however, it is hard to completely avoid this. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Web%20Forms%20Integration&amp;referringTitle=Using%20the%20Simple%20Injector"&gt;Integration Guide&lt;/a&gt; for alternatives.&lt;/i&gt;&lt;/blockquote&gt;

&lt;h3&gt;Resolving instances&lt;a name="Resolving_Instances"&gt;&lt;/a&gt;&lt;/h3&gt;The &lt;b&gt;Simple Injector&lt;/b&gt; allows two scenarios by which you can retrieve instances:
&lt;h5&gt;1. Getting an object by a specified type&lt;/h5&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; repository = container.GetInstance&amp;lt;IUserRepository&amp;gt;();

&lt;span style="color:Green;"&gt;// Alternatively, you can use the weakly typed version&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; repository = (IUserRepository)container.GetInstance(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IUserRepository));
&lt;/pre&gt;&lt;/div&gt;&lt;h5&gt;2. Getting a collection of objects by their type&lt;/h5&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
IEnumerable&amp;lt;ICommand&amp;gt; commands = container.GetAllInstances&amp;lt;ICommand&amp;gt;();

&lt;span style="color:Green;"&gt;// Alternatively, you can use the weakly typed version&lt;/span&gt;
IEnumerable&amp;lt;&lt;span style="color:Blue;"&gt;object&lt;/span&gt;&amp;gt; commands = container.GetAllInstances(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommand));
&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;Configuring the Simple Injector&lt;a name="Usage_Configuring_Simple_Injector"&gt;&lt;/a&gt;&lt;/h4&gt;The &lt;b&gt;Container&lt;/b&gt; class consists of several methods that enable registering instances to be retrieved when requested from the application. These methods enable most common scenarios. Here are the most common scenarios with a code example per scenario:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Configuring an automatically constructed single instance to be always returned:&lt;/b&gt;&lt;br /&gt;The following example configures that a single instance of type &lt;i&gt;RealUserService&lt;/i&gt; will always be returned when an instance of &lt;i&gt;IUserService&lt;/i&gt; is requested. The &lt;i&gt;RealUserService&lt;/i&gt; will be constructed using &lt;a href="#Automatic_constructor_injection"&gt;automatic constructor injection&lt;/a&gt;.&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Configuration&lt;/span&gt;
container.RegisterSingle&amp;lt;IUserService, RealUserService&amp;gt;();

&lt;span style="color:Green;"&gt;// Alternatively you can supply a Lifestyle with the same effect.&lt;/span&gt;
container.Register&amp;lt;IUserService, RealUserService&amp;gt;(Lifestyle.Singleton);

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
IUserService service = container.GetInstance&amp;lt;IUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: instances that are declared as &lt;b&gt;Single&lt;/b&gt; should be thread-safe in a multi-threaded environment.&lt;/i&gt;&lt;/blockquote&gt;&lt;b&gt;Configuring a single -manually created- instance to be always returned:&lt;/b&gt;&lt;br /&gt;The following example configures a single instance that will always be returned when an instance of that type will be requested.&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Configuration&lt;/span&gt;
container.RegisterSingle&amp;lt;IUserRepository&amp;gt;(&lt;span style="color:Blue;"&gt;new&lt;/span&gt; SqlUserRepository());

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
IUserRepository repository = container.GetInstance&amp;lt;IUserRepository&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Registering types using automatic constructor injection (auto-wiring) is the preferred way of registering types. Only new up instances manually when automatic constructor injection is not possible.&lt;/i&gt;&lt;/blockquote&gt;
&lt;b&gt;Configuring a single instance using a delegate:&lt;/b&gt;&lt;br /&gt;The following example configures a single instance using a delegate. The container will ensure that the delegate is not called more than once.&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Configuration&lt;/span&gt;
container.RegisterSingle&amp;lt;IUserRepository&amp;gt;(() =&amp;gt; UserRepFactory.Create(&lt;span style="color:#A31515;"&gt;&amp;quot;some constr&amp;quot;&lt;/span&gt;));

&lt;span style="color:Green;"&gt;// Alternatively you can supply the singleton Lifestyle with the same effect.&lt;/span&gt;
container.Register&amp;lt;IUserRepository&amp;gt;(() =&amp;gt; UserRepFactory.Create(&lt;span style="color:#A31515;"&gt;&amp;quot;some constr&amp;quot;&lt;/span&gt;), Lifestyle.Singleton);

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
IUserRepository repository = container.GetInstance&amp;lt;IUserRepository&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Registering types using automatic constructor injection (auto-wiring) is the preferred way of registering types. Only new up instances manually when automatic constructor injection is not possible.&lt;/i&gt;&lt;/blockquote&gt;
&lt;b&gt;Configuring an automatically constructed new instance to be returned:&lt;/b&gt;&lt;br /&gt;By supplying the service type and the created implementation as generic types, the container can create new instances of the implementation (&lt;i&gt;MoveCustomerHandler&lt;/i&gt; in this case) by using &lt;a href="#Automatic_constructor_injection"&gt;automatic constructor injection&lt;/a&gt;.&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Configuration&lt;/span&gt;
container.Register&amp;lt;IHandler&amp;lt;MoveCustomerCommand&amp;gt;, MoveCustomerHandler&amp;gt;();

&lt;span style="color:Green;"&gt;// Alternatively you can supply the transient Lifestyle with the same effect.&lt;/span&gt;
container.Register&amp;lt;IHandler&amp;lt;MoveCustomerCommand&amp;gt;, MoveCustomerHandler&amp;gt;(Lifestyle.Transient);

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = container.GetInstance&amp;lt;IHandler&amp;lt;MoveCustomerCommand&amp;gt;&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Configuring a new instance to be returned on each call using a delegate:&lt;/b&gt;&lt;br /&gt;By supplying a delegate, types can be registered that can not be created by using automatic constructor injection. By calling the container inside the delegate, you can let the container do as much as work for you as possible:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Configuration&lt;/span&gt;
container.Register&amp;lt;IHandler&amp;lt;MoveCustomerCommand&amp;gt;&amp;gt;(() =&amp;gt;
{
    &lt;span style="color:Green;"&gt;// Get a new instance of the concrete MoveCustomerHandler class:&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = container.GetInstance&amp;lt;MoveCustomerHandler&amp;gt;();

    &lt;span style="color:Green;"&gt;// Configure the handler:&lt;/span&gt;
    handler.ExecuteAsynchronously = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;

    &lt;span style="color:Blue;"&gt;return&lt;/span&gt; handler;
});

container.Register&amp;lt;IHandler&amp;lt;MoveCustomerCommand&amp;gt;&amp;gt;(() =&amp;gt; { ... }, Lifestyle.Transient);
&lt;span style="color:Green;"&gt;// Alternatively you can supply the transient Lifestyle with the same effect.&lt;/span&gt;
&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = container.GetInstance&amp;lt;IHandler&amp;lt;MoveCustomerCommand&amp;gt;&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Configuring property injection on an instance:&lt;/b&gt;&lt;a name="Configuring_Property_Injection"&gt;&lt;/a&gt;&lt;br /&gt;For types that need to be injected, define a single public constructor that holds all dependencies whenever possible. In scenarios where constructor injection is not possible, property injection is your second best pick. The previous example already showed an example of this. The preferred way of doing this however, is by using the &lt;b&gt;RegisterInitializer&lt;/b&gt; method:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Configuration&lt;/span&gt;
container.Register&amp;lt;IHandler&amp;lt;MoveCustomerCommand&amp;gt;&amp;gt;, MoveCustomerHandler&amp;gt;();
container.Register&amp;lt;IHandler&amp;lt;ShipOrderCommand&amp;gt;&amp;gt;, ShipOrderHandler&amp;gt;();

&lt;span style="color:Green;"&gt;// MoveCustomerCommand and ShipOrderCommand both inherit from HandlerBase&lt;/span&gt;
container.RegisterInitializer&amp;lt;HandlerBase&amp;gt;(handlerToInitialize =&amp;gt;
{
    handlerToInitialize.ExecuteAsynchronously = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;
});

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler1 = container.GetInstance&amp;lt;IHandler&amp;lt;MoveCustomerCommand&amp;gt;&amp;gt;();
Assert.IsTrue(handler1.ExecuteAsynchronously);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler2 = container.GetInstance&amp;lt;IHandler&amp;lt;ShipOrderCommand&amp;gt;&amp;gt;();
Assert.IsTrue(handler2.ExecuteAsynchronously);
&lt;/pre&gt;&lt;/div&gt;The &lt;i&gt;Action&amp;lt;T&amp;gt;&lt;/i&gt; delegate that is registered using the &lt;b&gt;RegisterInitializer&lt;/b&gt; method will be called after the container created a new instance that inherits from or implements the given T (or inherits from or implements the given T). In the case of the given example, the &lt;i&gt;MoveCustomerHandler&lt;/i&gt; inherits from &lt;i&gt;HandlerBase&lt;/i&gt; and because of this, &lt;i&gt;Action&amp;lt;HandlerBase&amp;gt;&lt;/i&gt; delegate will get called with the reference to the created instance.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The container will not be able to call an initializer delegate on a type that is manually constructed using the &lt;b&gt;new&lt;/b&gt; operator. Use automatic constructor injection whenever possible.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: Multiple initializers can apply to a concrete type and the container will call all initializers that apply to that type. They are guaranteed to run in the same order as they are registered.&lt;/i&gt;&lt;/blockquote&gt;
&lt;b&gt;Configuring a collection of instances to be returned:&lt;/b&gt;&lt;br /&gt;The &lt;b&gt;Simple Injector&lt;/b&gt; contains several methods for registration and resolving collections of types. Here are some examples:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Configuration&lt;/span&gt;
&lt;span style="color:Green;"&gt;// Registering a list of instances that will be created by the container.&lt;/span&gt;
&lt;span style="color:Green;"&gt;// Supplying a collection of types is the preferred way of registering collections.&lt;/span&gt;
container.RegisterAll&amp;lt;ILogger&amp;gt;(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IMailLogger), &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(SqlLogger));

&lt;span style="color:Green;"&gt;// Register a fixed list (these instances should be thread-safe).&lt;/span&gt;
container.RegisterAll&amp;lt;ILogger&amp;gt;(&lt;span style="color:Blue;"&gt;new&lt;/span&gt; MailLogger(), &lt;span style="color:Blue;"&gt;new&lt;/span&gt; SqlLogger());

&lt;span style="color:Green;"&gt;// Using a collection from another subsystem&lt;/span&gt;
container.RegisterAll&amp;lt;ILogger&amp;gt;(Logger.Providers);

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; loggers = container.GetAllInstances&amp;lt;ILogger&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;:  When no instances are registered using RegisterAll, Container.GetAllInstances will always return an empty list.&lt;/i&gt;&lt;/blockquote&gt;
Just as with normal types, the &lt;b&gt;Simple Injector&lt;/b&gt; can inject collections of instances into constructors:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Service : IService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IEnumerable&amp;lt;ILogger&amp;gt; loggers;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Service(IEnumerable&amp;lt;ILogger&amp;gt; loggers)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers = loggers;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IService.DoStuff()
    {
        &lt;span style="color:Green;"&gt;// Log to all loggers&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; logger &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers)
        {
            logger.Log(&lt;span style="color:#A31515;"&gt;&amp;quot;Some message&amp;quot;&lt;/span&gt;);
        }
    }
}

&lt;span style="color:Green;"&gt;// Configuration&lt;/span&gt;
container.RegisterAll&amp;lt;ILogger&amp;gt;(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(MailLogger)), &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(SqlLogger));
container.RegisterSingle&amp;lt;IService, Service&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service = container.GetInstance&amp;lt;IService&amp;gt;();
service.DoStuff();
&lt;/pre&gt;&lt;/div&gt;The &lt;b&gt;RegisterAll&lt;/b&gt; overloads that take a collection of &lt;i&gt;Type&lt;/i&gt; instances, forward the creation of those types to the container, which means that the same rules apply to them. Take a look at the following configuration:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Configuration&lt;/span&gt;
container.Register&amp;lt;MailLogger&amp;gt;(Lifestyle.Singleton);
container.Register&amp;lt;ILogger, FileLogger&amp;gt;();

container.RegisterAll&amp;lt;ILogger&amp;gt;(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(MailLogger)), &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(SqlLogger), &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ILogger));
&lt;/pre&gt;&lt;/div&gt;When the registered collection of &lt;i&gt;ILogger&lt;/i&gt; instance is resolved, the container will resolve each and every one of them using their configuration. When no such registration exists, the type is created with the &lt;i&gt;Transient&lt;/i&gt; lifestyle (which means, that a new instance is created every time the returned collection is iterated). The &lt;i&gt;MailLogger&lt;/i&gt; type however, has an explicit registration and since it is registered as &lt;i&gt;Singleton&lt;/i&gt;, the resolved &lt;i&gt;ILogger&lt;/i&gt; collections will always have the same instance.&lt;br /&gt;&lt;br /&gt;Since the creation is forwarded, also abstract types can be registered using &lt;b&gt;RegisterAll&lt;/b&gt;. In this case the &lt;i&gt;ILogger&lt;/i&gt; type itself is registered using &lt;b&gt;RegisterAll&lt;/b&gt;. This seems like a recursive definition, but it will work nonetheless. In this particular case you could imagine this to be a registration with a default ILogger registration, that is included in the collection of &lt;i&gt;ILogger&lt;/i&gt; instances as well.&lt;br /&gt;&lt;br /&gt;While resolving collections is useful and also works with automatic constructor injection, the registration of composites is preferred over the use of collections as constructor arguments in application code. Register a composite whenever possible, as shown in the example below:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CompositeLogger : ILogger
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger[] loggers;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; CompositeLogger(&lt;span style="color:Blue;"&gt;params&lt;/span&gt; ILogger[] loggers)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers = loggers;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; logger &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers)
            logger.Log(message);
    }
}

&lt;span style="color:Green;"&gt;// Configuration&lt;/span&gt;
container.RegisterSingle&amp;lt;IService, Service&amp;gt;();
container.RegisterSingle&amp;lt;ILogger&amp;gt;(() =&amp;gt; 
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; CompositeLogger(
        container.GetInstance&amp;lt;MailLogger&amp;gt;(),
        container.GetInstance&amp;lt;SqlLogger&amp;gt;()
    )
);

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service = container.GetInstance&amp;lt;IService&amp;gt;();
service.DoStuff();
&lt;/pre&gt;&lt;/div&gt;When using the approach given above, your services don’t need a dependency on &lt;i&gt;IEnumerable&amp;lt;ILogger&amp;gt;&lt;/i&gt;, but can simply have a dependency on the &lt;i&gt;ILogger&lt;/i&gt; interface itself.&lt;br /&gt;
&lt;h4&gt;Verifying the container&amp;#39;s configuration &lt;a name="Verifying_Container"&gt;&lt;/a&gt;&lt;/h4&gt;Optionally, you can call the &lt;b&gt;Verify&lt;/b&gt; method of the &lt;i&gt;Container&lt;/i&gt;. This method allows a fail-fast mechanism to prevent the application to start when the container is misconfigured. The &lt;b&gt;Verify&lt;/b&gt; method checks the container&amp;#39;s configuration by creating an instance of all registered types.&lt;br /&gt;&lt;br /&gt;For more information about creating an application and container configuration that can be succesfully verified, please read the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Using%20the%20Simple%20Injector&amp;ANCHOR#Verifying_Configuration"&gt;How To Verify the container&amp;#8217;s configuration&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;Automatic constructor injection / auto-wiring. &lt;a name="Automatic_constructor_injection"&gt;&lt;/a&gt;&lt;/h3&gt;The &lt;b&gt;Simple Injector&lt;/b&gt; uses the public constructor of a registered type and looks at the arguments of that constructor. The container will resolve instances for the argument types and invokes the constructor using those instances. This mechanism is called automatic constructor injection or auto-wiring and this is one of the core differences that separates DI containers from doing injection manually. For the &lt;b&gt;Simple Injector&lt;/b&gt; to be able to use auto-wiring, the following requirements must be met:
&lt;ol&gt;&lt;li&gt;The type is concrete (not abstract, no interface, no generic type definition).&lt;/li&gt;
&lt;li&gt;The type has exactly one public constructor (this may be a default constructor).&lt;/li&gt;
&lt;li&gt;All types of the arguments on that constructor can be resolved by the container.&lt;/li&gt;&lt;/ol&gt;
The &lt;b&gt;Simple Injector&lt;/b&gt; can create a type even if it hasn’t registered in the container by using constructor injection.&lt;br /&gt;&lt;br /&gt;The following code shows an example of the use of automatic constructor injection. The example shows an &lt;i&gt;IUserRepository&lt;/i&gt; interface with a concrete &lt;i&gt;SqlUserRepository&lt;/i&gt; implementation and a concrete &lt;i&gt;UserService&lt;/i&gt; class. The &lt;i&gt;UserService&lt;/i&gt; class has one public constructor with an &lt;i&gt;IUserRepository&lt;/i&gt; argument. Because the dependencies of the &lt;i&gt;UserService&lt;/i&gt; are registered, the &lt;b&gt;Simple Injector&lt;/b&gt; is able to create a new &lt;i&gt;UserService&lt;/i&gt; instance.&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definitions&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IUserRepository { }
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; SqlUserRepository : IUserRepository { }
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; UserService : IUserService
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; UserService(IUserRepository repository) { }
}

&lt;span style="color:Green;"&gt;// Configuration&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.RegisterSingle&amp;lt;IUserRepository, SqlUserRepository&amp;gt;();
container.RegisterSingle&amp;lt;IUserService, UserService&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service = container.GetInstance&amp;lt;IUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Because UserService is a concrete type, calling container.GetInstance&amp;lt;UserService&amp;gt;() without registering it explicitly will work as well. This can simplify the container’s configuration significantly for more complex scenarios. However, keep in mind that the best practice is to program to an interface, not a concrete type. Prevent using and depending on concrete types whenever possible.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h2&gt;More information&lt;a name="More_information"&gt;&lt;/a&gt;&lt;/h2&gt;For more information about the &lt;b&gt;Simple Injector&lt;/b&gt; please visit the following links: 
&lt;ul&gt;&lt;li&gt;The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Using%20the%20Simple%20Injector"&gt;Simple Injector and object lifetime management&lt;/a&gt; page explains how to configure lifestyles such as &lt;i&gt;transient&lt;/i&gt;, &lt;i&gt;singleton&lt;/i&gt;, and many others.&lt;/li&gt;
&lt;li&gt;See the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Integration%20Guide&amp;referringTitle=Using%20the%20Simple%20Injector"&gt;Integration Guide&lt;/a&gt; for more information about how to integrate the &lt;b&gt;Simple Injector&lt;/b&gt; into your specific application framework.&lt;/li&gt;
&lt;li&gt;For more information about &lt;i&gt;dependency injection&lt;/i&gt; in general, please visit &lt;a href="http://stackoverflow.com/tags/dependency-injection/info"&gt;this page on Stackoverflow&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;If you have any questions about how to use the &lt;b&gt;Simple Injector&lt;/b&gt; or about &lt;i&gt;dependency injection&lt;/i&gt; in general, the experts at &lt;a href="http://stackoverflow.com/questions/ask?tags=simple-injector+%20ioc-container+dependency-injection+.net+c%23"&gt;Stackoverflow.com&lt;/a&gt; are waiting for you.&lt;/li&gt;
&lt;li&gt;For all other &lt;b&gt;Simple Injector&lt;/b&gt; related question and discussions, such as bug reports and feature requests, the &lt;a href="http://simpleinjector.codeplex.com/discussions"&gt;Simple Injector discussion forum&lt;/a&gt; will be the place to start.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Thu, 02 May 2013 20:56:23 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Using the Simple Injector 20130502085623P</guid></item><item><title>New Comment on "Unity"</title><link>https://simpleinjector.codeplex.com/wikipage?title=Unity&amp;ANCHOR#C27308</link><description>See&amp;#58; http&amp;#58;&amp;#47;&amp;#47;stackoverflow.com&amp;#47;questions&amp;#47;16225737</description><author>dot_NET_Junkie</author><pubDate>Thu, 25 Apr 2013 23:16:22 GMT</pubDate><guid isPermaLink="false">New Comment on "Unity" 20130425111622P</guid></item><item><title>New Comment on "Unity"</title><link>https://simpleinjector.codeplex.com/wikipage?title=Unity&amp;ANCHOR#C27307</link><description>Does it have an IContainer interface like unity has&amp;#63;  I can&amp;#39;t seem to find it.</description><author>Vaccano</author><pubDate>Thu, 25 Apr 2013 21:43:27 GMT</pubDate><guid isPermaLink="false">New Comment on "Unity" 20130425094327P</guid></item><item><title>Updated Wiki: Integration Guide</title><link>https://simpleinjector.codeplex.com/wikipage?title=Integration Guide&amp;version=24</link><description>&lt;div class="wikidoc"&gt;&lt;h2&gt;Integration Guide&lt;/h2&gt;The &lt;b&gt;Simple Injector&lt;/b&gt; library can be used in a wide range of .NET technologies, both server side as client side. Jump directly to to the integration page for the application framework of your choice. When the framework of your choice is not listed, doesn&amp;#39;t mean it isn&amp;#39;t supported, but just that we didn&amp;#39;t have the time write it :-)&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=MVC%20Integration&amp;referringTitle=Integration%20Guide"&gt;ASP.NET MVC&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Web%20API%20Integration&amp;referringTitle=Integration%20Guide"&gt;ASP.NET Web API&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Web%20Forms%20Integration&amp;referringTitle=Integration%20Guide"&gt;ASP.NET Web Forms&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Windows%20Forms%20Integration&amp;referringTitle=Integration%20Guide"&gt;Windows Forms&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=WCF%20Integration&amp;referringTitle=Integration%20Guide"&gt;WCF&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=WPF%20Integration&amp;referringTitle=Integration%20Guide"&gt;WPF and Silverlight&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
Besides integration with standard .NET technologies, Simple Injector can be integrated with a wide range of other technologies. Here are a few to help you get started quickly:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&lt;a href="http://stackoverflow.com/questions/10940988/how-to-configure-simple-injector-ioc-to-use-ravendb"&gt;RavenDB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://stackoverflow.com/questions/10555791/using-simpleinjector-with-signalr"&gt;SignalR&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://stackoverflow.com/questions/9984144/what-is-the-correct-way-to-register-fluentvalidation-with-simpleinjector"&gt;Fluent Validations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://simpleinjector.codeplex.com/discussions/283850"&gt;PetaPoco&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=T4MVC%20Integration&amp;referringTitle=Integration%20Guide"&gt;T4MVC&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://stackoverflow.com/questions/14562176/constructor-injection-with-quartz-net-and-simple-injector"&gt;Quartz.NET&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://stackoverflow.com/questions/16123641/membus-and-ioc-simpleinjector-wiring-command-handlers-automatically-by-interfa"&gt;Membus&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
Here is guidance about some general patterns:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&lt;a href="http://stackoverflow.com/questions/10585478"&gt;Working with the Unit of Work pattern&lt;/a&gt; such as Entity Framework&amp;#39;s &lt;i&gt;ObjectContext&lt;/i&gt;, LINQ to SQL&amp;#39;s &lt;i&gt;DataContext&lt;/i&gt;, and NHibernate&amp;#39;s &lt;i&gt;ISession&lt;/i&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://simpleinjector.codeplex.com/discussions/434951"&gt;Working with multi-tenant applications&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Sun, 21 Apr 2013 20:34:59 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Integration Guide 20130421083459P</guid></item><item><title>Updated Wiki: Web API Integration</title><link>https://simpleinjector.codeplex.com/wikipage?title=Web API Integration&amp;version=4</link><description>&lt;div class="wikidoc"&gt;&lt;h1&gt;ASP.NET Web API Integration Guide&lt;/h1&gt;The following code snippet defines a &lt;b&gt;SimpleInjectorWebApiDependencyResolver&lt;/b&gt; class for an IIS hosted ASP.NET Web API application (for self hosted applications, go &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Self%20Hosted%20Web%20API%20Integration&amp;referringTitle=Web%20API%20Integration"&gt;here&lt;/a&gt;). You can create a new C# file in your Web API project and copy paste the code below into it.&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Reflection;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Web.Http.Controllers;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Web.Http.Dependencies;
    
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; SimpleInjector;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; SimpleInjector.Extensions;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; SimpleInjectorWebApiDependencyResolver 
    : IDependencyResolver
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Container container;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; SimpleInjectorWebApiDependencyResolver(
        Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }

    [DebuggerStepThrough]
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IDependencyScope BeginScope()
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;;
    }

    [DebuggerStepThrough]
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;object&lt;/span&gt; GetService(Type serviceType)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; ((IServiceProvider)&lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container)
            .GetService(serviceType);
    }

    [DebuggerStepThrough]
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color:Blue;"&gt;object&lt;/span&gt;&amp;gt; GetServices(Type serviceType)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container.GetAllInstances(serviceType);
    }

    [DebuggerStepThrough]
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Dispose()
    {
    }
}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;The following code snippet shows how to use the &lt;b&gt;SimpleInjectorWebApiDependencyResolver&lt;/b&gt; class.&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;protected&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Application_Start(&lt;span style="color:Blue;"&gt;object&lt;/span&gt; sender, EventArgs e)
{
    &lt;span style="color:Green;"&gt;// Create the container as usual.&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

    &lt;span style="color:Green;"&gt;// register Web API controllers (important!)&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; controllerTypes =
        &lt;span style="color:Blue;"&gt;from&lt;/span&gt; type &lt;span style="color:Blue;"&gt;in&lt;/span&gt; Assembly.GetExecutingAssembly().GetExportedTypes()
        &lt;span style="color:Blue;"&gt;where&lt;/span&gt; &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IHttpController).IsAssignableFrom(type)
        &lt;span style="color:Blue;"&gt;where&lt;/span&gt; !type.IsAbstract
        &lt;span style="color:Blue;"&gt;where&lt;/span&gt; !type.IsGenericTypeDefinition
        &lt;span style="color:Blue;"&gt;where&lt;/span&gt; type.Name.EndsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Controller&amp;quot;&lt;/span&gt;, StringComparison.Ordinal)
        &lt;span style="color:Blue;"&gt;select&lt;/span&gt; type;

    &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; controllerType &lt;span style="color:Blue;"&gt;in&lt;/span&gt; controllerTypes)
    {
        container.Register(controllerType);
    }

    &lt;span style="color:Green;"&gt;// Register your types, for instance:&lt;/span&gt;
    container.Register&amp;lt;IUserRepository, SqlUserRepository&amp;gt;();

    &lt;span style="color:Green;"&gt;// Verify the container configuration&lt;/span&gt;
    container.Verify();

    &lt;span style="color:Green;"&gt;// Register the dependency resolver.&lt;/span&gt;
    GlobalConfiguration.Configuration.DependencyResolver = 
        &lt;span style="color:Blue;"&gt;new&lt;/span&gt; SimpleInjectorWebApiDependencyResolver(container);
}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;With this configuration, ASP.NET Web API will create new &lt;i&gt;ApiController&lt;/i&gt; instances through the container. Because controllers are concrete classes, the container will be able to create them without any registration. However, because of the way Web API requests types from the DependencyResolver, it is very important to register your controllers explicitly. Otherwise, a very generic, non-descriptive exception will be thrown by Web API, when a Controller instance could not be created caused by a misconfiguration. Explicitly registering controller types solves this problem.&lt;br /&gt;&lt;br /&gt;Given the configuration above, an actual controller could look like this:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; UserController : ApiController
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IUserRepository repository;

    &lt;span style="color:Green;"&gt;// Use constructor injection here&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; UserController(IUserRepository repository)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.repository = repository;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IEnumerable&amp;lt;User&amp;gt; GetAllUsers()
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.repository.GetAll();
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Product GetUserById(&lt;span style="color:Blue;"&gt;int&lt;/span&gt; id)
    {
        &lt;span style="color:Blue;"&gt;try&lt;/span&gt;
        {
            &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.repository.GetById(id);
        }
        &lt;span style="color:Blue;"&gt;catch&lt;/span&gt; (KeyNotFoundException)
        {
            &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; HttpResponseException(HttpStatusCode.NotFound);
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Wed, 10 Apr 2013 11:41:01 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Web API Integration 20130410114101A</guid></item><item><title>New Comment on "Unity"</title><link>https://simpleinjector.codeplex.com/wikipage?title=Unity&amp;ANCHOR#C27139</link><description>See http&amp;#58;&amp;#47;&amp;#47;stackoverflow.com&amp;#47;a&amp;#47;13859582 for Batch Registration of generic interfaces in Unity.</description><author>bdeem</author><pubDate>Tue, 09 Apr 2013 14:52:57 GMT</pubDate><guid isPermaLink="false">New Comment on "Unity" 20130409025257P</guid></item><item><title>Updated Wiki: Advanced-scenarios</title><link>https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;version=54</link><description>&lt;div class="wikidoc"&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: this documentation is specific for &lt;b&gt;Simple Injector version 2.0&lt;/b&gt; and up. Look &lt;a href="http://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;amp;version=48"&gt;here&lt;/a&gt; for 1.x specific documentation.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h2&gt;Advanced scenarios with the Simple Injector&lt;/h2&gt;
Although its name might not indicate it, &lt;b&gt;Simple Injector&lt;/b&gt; is capable of handling many complex scenarios. Sometimes by writing some custom code, taking code from this wiki, or by using the extension points from the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace of the core library.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: After including the &lt;b&gt;SimpleInjector.dll&lt;/b&gt; to your project, you will have to add the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace to your code to be able to use most features that are presented in this wiki page.&lt;/i&gt;&lt;/blockquote&gt;
This page discusses the following subjects:
&lt;ul&gt;&lt;li&gt;&lt;a href="#Batch-Registration"&gt;Batch registration &amp;#47; Automatic registration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Registration-Of-Open-Generic-Types"&gt;Registration of open generic types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Unregistered-Type-Resolution"&gt;Unregistered type resolution&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Context-Based-Injection"&gt;Context based injection &amp;#47; Contextual binding&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="#Decorators"&gt;Decorators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Interception"&gt;Interception&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Property-Injection"&gt;Property injection&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Covariance-Contravariance"&gt;Covariance and Contravariance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Plugins"&gt;Registering plugins dynamically&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Batch registration / Automatic registration&lt;a name="Batch_Registration"&gt;&lt;/a&gt;&lt;a name="Batch-Registration"&gt;&lt;/a&gt;&lt;/h3&gt;Batch registration or automatic registration is a way to register all types in a given source based on some sort of convention. This saves you from adding a new line to your configuration for each new type that you register, as in the following example: &lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IUserRepository, SqlUserRepository&amp;gt;();
container.Register&amp;lt;ICustomerRepository, SqlCustomerRepository&amp;gt;();
container.Register&amp;lt;IOrderRepository, SqlOrderRepository&amp;gt;();
container.Register&amp;lt;IProductRepository, SqlProductRepository&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;To prevent having to change the container configuration too often, we can use the non-generic registration overloads, combined with a simple LINQ query:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; repositoryAssembly = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(SqlUserRepository).Assembly;

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; registrations =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; type &lt;span style="color:Blue;"&gt;in&lt;/span&gt; repositoryAssembly.GetExportedTypes()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; type.Namespace == &lt;span style="color:#A31515;"&gt;&amp;quot;MyComp.MyProd.BL.SqlRepositories&amp;quot;&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; type.GetInterfaces().Any()
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt;
    {
        Service = type.GetInterfaces().Single(),
        Implementation = type
    };

&lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; reg &lt;span style="color:Blue;"&gt;in&lt;/span&gt; registrations)
{
    container.Register(reg.Service, reg.Implementation, Lifestyle.Transient);
}
&lt;/pre&gt;&lt;/div&gt;Although many DI frameworks contain an advanced API for doing convention based registration, we found that doing this with custom LINQ queries is often easier to write, easier to understand, and even more flexible than using such an API.&lt;br /&gt;&lt;br /&gt;Another interesting scenario is that of registering implementations of some generic interface. Say for instance that your application contains the following interface:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IValidate&amp;lt;T&amp;gt;
{
    ValidationResults Validate(T instance);
}
&lt;/pre&gt;&lt;/div&gt;Your application might contain many implementations of this interface, for instance for validating customers, employees, orders, products, etc. You would normally end up with a registration as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;Customer&amp;gt;, CustomerValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Employee&amp;gt;, EmployeeValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Order&amp;gt;, OrderValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Product&amp;gt;, ProductValidator&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;Using the extension methods for batch registration of open generic types from the &lt;i&gt;SimpleInjector.Extesions&lt;/i&gt; namespace however, you can write it down to a single line:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;This call searches in the supplied assembly for all public types that implement the &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; interface and it registers each type by their specific interface (closed generic) interface. It even works for types that implement multiple closed versions of the given interface.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: There are &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_OpenGenericBatchRegistrationExtensions_RegisterManyForOpenGeneric.htm"&gt;overloads&lt;/a&gt; available that take a list of &lt;b&gt;System.Type&lt;/b&gt; instances, instead a list of &lt;b&gt;Assembly&lt;/b&gt; instances.&lt;/i&gt;&lt;/blockquote&gt;
This however is just one example of what you can do using batch registration. A more advanced scenario is the registration of multiple implementations of the same closed generic type to a single interface. Because there are many possible variations of this scenario, &lt;b&gt;Simple Injector&lt;/b&gt; does not contain any methods to do this. It does contain however, multiple overloads of the &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; that allows you to supply a callback delegate that lets you do the registration yourself. Take for instance the scenario where you have a &lt;i&gt;CustomerValidator&lt;/i&gt; and a &lt;i&gt;GoldCustomerValidator&lt;/i&gt; type, that each implement &lt;i&gt;IValidate&amp;lt;Customer&amp;gt;&lt;/i&gt;, and you want to register both of them. The previous registration would throw an exception telling you that you have multiple types implementing the same closed generic type. The following registration however, does enable this scenario:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;),
    AccessibilityOption.PublicTypesOnly,
    (serviceType, implTypes) =&amp;gt; container.RegisterAll(serviceType, implTypes),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly
);
&lt;/pre&gt;&lt;/div&gt;The previous code snippet registers all types from the given assembly that implement &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; and it registers them as a collection of instances. In other words, with the given example, &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; instances can be retrieved by calling &lt;i&gt;container.GetAllInstances&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;()&lt;/i&gt; or by using an &lt;i&gt;IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; argument in a constructor. However, using an &lt;i&gt;IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; dependency in your constructor or calling into the container directly, are generally not recommended approaches. Depending on a collection of types complicates your application design, and can often be simplified with the right configuration of your container. Your constructors should simply depend on a single &lt;i&gt;IValidator&amp;lt;T&amp;gt;&lt;/i&gt;. A better solution would therefore be to create and inject a &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; in that case:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CompositeValidator&amp;lt;T&amp;gt; : IValidate&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt; validators;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; CompositeValidator(IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt; validators)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validators = validators;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationResults Validate(T instance)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; allResults = ValidationResults.Valid;

        &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; validator &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validators)
        {
            &lt;span style="color:Blue;"&gt;var&lt;/span&gt; results = validator.Validate(instance);
            allResults = ValidationResults.Join(allResults, results);
        }

        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; allResults;
    }
}
&lt;/pre&gt;&lt;/div&gt;This &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; can be registered as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CompositeValidator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This maps the open generic &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; interface to the open generic &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; implementation. Because the &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; contains an &lt;i&gt;IEnumerable&amp;lt;IValidator&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; dependency, the registered types will be injected into its constructor. This allows you to let the rest of the application simply depend on the &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt;, while registering a collection of &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; implementations under the covers.&lt;br /&gt;&lt;br /&gt;The next section will explain mapping of open generic types, as the one we&amp;#39;ve just seen.&lt;br /&gt;
&lt;h3&gt;Registration of open generic types&lt;a name="Registration_Of_Open_Generic_Types"&gt;&lt;/a&gt;&lt;a name="Registration-Of-Open-Generic-Types"&gt;&lt;/a&gt;&lt;/h3&gt;When working with generic interfaces, we will often see many implementations of that interface being registered:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;Customer&amp;gt;, CustomerValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Employee&amp;gt;, EmployeeValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Order&amp;gt;, OrderValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Product&amp;gt;, ProductValidator&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;As the previous section explained, this can be rewritten to the following one-liner:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;Sometimes you&amp;#39;ll find that many implementations of the given generic interface are no-ops. The &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; is a good example, because it is very likely that not all entities will need validation. Still we would like to prevent from having to write an empty validation class per entity or having to write special application logic to check if an entity has validation. In this situation, we will probably want to use the registration as described in the previous section, and fallback to returning the default implementation when no such registration exists for a given type. Such default implementation could look like this, for instance:&lt;br /&gt; &lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Implementation of the Null Object pattern.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;class&lt;/span&gt; NullValidator&amp;lt;T&amp;gt; : IValidate&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationResults Validate(T instance)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; ValidationResults.Valid;
    }
}
&lt;/pre&gt;&lt;/div&gt;We can use this &lt;i&gt;NullValidator&amp;lt;T&amp;gt;&lt;/i&gt; for all entities that don&amp;#39;t need validation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;OrderLine&amp;gt;, NullValidator&amp;lt;OrderLine&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Address&amp;gt;, NullValidator&amp;lt;Address&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;UploadImage&amp;gt;, NullValidator&amp;lt;UploadImage&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Mothership&amp;gt;, NullValidator&amp;lt;Mothership&amp;gt;&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;This is of course not very practical. Falling back to such a default implementation is a typical scenario for &lt;i&gt;unregistered type resolution&lt;/i&gt;. The &lt;b&gt;Simple Injector&lt;/b&gt; contains an event that you can hook to that allows to fallback to a default implementation. On top of this event, the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Methods_T_SimpleInjector_Extensions_OpenGenericRegistrationExtensions.htm"&gt;RegisterOpenGeneric&lt;/a&gt; extension method available. The given situation would be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(NullValidator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;The result of this registration is that for instance an &lt;i&gt;NullValidator&amp;lt;Department&amp;gt;&lt;/i&gt; instance is returned when an &lt;i&gt;IValidate&amp;lt;Department&amp;gt;&lt;/i&gt; is requested.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Because the use of unregistered type resolution, a NullValidator&amp;lt;T&amp;gt; will only get returned for types that are not registered, therefore allowing the default implementation to be overridden for a specific implementation. The &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; method, for instance, doesn’t use unregistered type resolution, but simply registers all concrete types it finds in the given assemblies. Those types will therefore always be returned, giving a very convenient and easy to grasp mix.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Unregistered type resolution&lt;a name="Unregistered_Type_Resolution"&gt;&lt;/a&gt;&lt;a name="Unregistered-Type-Resolution"&gt;&lt;/a&gt;&lt;/h3&gt;Unregistered type resolution is the ability to get notified by the container when a type is requested that is currently unregistered in the container. This gives the user the change of registering that type. The &lt;b&gt;Simple Injector&lt;/b&gt; supports this scenario with the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/E_SimpleInjector_Container_ResolveUnregisteredType.htm"&gt;ResolveUnregisteredType&lt;/a&gt; event. Unregistered type resolution enables many advanced scenarios. The framework itself uses this event for implementing the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Registration_Of_Open_Generic_Types"&gt;registration of open generic types&lt;/a&gt;. Other examples of possible scenarios that can be built on top of this event are &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolve-Arrays-And-Lists"&gt;resolving array and lists&lt;/a&gt; and &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Covariance-Contravariance"&gt;covariance and contravariance&lt;/a&gt;. Those scenarios are described here in the advanced scenarios page.&lt;br /&gt;&lt;br /&gt;For more information about how to use this event, please look at the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/E_SimpleInjector_Container_ResolveUnregisteredType.htm"&gt;ResolveUnregisteredType event documentation&lt;/a&gt; in the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/"&gt;reference library&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;Context based injection&lt;a name="Context_Based_Injection"&gt;&lt;/a&gt;&lt;a name="Contextual_Binding"&gt;&lt;/a&gt;&lt;a name="Context-Based-Injection"&gt;&lt;/a&gt;&lt;/h3&gt;Context based injection is the ability to inject a dependency based on the context it lives in. This context is often supplied by the container. Some DI frameworks contain a feature that allows this, while others don’t. The &lt;b&gt;Simple Injector&lt;/b&gt; does &lt;i&gt;not&lt;/i&gt; does not contain such a feature out of the box, but this ability can easily be added by using the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ContextDependentExtensions&amp;referringTitle=Advanced-scenarios"&gt;context based injection extension method&lt;/a&gt; code snippet.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: In many cases context based injection is not the best solution, and the design should be reevaluated. In some narrow cases however it can make sense.&lt;/i&gt;&lt;/blockquote&gt;
The most common scenario is to base the type of the injected dependency on the type of the consumer. Take for instance the following &lt;i&gt;ILogger&lt;/i&gt; interface with a generic &lt;i&gt;Logger&amp;lt;T&amp;gt;&lt;/i&gt; class that needs to be injected into several consumers. &lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; ILogger
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message);
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Logger&amp;lt;T&amp;gt; : ILogger
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message) { }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Consumer1
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Consumer1(ILogger logger) { }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Consumer2
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Consumer2(ILogger logger) { }
}
&lt;/pre&gt;&lt;/div&gt;In this case we want to inject a &lt;i&gt;Logger&amp;lt;Consumer1&amp;gt;&lt;/i&gt; into &lt;i&gt;Consumer1&lt;/i&gt; and a &lt;i&gt;Logger&amp;lt;Consumer2&amp;gt;&lt;/i&gt; into &lt;i&gt;Consumer2&lt;/i&gt;. By using the previous &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ContextDependentExtensions&amp;referringTitle=Advanced-scenarios"&gt;extension method&lt;/a&gt;, we can accomplish this as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterWithContext&amp;lt;ILogger&amp;gt;(dependencyContext =&amp;gt;
{
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; type = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Logger&amp;lt;&amp;gt;).MakeGenericType(
        dependencyContext.ImplementationType);
    
    &lt;span style="color:Blue;"&gt;return&lt;/span&gt; (ILogger)container.GetInstance(type);
});
&lt;/pre&gt;&lt;/div&gt;In the previous code snippet we registered a &lt;i&gt;Func&amp;lt;DependencyContext, ILogger&amp;gt;&lt;/i&gt; delegate, that will get called each time a &lt;i&gt;ILogger&lt;/i&gt; dependency gets resolved. The &lt;i&gt;DependencyContext&lt;/i&gt; instance that gets supplied to that instance, contains the &lt;i&gt;ServiceType&lt;/i&gt; and &lt;i&gt;ImplementationType&lt;/i&gt; into which the &lt;i&gt;ILogger&lt;/i&gt; is getting injected.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Although building a generic type using MakeGenericType is relatively slow, the call to the Func&amp;lt;DependencyContext, TService&amp;gt; delegate itself is about as cheap as calling a Func&amp;lt;TService&amp;gt; delegate. If performance of the MakeGenericType gets a problem, you can always cache the generated types, cache InstanceProducer instances, or cache ILogger instances (note that caching the ILogger instances will make them singletons).&lt;/i&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Even though the use of a generic Logger&amp;lt;T&amp;gt; is a common design (with log4net as the grand godfather of this design), doesn&amp;#39;t always make it a good design. The need for having the logger contain information about its parent type, might indicate design problems. If you&amp;#39;re doing this, please take a look at &lt;a href="http://stackoverflow.com/a/9915056/264697"&gt;this Stackoverflow answer&lt;/a&gt;. It talks about logging in conjunction with the SOLID design principles.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Decorators&lt;a name="Decorators"&gt;&lt;/a&gt;&lt;a name="Generic_Decorators"&gt;&lt;/a&gt;&lt;/h3&gt;The &lt;a href="http://en.wikipedia.org/wiki/SOLID"&gt;SOLID&lt;/a&gt; principles give us important guidance when it comes to writing maintainable software. The &amp;#39;O&amp;#39; of the &amp;#39;SOLID&amp;#39; acronym stands for the &lt;a href="http://en.wikipedia.org/wiki/Open/closed_principle"&gt;Open/closed Principle&lt;/a&gt; which states that classes should be open for extension, but closed for modification. Designing systems around the Open/closed principle means that new behavior can be plugged into the system, without the need to change any existing parts, making the change of breaking existing code much smaller.&lt;br /&gt;&lt;br /&gt;One of the ways to add new functionality (such as &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=http%3a%2f%2fen.wikipedia.org%2fwiki%2fCross-cutting_concern&amp;referringTitle=Advanced-scenarios"&gt;cross-cutting concerns&lt;/a&gt;) to types is by the use of the &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;decorator pattern&lt;/a&gt;. The decorator pattern can be used to extend (decorate) the functionality of a certain object at run-time. Especially when using generic interfaces, the concept of decorators gets really powerful. Take for instance the examples given in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Registration_Of_Open_Generic_Types"&gt;Registration of open generic types&lt;/a&gt; section of this page or for instance the use of an generic &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; interface.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: &lt;a href="http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91"&gt;This article&lt;/a&gt; describes an architecture based on the use of the ICommandHandler&amp;lt;TCommand&amp;gt; interface.&lt;/i&gt;&lt;/blockquote&gt;
Take the plausible scenario where we want to validate all commands that get executed by an &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; implementation. The Open/Closed principle states that we want to do this, without having to alter each and every implementation. We can do this using a (single) decorator:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;
    : ICommandHandler&amp;lt;TCommand&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IValidator validator;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ICommandHandler&amp;lt;TCommand&amp;gt; handler;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationCommandHandlerDecorator(
        IValidator validator, 
        ICommandHandler&amp;lt;TCommand&amp;gt; handler)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validator = validator;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handler = handler;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; ICommandHandler&amp;lt;TCommand&amp;gt;.Handle(TCommand command)
    {
        &lt;span style="color:Green;"&gt;// validate the supplied command (throws when invalid).&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validator.ValidateObject(command);
		
        &lt;span style="color:Green;"&gt;// forward the (valid) command to the real&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// command handler.&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handler.Handle(command);
    }
}
&lt;/pre&gt;&lt;/div&gt;The &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; class is an implementation of the &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; interface, but it also wraps / decorates an &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; instance. Instead of injecting the real implementation directly into a consumer, we can (let Simple Injector) inject a validator decorator that wraps the real implementation.&lt;br /&gt;&lt;br /&gt;The &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; depends on an &lt;i&gt;IValidator&lt;/i&gt; interface. An implementation that used Microsoft Data Annotations might look like this:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.ComponentModel.DataAnnotations;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; DataAnnotationsValidator : IValidator
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IServiceProvider container;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; DataAnnotationsValidator(Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }
    
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IValidator.ValidateObject(&lt;span style="color:Blue;"&gt;object&lt;/span&gt; instance)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; context = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ValidationContext(instance,
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container, &lt;span style="color:Blue;"&gt;null&lt;/span&gt;);

        &lt;span style="color:Green;"&gt;// Throws an exception when instance is invalid.&lt;/span&gt;
        Validator.ValidateObject(instance, context);    
    }
}
&lt;/pre&gt;&lt;/div&gt;The implementations of the &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt; interface can be registered using the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_OpenGenericBatchRegistrationExtensions_RegisterManyForOpenGeneric.htm"&gt;RegisterManyForOpenGeneric&lt;/a&gt; extension method:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;By using the following extension method from the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace, you can wrap the &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; around each and every &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; implementation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;Multiple decorators can be wrapped by calling the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterDecorator.htm"&gt;RegisterDecorator&lt;/a&gt; method multiple times, as the following registration shows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TransactionCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DeadlockRetryCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;The decorators are applied in the order in which they are registered, which means that the first decorator (&lt;i&gt;TransactionCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; in this case) wraps the real instance, the second decorator (&lt;i&gt;DeadlockRetryCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; in this case) wraps the first decorator, and so on.&lt;br /&gt;&lt;br /&gt;There&amp;#39;s an overload of the &lt;b&gt;RegisterDecorator&lt;/b&gt; available that allows you to supply a predicate to determine whether that decorator should be applied to a specific service type. Using a given context you can determine whether the decorator should be applied. Here is an example:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AccessValidationCommandHandlerDecorator&amp;lt;&amp;gt;),
    context =&amp;gt; !context.ImplementationType.Namespace.EndsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Admins&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;The given context contains several properties that allows you to analyze whether a decorator should be applied to a given service type, such as the current closed generic service type (using the &lt;i&gt;ServiceType&lt;/i&gt; property) and the concrete type that will be created (using the &lt;i&gt;ImplementationType&lt;/i&gt; property). The predicate will (under normal circumstances) be called only once per generic type, so there is no performance implication for using it.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Runtime-Decorators&amp;referringTitle=Advanced-scenarios"&gt;This extension method&lt;/a&gt; allows registering decorators that can be applied based on runtime conditions (such as the role of the current user).&lt;/i&gt;&lt;/blockquote&gt;
&lt;h4&gt;Decorators with Func&amp;lt;T&amp;gt; factories&lt;a name="Decorators_With_Func_Factories"&gt;&lt;/a&gt;&lt;/h4&gt;In certain scenarios, it is needed to postpone building part of the object graph. For instance when a service needs to control the lifetime of a dependency, need multiple instances, when instances need to be &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Multi_Threaded_Applications"&gt;executed on a different thread&lt;/a&gt;, or when instances need to be created in a certain &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Scoped"&gt;scope&lt;/a&gt; or (security) context.&lt;br /&gt;&lt;br /&gt;When building a &amp;#39;normal&amp;#39; object graph with dependencies, you can easily delay building a part of the graph by letting a service depend on a factory. This allows building that part of the object graph to be postponed until the time the type starts using the factory. When working with decorators however, injecting a factory to postpone the creation of the decorated instance will not work. Take for instance a &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; that allows executing a command handler on a different thread. We could let the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; depend on a &lt;i&gt;CommandHandlerFactory&amp;lt;T&amp;gt;&lt;/i&gt;, and let this factory call back into the container to retrieve a new &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt;. Unfortunately this would fail, since requesting an &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt; would again wrap this instance with a new &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt;, and we&amp;#39;d end up causing a stack overflow.&lt;br /&gt;&lt;br /&gt;Since this is a scenario that is really hard to solve without framework support, &lt;b&gt;Simple Injector&lt;/b&gt; allows injecting a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate into registered decorators. This delegate functions as a factory for the creation of the decorated instance. Taking the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; as example, it could be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;
    : ICommandHandler&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;ICommandHandler&amp;lt;T&amp;gt;&amp;gt; factory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AsyncCommandHandlerDecorator(
        Func&amp;lt;ICommandHandler&amp;lt;T&amp;gt;&amp;gt; factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
	
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(T command)
    {
        &lt;span style="color:Green;"&gt;// Execute on different thread.&lt;/span&gt;
        ThreadPool.QueueUserWorkItem(_ =&amp;gt;
        {
            &lt;span style="color:Green;"&gt;// Create new handler in this thread.&lt;/span&gt;
            &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory();
            handler.Handle(command)
        });
    }
}
&lt;/pre&gt;&lt;/div&gt;This special decorator can be registered just as any other decorator:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;However, since the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; solely has singleton dependencies (the &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; is a singleton), and creates a new decorated instance each time it’s called, we can even register it as a singleton itself:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;When mixing this with other (synchronous) decorators, you&amp;#39;ll get an extremely powerful and pluggable system:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TransactionCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DeadlockRetryCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterSingleDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This configuration has an interesting mix of decorator registrations. The registration of the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; allows (some of) the command handlers to be executed on the background (while others -who&amp;#39;s name does not start with &amp;#39;Async&amp;#39;- still run synchronously), but before execution, all commands are validated synchronously (to allow communicating validation errors to the caller). And all handlers (even the asynchronous ones) are executed in a transaction and the operation is retried when the database rolled back because of a deadlock).&lt;br /&gt;
&lt;h4&gt;Decorated collections&lt;a name="Decorated_Collections"&gt;&lt;/a&gt;&lt;/h4&gt;When registering a decorator, Simple Injector will automatically decorate any collection with elements of that service type:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterAll&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt;(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CustomerMovedEventHandler),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(NotifyStaffWhenCustomerMovedEventHandler));
    
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationEventHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; SomeCondition);
&lt;/pre&gt;&lt;/div&gt;The previous registration registers a collection of &lt;i&gt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&lt;/i&gt; services. Those services are decorated with a &lt;i&gt;ValidationEventHandlerDecorator&amp;lt;TEvent&amp;gt;&lt;/i&gt; when the supplied predicate holds.&lt;br /&gt;&lt;br /&gt;For collections of elements that are created by the container (container controlled), the predicate is checked for each element in the collection. For collections of uncontrolled elements (a list of items that is not created by the container), the predicate is checked once for the whole collection. This means that controlled collections can be partially decorated. Taking the previous example for instance, you could let the &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; be decorated, while leaving the &lt;i&gt;NotifyStaffWhenCustomerMovedEventHandler&lt;/i&gt; undecorated (determined by the supplied predicate).&lt;br /&gt;&lt;br /&gt;When a collection is uncontrolled, it means that the lifetime of its elements are unknown to the container. The following registration is an example of an uncontrolled collection:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
IEnumerable&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt; handlers =
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;[]
    {
        &lt;span style="color:Blue;"&gt;new&lt;/span&gt; CustomerMovedEventHandler(),
        &lt;span style="color:Blue;"&gt;new&lt;/span&gt; NotifyStaffWhenCustomerMovedEventHandler(),
    };

container.RegisterAll&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt;(handlers);
&lt;/pre&gt;&lt;/div&gt;Although this registration contains a list of singletons, the container has no way of knowing this. The collection could easily have been a dynamic (an ever changing) collection. In this case, the container calls the registered predicate once (and supplies the predicate with the &lt;i&gt;IEventHandler&amp;lt;CusotmerMovedEvent&amp;gt;&lt;/i&gt; type)  and if the predicate returns true, each element in the collection is decorated with a decorator instance.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Warning&lt;/b&gt;: In general you should prevent registering uncontrolled collections. The container knows nothing about them, and can&amp;#39;t help you in doing &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;diagnostics&lt;/a&gt;. Since the lifetime of those items is unknown, the container will be unable to wrap a decorator with a lifestyle other than transient. Best practice is to register container-controlled collections which is done by using one of the &lt;b&gt;RegisterAll&lt;/b&gt; overloads that take a collection of &lt;b&gt;System.Type&lt;/b&gt; instances.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Interception&lt;a name="Interception"&gt;&lt;/a&gt;&lt;/h3&gt;Interception is the ability to intercept a call from a consumer to a service, and add or change behavior. The &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;decorator pattern&lt;/a&gt; describes a form of interception, but when it comes to applying cross-cutting concerns, you might end up writing decorators for many service interfaces, but with the exact same code. If this is happening, it is time to explore the possibilities of interception.&lt;br /&gt;&lt;br /&gt;Using the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets, you can add the ability to do interception with the &lt;b&gt;Simple Injector&lt;/b&gt;. Using the given code, you can for instance define a &lt;i&gt;MonitoringInterceptor&lt;/i&gt; that allows logging the execution time of the called service method:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MonitoringInterceptor : IInterceptor
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;

    &lt;span style="color:Green;"&gt;// Using constructor injection on the interceptor&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MonitoringInterceptor(ILogger logger)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = logger;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Intercept(IInvocation invocation)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; watch = Stopwatch.StartNew();

        &lt;span style="color:Green;"&gt;// Calls the decorated instance.&lt;/span&gt;
        invocation.Proceed();

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; decoratedType =
            invocation.InvocationTarget.GetType();
        
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger.Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt;.Format(
            &lt;span style="color:#A31515;"&gt;&amp;quot;{0} executed in {1} ms.&amp;quot;&lt;/span&gt;,
            decoratedType.Name,
            watch.ElapsedMiliseconds));
    }
}
&lt;/pre&gt;&lt;/div&gt;This interceptor can be registered to be wrapped around a concrete implementation. Using the given extension methods, this can be done as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.InterceptWith&amp;lt;MonitoringInterceptor&amp;gt;(type =&amp;gt; type == &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IUserRepository));
&lt;/pre&gt;&lt;/div&gt;This registration ensures that every time an &lt;i&gt;IUserRepository&lt;/i&gt; interface is requested, an interception proxy is returned that wraps that instance and uses the &lt;i&gt;MonitoringInterceptor&lt;/i&gt; to extend the behavior.&lt;br /&gt;&lt;br /&gt;The current example doesn&amp;#39;t add much compared to simply using a decorator. When having many interface service types that need to be decorated with the same behavior however, it gets different:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.InterceptWith&amp;lt;MonitoringInterceptor&amp;gt;(t =&amp;gt; t.Name.EndsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Repository&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets use .NET&amp;#39;s &lt;/i&gt;System.Runtime.Remoting.Proxies.RealProxy&lt;i&gt; class to generate interception proxies. The &lt;/i&gt;RealProxy&lt;i&gt; only allows to proxy interfaces.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: the interfaces in the given &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets are a simplified version of the Castle Project interception facility. If you need to create lots different interceptors, you might benefit from using the interception abilities of the Castle Project. Also please note that the given snippets use dynamic proxies to do the interception, while Castle uses lightweight code generation (LCG). LCG allows much better performance than the use of dynamic proxies.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Don&amp;#39;t use interception for intercepting types that all implement the same generic interface, such as ICommandHandler&amp;lt;T&amp;gt; or IValidator&amp;lt;T&amp;gt;. Try using decorator classes instead, as shown in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Decorators"&gt;Decorators&lt;/a&gt; section on this page.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Property injection&lt;a name="Implicit_Property_Injection"&gt;&lt;/a&gt;&lt;a name="Implicit-Property-Injection"&gt;&lt;/a&gt;&lt;a name="Property-Injection"&gt;&lt;/a&gt;&lt;/h3&gt;Simple Injector does not inject any properties into types that get resolved by the container. In general there are two ways of doing property injection, and both are not supported by default for reasons explained below.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Implicit property injection&lt;/b&gt;&lt;br /&gt;Some containers (such as Castle Windsor) implicitly inject public writable properties by default for any instance you resolve. They do this by mapping those properties to configured types. When no such registration exists, or when the property doesn’t have a public setter, the property will be skipped. Simple Injector does not do this, and for good reason. We think that &lt;i&gt;implicit property injection&lt;/i&gt; is simply too uuhh...  implicit :-). Silently skipping properties that can&amp;#39;t be mapped can lead to a DI configuration that can&amp;#39;t be verified easily and can therefore result in an application that fails at runtime instead of failing when the container is verified.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Explicit property injection&lt;/b&gt;&lt;br /&gt;We strongly feel that explicit property injection is a much better way to go. With explicit property injection the container is forced to inject a property and the process will fail immediately when a property can&amp;#39;t be mapped or injected. Some containers (such as Unity and Ninject) allow explicit property injection by default by allowing properties to be decorated with attributes. Problem with this is that this forces the application to take a dependency on the framework, which is something that should be prevented.&lt;br /&gt;&lt;br /&gt;Because Simple Injector does not encourage its users to take a dependency on the container (except for the startup path of course), Simple Injector does not contain any attributes that allow explicit property injection and it can therefore not explicitly inject properties out-of-the-box.&lt;br /&gt;&lt;br /&gt;Besides this, the use of property injection should be very exceptional and in general constructor injection should always be used. If a constructor gets too many parameters (constructor over-injection anti-pattern), it is an indication of a violation of the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; (SRP). SRP violations often lead to maintainability issues. So instead of patching constructor over-injection with property injection, the root cause should be analyzed and the type should be refactored, probably with &lt;a href="http://blog.ploeh.dk/2010/02/02/RefactoringtoAggregateServices/"&gt;Facade Services&lt;/a&gt;. Another common reason to use properties is because those dependencies are optional. Instead of using optional property dependencies, best practice is to inject empty implementations (a.k.a. &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object pattern&lt;/a&gt;) into the constructor.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Enabling property injection&lt;/b&gt;&lt;br /&gt;Simple Injector contains two ways to enable property injection. First of all the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Configuring_Property_Injection"&gt;RegisterInitializer&amp;#60;T&amp;#62;&lt;/a&gt; method can be used to inject properties (especially configuration values) on a per-type basis. Take for instance the following code snippet:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterInitializer&amp;lt;HandlerBase&amp;gt;(handlerToInitialize =&amp;gt;
{
    handlerToInitialize.ExecuteAsynchronously = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;
});
&lt;/pre&gt;&lt;/div&gt;In the previous example a &lt;i&gt;Action&amp;lt;T&amp;gt;&lt;/i&gt; delegate is registered that will be called every time the container creates a type that inherits from &lt;i&gt;HandlerBase&lt;/i&gt;. In this case, the handler will set a configuration value on that class.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: although this method can also be used injecting services, please note that the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;Diagnostic Services&lt;/a&gt; will be unable to see and analyze this dependency.&lt;/i&gt;&lt;/blockquote&gt;
The second way to inject properties is by implementing a custom &lt;b&gt;IPropertySelectionBehavior&lt;/b&gt;. The &lt;i&gt;property selection behavior&lt;/i&gt; is a general extension point provided by the container, to override the library&amp;#39;s default behavior. The following code for instance enables explicit property injection using attributes, using any customly defined attribute:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Reflection;

&lt;span style="color:Blue;"&gt;using&lt;/span&gt; SimpleInjector.Advanced;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AttributedPropertyInjectionExtensions
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; AutowirePropertiesWithAttribute&amp;lt;TAttribute&amp;gt;(
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt; ContainerOptions options)
        &lt;span style="color:Blue;"&gt;where&lt;/span&gt; TAttribute : Attribute
    {
        options.PropertySelectionBehavior =
            &lt;span style="color:Blue;"&gt;new&lt;/span&gt; AttributePropertyInjectionBehavior(
                options.PropertySelectionBehavior, 
                &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TAttribute));
    }

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AttributePropertyInjectionBehavior 
        : IPropertySelectionBehavior
    {
        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IPropertySelectionBehavior behavior;
        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Type attribute;

        &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AttributePropertyInjectionBehavior(
            IPropertySelectionBehavior baseBehavior, 
            Type attributeType)
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.behavior = baseBehavior;
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.attribute = attributeType;
        }

        &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;bool&lt;/span&gt; SelectProperty(Type type, PropertyInfo prop)
        {
            &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.IsPropertyDecorated(prop) ||
                &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.behavior.SelectProperty(type, prop);
        }

        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;bool&lt;/span&gt; IsPropertyDecorated(PropertyInfo p)
        {
            &lt;span style="color:Blue;"&gt;return&lt;/span&gt; p.GetCustomAttributes(&lt;span style="color:Blue;"&gt;this&lt;/span&gt;.attribute, &lt;span style="color:Blue;"&gt;true&lt;/span&gt;).Any();
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;The previous code snippet can be used as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();
container.Options.AutowirePropertiesWithAttribute&amp;lt;MyInjectAttribute&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;This enables explicit property injection on all properties that are marked with the &lt;b&gt;MyInjectAttribute&lt;/b&gt; and it will fail when the property cannot be injected for whatever reason.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: Properties injected by the container through the &lt;b&gt;IPropertySelectionBehavior&lt;/b&gt; will be analyzed by the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;Diagnostic Services&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The &lt;b&gt;IPropertySelectionBehavior&lt;/b&gt; extension mechanism can also be used to implement implicit property injection. Doing so however is not advised because of the reasons given above.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Covariance and Contravariance&lt;a name="Covariance_Contravariance"&gt;&lt;/a&gt;&lt;a name="Covariance-Contravariance"&gt;&lt;/a&gt;&lt;/h3&gt;Since version 4.0 of the .NET framework, the type system allows &lt;a href="http://msdn.microsoft.com/en-us/library/dd799517.aspx"&gt;Covariance and Contravariance in Generics&lt;/a&gt; (especially interfaces and delegates). This allows for instance, to use a &lt;i&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/i&gt; as an &lt;i&gt;IEnumerable&amp;lt;object&amp;gt;&lt;/i&gt; (covariance), or to use an &lt;i&gt;Action&amp;lt;object&amp;gt;&lt;/i&gt; as an &lt;i&gt;Action&amp;lt;string&amp;gt;&lt;/i&gt; (contravariance).&lt;br /&gt;&lt;br /&gt;In some circumstances, the application design can benefit from the use of covariance and contravariance (or variance for short) and it would be beneficial when the IoC container returns services that are &amp;#39;compatible&amp;#39; to the requested service, even although the requested service is not registered. To stick with the previous example, the container could return an &lt;i&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/i&gt; even when an &lt;i&gt;IEnumerable&amp;lt;object&amp;gt;&lt;/i&gt; is requested.&lt;br /&gt;&lt;br /&gt;By default, the Simple Injector does not return variant implementations of given services, but the Simple Injector can be extended to behave this way. The actual way to write this extension depends on the requirements of the application.&lt;br /&gt;&lt;br /&gt;Take a look at the following application design around the &lt;i&gt;IEventHandler&amp;lt;in TEvent&amp;gt;&lt;/i&gt; interface:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IEventHandler&amp;lt;&lt;span style="color:Blue;"&gt;in&lt;/span&gt; TEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(TEvent e);
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedEvent 
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;int&lt;/span&gt; CustomerId { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Address NewAddress { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedAbroadEvent : CustomerMovedEvent
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Country Country { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedEventHandler : IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(CustomerMovedEvent e) { ... }
}
&lt;/pre&gt;&lt;/div&gt;The design contains two event classes &lt;i&gt;CustomerMovedEvent&lt;/i&gt; and &lt;i&gt;CustomerMovedAbroadEvent&lt;/i&gt; (where &lt;i&gt;CustomerMovedAbroadEvent&lt;/i&gt; inherits from &lt;i&gt;CustomerMovedEvent&lt;/i&gt;) one concrete event handler &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; and a generic interface for event handlers.&lt;br /&gt;&lt;br /&gt;We can configure the container in such way that not only a request for &lt;i&gt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&lt;/i&gt; results in a &lt;i&gt;CustomerMovedEventHandler,&lt;/i&gt; but also a request for &lt;i&gt;IEventHandler&amp;lt;CustomerMovedAbroadEvent&amp;gt;&lt;/i&gt; results in that same &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; (because &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; also accepts &lt;i&gt;CustomerMovedAbroadEvents&lt;/i&gt;).&lt;br /&gt;&lt;br /&gt;There are several ways to achieve this, but one of the ways is the following:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;CustomerMovedEventHandler&amp;gt;();

container.RegisterSingleOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ContravarianceEventHandler&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This registration depends on the custom &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; that should be placed close to the registration itself:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ContravarianceEventHandler&amp;lt;TEvent&amp;gt;
    : IEventHandler&amp;lt;TEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; Func&amp;lt;IEventHandler&amp;lt;TEvent&amp;gt;&amp;gt; handlerFactory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ContravarianceEventHandler(Container container)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handlerType = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;TEvent&amp;gt;);

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; registration = (
            &lt;span style="color:Blue;"&gt;from&lt;/span&gt; r &lt;span style="color:Blue;"&gt;in&lt;/span&gt; container.GetCurrentRegistrations()
            &lt;span style="color:Blue;"&gt;let&lt;/span&gt; assignableInterfaces = (
                &lt;span style="color:Blue;"&gt;from&lt;/span&gt; iface &lt;span style="color:Blue;"&gt;in&lt;/span&gt; r.ServiceType.GetInterfaces()
                &lt;span style="color:Blue;"&gt;where&lt;/span&gt; handlerType.IsAssignableFrom(iface)
                &lt;span style="color:Blue;"&gt;select&lt;/span&gt; iface)
            &lt;span style="color:Blue;"&gt;where&lt;/span&gt; assignableInterfaces.Any()
            &lt;span style="color:Blue;"&gt;select&lt;/span&gt; r)
            .Single();

        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handlerFactory =
            () =&amp;gt; (IEventHandler&amp;lt;TEvent&amp;gt;)r.GetInstance();
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IEventHandler&amp;lt;TEvent&amp;gt;.Handle(TEvent e)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handlerFactory().Handle(e);
    }
}
&lt;/pre&gt;&lt;/div&gt;The registration ensures that every time an &lt;i&gt;IEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; is requested, a &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; is returned. The &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; will on creation query the container for a single service type that implements &lt;i&gt;IEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt;. Because the &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; is not registered by its interface, but by itself, the &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; will find that type.&lt;br /&gt;&lt;br /&gt;This is just one example and one way of adding variance support. For a more elaborate discussion on this subject, please read the following article: &lt;a href="http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=90"&gt;Adding Covariance and Contravariance to the Simple Injector&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;Registering plugins dynamically&lt;a name="Plugins"&gt;&lt;/a&gt;&lt;/h3&gt;Applications with a plugin architecture often allow special plugin assemblies to be dropped in a special folder and to be picked up by the application, without the need of a recompile. Although Simple Injector does not support this out of the box, registering plugins from dynamically loaded assemblies can be implemented in a few lines of code. Here is an example:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;string&lt;/span&gt; pluginDirectory =
    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, &lt;span style="color:#A31515;"&gt;&amp;quot;Plugins&amp;quot;&lt;/span&gt;);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; pluginAssemblies =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; file &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; DirectoryInfo(pluginDirectory).GetFiles()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; file.Extension == &lt;span style="color:#A31515;"&gt;&amp;quot;.dll&amp;quot;&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; Assembly.LoadFile(file.FullName);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; pluginTypes =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; dll &lt;span style="color:Blue;"&gt;in&lt;/span&gt; pluginAssemblies
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; type &lt;span style="color:Blue;"&gt;in&lt;/span&gt; dll.GetExportedTypes()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IPlugin).IsAssignableFrom(type)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; !type.IsAbstract
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; !type.IsGenericTypeDefinition
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; type;

container.RegisterAll&amp;lt;IPlugin&amp;gt;(pluginTypes);
&lt;/pre&gt;&lt;/div&gt;The given example makes use of an &lt;i&gt;IPlugin&lt;/i&gt; interface that is known to the application, and probably located in a shared assembly. The dynamically loaded plugin .dll files can contain multiple classes that implement &lt;i&gt;IPlugin&lt;/i&gt;, and all publicly exposed concrete types that implements &lt;i&gt;IPlugin&lt;/i&gt; will be registered using the &lt;i&gt;RegisterAll&lt;/i&gt; method and can get resolved using the default auto-wiring behavior of the container, meaning that the plugin must have a single public constructor and all constructor arguments must be resolvable by the container. The plugins can get resolved using &lt;i&gt;container.GetAllInstances&amp;lt;IPlugin&amp;gt;()&lt;/i&gt; or by adding an &lt;i&gt;IEnumerable&amp;lt;IPlugin&amp;gt;&lt;/i&gt; argument to a constructor.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Resolving instances by key&lt;a name="Resolving_Instances_By_Key"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolve_Instances_By_Key"&gt;Resolve Instances By Key&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios"&gt;How to&lt;/a&gt; documentation page, for the information you are looking for.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Resolving array and lists&lt;a name="Resolving_Arrays_And_Lists"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolve_Arrays_And_Lists"&gt;Resolve Arrays and Lists&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios"&gt;How to&lt;/a&gt; page, for the information you are looking for.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Lifetime scoping&lt;a name="Lifetime_scoping"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#PerLifetimeScope"&gt;Per Lifetime Scope&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios"&gt;Object Lifestyle Management&lt;/a&gt; documentation page for more information.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Fri, 05 Apr 2013 15:33:20 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Advanced-scenarios 20130405033320P</guid></item><item><title>Updated Wiki: Runtime-Decorators</title><link>https://simpleinjector.codeplex.com/wikipage?title=Runtime-Decorators&amp;version=1</link><description>&lt;div class="wikidoc"&gt;&lt;h2&gt;Applying decorators at runtime using Simple Injector&lt;/h2&gt;The &lt;b&gt;RegisterDecorator&lt;/b&gt; extension methods contain overloads that allow you to apply a predicate (delegate) that allows you to conditionally apply a decorator.&lt;br /&gt;&lt;br /&gt;This predicate is meant to conditionally apply a decorator based on constant information. This can be compile time information such as type names, namespaces, configuration values etc. Because of this this predicate only be called once (or at most a few times) per closed generic type. Whether or not the decorator should be applied is after that point compiled in the delegate.&lt;br /&gt;&lt;br /&gt;Sometimes however, decorators must be applied based on some runtime conditions. Take for instance a authorization decorator that must conditionally be applied based on the role of the current user.&lt;br /&gt;&lt;br /&gt;The following example shows an extension method that allows registering a decorator using a runtime predicate:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Linq.Expressions;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Threading;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; SimpleInjector.Extensions;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RuntimeDecoratorExtensions
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; RegisterRuntimeDecorator(
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container, Type serviceType, Type decoratorType,
        Func&amp;lt;&lt;span style="color:Blue;"&gt;bool&lt;/span&gt;&amp;gt; runtimePredicate)
    {
        container.RegisterRuntimeDecorator&amp;lt;Container&amp;gt;(
            serviceType, decoratorType,
            Lifestyle.Transient,
            context =&amp;gt; &lt;span style="color:Blue;"&gt;true&lt;/span&gt;,
            context =&amp;gt; runtimePredicate());
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; RegisterRuntimeDecorator&amp;lt;TContextInfo&amp;gt;(
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container, Type serviceType, Type decoratorType,
        Lifestyle lifestyle,
        Predicate&amp;lt;DecoratorPredicateContext&amp;gt; compileTimePredicate,
        Predicate&amp;lt;TContextInfo&amp;gt; runtimePredicate)
        &lt;span style="color:Blue;"&gt;where&lt;/span&gt; TContextInfo : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; localContext =
                &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ThreadLocal&amp;lt;DecoratorPredicateContext&amp;gt;();

        container.RegisterDecorator(serviceType, decoratorType, lifestyle, c =&amp;gt;
        {
            &lt;span style="color:Blue;"&gt;bool&lt;/span&gt; mustDecorate = compileTimePredicate(c);
            localContext.Value = mustDecorate ? c : &lt;span style="color:Blue;"&gt;null&lt;/span&gt;;
            &lt;span style="color:Blue;"&gt;return&lt;/span&gt; mustDecorate;
        });

        container.ExpressionBuilt += (s, e) =&amp;gt;
        {
            &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (localContext.Value != &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
            {
                Expression decoratee = localContext.Value.Expression;
                Expression decorated = e.Expression;

                localContext.Value = &lt;span style="color:Blue;"&gt;null&lt;/span&gt;;

                &lt;span style="color:Blue;"&gt;var&lt;/span&gt; contextInfoRegistration =
                    container.GetRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TContextInfo), &lt;span style="color:Blue;"&gt;true&lt;/span&gt;);

                Expression shouldDecorate = Expression.Invoke(
                    Expression.Constant(runtimePredicate),
                    contextInfoRegistration.BuildExpression());

                e.Expression = Expression.Condition(shouldDecorate,
                    Expression.Convert(decorated, e.RegisteredServiceType),
                    Expression.Convert(decoratee, e.RegisteredServiceType));
            }
        };
    }
}
&lt;/pre&gt;&lt;/div&gt;The following line shows an example of how to use this extension method:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterRuntimeDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AuthorizationHandlerDecorator&amp;lt;&amp;gt;),
    () =&amp;gt;
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; context =
          container.GetInstance&amp;lt;IUserContext&amp;gt;();
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; !context.UserInRole(&lt;span style="color:#A31515;"&gt;&amp;quot;Admin&amp;quot;&lt;/span&gt;);
    });
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Fri, 05 Apr 2013 15:29:54 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Runtime-Decorators 20130405032954P</guid></item><item><title>Updated Wiki: Advanced-scenarios</title><link>https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;version=53</link><description>&lt;div class="wikidoc"&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: this documentation is specific for &lt;b&gt;Simple Injector version 2.0&lt;/b&gt; and up. Look &lt;a href="http://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;amp;version=48"&gt;here&lt;/a&gt; for 1.x specific documentation.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h2&gt;Advanced scenarios with the Simple Injector&lt;/h2&gt;
Although its name might not indicate it, &lt;b&gt;Simple Injector&lt;/b&gt; is capable of handling many complex scenarios. Sometimes by writing some custom code, taking code from this wiki, or by using the extension points from the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace of the core library.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: After including the &lt;b&gt;SimpleInjector.dll&lt;/b&gt; to your project, you will have to add the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace to your code to be able to use most features that are presented in this wiki page.&lt;/i&gt;&lt;/blockquote&gt;
This page discusses the following subjects:
&lt;ul&gt;&lt;li&gt;&lt;a href="#Batch-Registration"&gt;Batch registration &amp;#47; Automatic registration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Registration-Of-Open-Generic-Types"&gt;Registration of open generic types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Unregistered-Type-Resolution"&gt;Unregistered type resolution&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Context-Based-Injection"&gt;Context based injection &amp;#47; Contextual binding&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="#Decorators"&gt;Decorators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Interception"&gt;Interception&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Property-Injection"&gt;Property injection&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Covariance-Contravariance"&gt;Covariance and Contravariance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Plugins"&gt;Registering plugins dynamically&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Batch registration / Automatic registration&lt;a name="Batch_Registration"&gt;&lt;/a&gt;&lt;a name="Batch-Registration"&gt;&lt;/a&gt;&lt;/h3&gt;Batch registration or automatic registration is a way to register all types in a given source based on some sort of convention. This saves you from adding a new line to your configuration for each new type that you register, as in the following example: &lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IUserRepository, SqlUserRepository&amp;gt;();
container.Register&amp;lt;ICustomerRepository, SqlCustomerRepository&amp;gt;();
container.Register&amp;lt;IOrderRepository, SqlOrderRepository&amp;gt;();
container.Register&amp;lt;IProductRepository, SqlProductRepository&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;To prevent having to change the container configuration too often, we can use the non-generic registration overloads, combined with a simple LINQ query:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; repositoryAssembly = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(SqlUserRepository).Assembly;

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; registrations =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; type &lt;span style="color:Blue;"&gt;in&lt;/span&gt; repositoryAssembly.GetExportedTypes()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; type.Namespace == &lt;span style="color:#A31515;"&gt;&amp;quot;MyComp.MyProd.BL.SqlRepositories&amp;quot;&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; type.GetInterfaces().Any()
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt;
    {
        Service = type.GetInterfaces().Single(),
        Implementation = type
    };

&lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; reg &lt;span style="color:Blue;"&gt;in&lt;/span&gt; registrations)
{
    container.Register(reg.Service, reg.Implementation, Lifestyle.Transient);
}
&lt;/pre&gt;&lt;/div&gt;Although many DI frameworks contain an advanced API for doing convention based registration, we found that doing this with custom LINQ queries is often easier to write, easier to understand, and even more flexible than using such an API.&lt;br /&gt;&lt;br /&gt;Another interesting scenario is that of registering implementations of some generic interface. Say for instance that your application contains the following interface:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IValidate&amp;lt;T&amp;gt;
{
    ValidationResults Validate(T instance);
}
&lt;/pre&gt;&lt;/div&gt;Your application might contain many implementations of this interface, for instance for validating customers, employees, orders, products, etc. You would normally end up with a registration as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;Customer&amp;gt;, CustomerValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Employee&amp;gt;, EmployeeValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Order&amp;gt;, OrderValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Product&amp;gt;, ProductValidator&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;Using the extension methods for batch registration of open generic types from the &lt;i&gt;SimpleInjector.Extesions&lt;/i&gt; namespace however, you can write it down to a single line:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;This call searches in the supplied assembly for all public types that implement the &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; interface and it registers each type by their specific interface (closed generic) interface. It even works for types that implement multiple closed versions of the given interface.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: There are &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_OpenGenericBatchRegistrationExtensions_RegisterManyForOpenGeneric.htm"&gt;overloads&lt;/a&gt; available that take a list of &lt;b&gt;System.Type&lt;/b&gt; instances, instead a list of &lt;b&gt;Assembly&lt;/b&gt; instances.&lt;/i&gt;&lt;/blockquote&gt;
This however is just one example of what you can do using batch registration. A more advanced scenario is the registration of multiple implementations of the same closed generic type to a single interface. Because there are many possible variations of this scenario, &lt;b&gt;Simple Injector&lt;/b&gt; does not contain any methods to do this. It does contain however, multiple overloads of the &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; that allows you to supply a callback delegate that lets you do the registration yourself. Take for instance the scenario where you have a &lt;i&gt;CustomerValidator&lt;/i&gt; and a &lt;i&gt;GoldCustomerValidator&lt;/i&gt; type, that each implement &lt;i&gt;IValidate&amp;lt;Customer&amp;gt;&lt;/i&gt;, and you want to register both of them. The previous registration would throw an exception telling you that you have multiple types implementing the same closed generic type. The following registration however, does enable this scenario:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;),
    AccessibilityOption.PublicTypesOnly,
    (serviceType, implTypes) =&amp;gt; container.RegisterAll(serviceType, implTypes),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly
);
&lt;/pre&gt;&lt;/div&gt;The previous code snippet registers all types from the given assembly that implement &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; and it registers them as a collection of instances. In other words, with the given example, &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; instances can be retrieved by calling &lt;i&gt;container.GetAllInstances&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;()&lt;/i&gt; or by using an &lt;i&gt;IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; argument in a constructor. However, using an &lt;i&gt;IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; dependency in your constructor or calling into the container directly, are generally not recommended approaches. Depending on a collection of types complicates your application design, and can often be simplified with the right configuration of your container. Your constructors should simply depend on a single &lt;i&gt;IValidator&amp;lt;T&amp;gt;&lt;/i&gt;. A better solution would therefore be to create and inject a &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; in that case:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CompositeValidator&amp;lt;T&amp;gt; : IValidate&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt; validators;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; CompositeValidator(IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt; validators)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validators = validators;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationResults Validate(T instance)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; allResults = ValidationResults.Valid;

        &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; validator &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validators)
        {
            &lt;span style="color:Blue;"&gt;var&lt;/span&gt; results = validator.Validate(instance);
            allResults = ValidationResults.Join(allResults, results);
        }

        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; allResults;
    }
}
&lt;/pre&gt;&lt;/div&gt;This &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; can be registered as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CompositeValidator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This maps the open generic &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; interface to the open generic &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; implementation. Because the &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; contains an &lt;i&gt;IEnumerable&amp;lt;IValidator&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; dependency, the registered types will be injected into its constructor. This allows you to let the rest of the application simply depend on the &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt;, while registering a collection of &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; implementations under the covers.&lt;br /&gt;&lt;br /&gt;The next section will explain mapping of open generic types, as the one we&amp;#39;ve just seen.&lt;br /&gt;
&lt;h3&gt;Registration of open generic types&lt;a name="Registration_Of_Open_Generic_Types"&gt;&lt;/a&gt;&lt;a name="Registration-Of-Open-Generic-Types"&gt;&lt;/a&gt;&lt;/h3&gt;When working with generic interfaces, we will often see many implementations of that interface being registered:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;Customer&amp;gt;, CustomerValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Employee&amp;gt;, EmployeeValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Order&amp;gt;, OrderValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Product&amp;gt;, ProductValidator&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;As the previous section explained, this can be rewritten to the following one-liner:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;Sometimes you&amp;#39;ll find that many implementations of the given generic interface are no-ops. The &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; is a good example, because it is very likely that not all entities will need validation. Still we would like to prevent from having to write an empty validation class per entity or having to write special application logic to check if an entity has validation. In this situation, we will probably want to use the registration as described in the previous section, and fallback to returning the default implementation when no such registration exists for a given type. Such default implementation could look like this, for instance:&lt;br /&gt; &lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Implementation of the Null Object pattern.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;class&lt;/span&gt; NullValidator&amp;lt;T&amp;gt; : IValidate&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationResults Validate(T instance)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; ValidationResults.Valid;
    }
}
&lt;/pre&gt;&lt;/div&gt;We can use this &lt;i&gt;NullValidator&amp;lt;T&amp;gt;&lt;/i&gt; for all entities that don&amp;#39;t need validation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;OrderLine&amp;gt;, NullValidator&amp;lt;OrderLine&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Address&amp;gt;, NullValidator&amp;lt;Address&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;UploadImage&amp;gt;, NullValidator&amp;lt;UploadImage&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Mothership&amp;gt;, NullValidator&amp;lt;Mothership&amp;gt;&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;This is of course not very practical. Falling back to such a default implementation is a typical scenario for &lt;i&gt;unregistered type resolution&lt;/i&gt;. The &lt;b&gt;Simple Injector&lt;/b&gt; contains an event that you can hook to that allows to fallback to a default implementation. On top of this event, the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Methods_T_SimpleInjector_Extensions_OpenGenericRegistrationExtensions.htm"&gt;RegisterOpenGeneric&lt;/a&gt; extension method available. The given situation would be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(NullValidator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;The result of this registration is that for instance an &lt;i&gt;NullValidator&amp;lt;Department&amp;gt;&lt;/i&gt; instance is returned when an &lt;i&gt;IValidate&amp;lt;Department&amp;gt;&lt;/i&gt; is requested.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Because the use of unregistered type resolution, a NullValidator&amp;lt;T&amp;gt; will only get returned for types that are not registered, therefore allowing the default implementation to be overridden for a specific implementation. The &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; method, for instance, doesn’t use unregistered type resolution, but simply registers all concrete types it finds in the given assemblies. Those types will therefore always be returned, giving a very convenient and easy to grasp mix.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Unregistered type resolution&lt;a name="Unregistered_Type_Resolution"&gt;&lt;/a&gt;&lt;a name="Unregistered-Type-Resolution"&gt;&lt;/a&gt;&lt;/h3&gt;Unregistered type resolution is the ability to get notified by the container when a type is requested that is currently unregistered in the container. This gives the user the change of registering that type. The &lt;b&gt;Simple Injector&lt;/b&gt; supports this scenario with the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/E_SimpleInjector_Container_ResolveUnregisteredType.htm"&gt;ResolveUnregisteredType&lt;/a&gt; event. Unregistered type resolution enables many advanced scenarios. The framework itself uses this event for implementing the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Registration_Of_Open_Generic_Types"&gt;registration of open generic types&lt;/a&gt;. Other examples of possible scenarios that can be built on top of this event are &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolving_Arrays_And_Lists"&gt;resolving array and lists&lt;/a&gt; and &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Covariance-Contravariance"&gt;covariance and contravariance&lt;/a&gt;. Those scenarios are described here in the advanced scenarios page.&lt;br /&gt;&lt;br /&gt;For more information about how to use this event, please look at the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/E_SimpleInjector_Container_ResolveUnregisteredType.htm"&gt;ResolveUnregisteredType event documentation&lt;/a&gt; in the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/"&gt;reference library&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;Context based injection&lt;a name="Context_Based_Injection"&gt;&lt;/a&gt;&lt;a name="Contextual_Binding"&gt;&lt;/a&gt;&lt;a name="Context-Based-Injection"&gt;&lt;/a&gt;&lt;/h3&gt;Context based injection is the ability to inject a dependency based on the context it lives in. This context is often supplied by the container. Some DI frameworks contain a feature that allows this, while others don’t. The &lt;b&gt;Simple Injector&lt;/b&gt; does &lt;i&gt;not&lt;/i&gt; does not contain such a feature out of the box, but this ability can easily be added by using the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ContextDependentExtensions&amp;referringTitle=Advanced-scenarios"&gt;context based injection extension method&lt;/a&gt; code snippet.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: In many cases context based injection is not the best solution, and the design should be reevaluated. In some narrow cases however it can make sense.&lt;/i&gt;&lt;/blockquote&gt;
The most common scenario is to base the type of the injected dependency on the type of the consumer. Take for instance the following &lt;i&gt;ILogger&lt;/i&gt; interface with a generic &lt;i&gt;Logger&amp;lt;T&amp;gt;&lt;/i&gt; class that needs to be injected into several consumers. &lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; ILogger
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message);
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Logger&amp;lt;T&amp;gt; : ILogger
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message) { }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Consumer1
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Consumer1(ILogger logger) { }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Consumer2
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Consumer2(ILogger logger) { }
}
&lt;/pre&gt;&lt;/div&gt;In this case we want to inject a &lt;i&gt;Logger&amp;lt;Consumer1&amp;gt;&lt;/i&gt; into &lt;i&gt;Consumer1&lt;/i&gt; and a &lt;i&gt;Logger&amp;lt;Consumer2&amp;gt;&lt;/i&gt; into &lt;i&gt;Consumer2&lt;/i&gt;. By using the previous &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ContextDependentExtensions&amp;referringTitle=Advanced-scenarios"&gt;extension method&lt;/a&gt;, we can accomplish this as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterWithContext&amp;lt;ILogger&amp;gt;(dependencyContext =&amp;gt;
{
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; type = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Logger&amp;lt;&amp;gt;).MakeGenericType(
        dependencyContext.ImplementationType);
    
    &lt;span style="color:Blue;"&gt;return&lt;/span&gt; (ILogger)container.GetInstance(type);
});
&lt;/pre&gt;&lt;/div&gt;In the previous code snippet we registered a &lt;i&gt;Func&amp;lt;DependencyContext, ILogger&amp;gt;&lt;/i&gt; delegate, that will get called each time a &lt;i&gt;ILogger&lt;/i&gt; dependency gets resolved. The &lt;i&gt;DependencyContext&lt;/i&gt; instance that gets supplied to that instance, contains the &lt;i&gt;ServiceType&lt;/i&gt; and &lt;i&gt;ImplementationType&lt;/i&gt; into which the &lt;i&gt;ILogger&lt;/i&gt; is getting injected.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Although building a generic type using MakeGenericType is relatively slow, the call to the Func&amp;lt;DependencyContext, TService&amp;gt; delegate itself is about as cheap as calling a Func&amp;lt;TService&amp;gt; delegate. If performance of the MakeGenericType gets a problem, you can always cache the generated types, cache InstanceProducer instances, or cache ILogger instances (note that caching the ILogger instances will make them singletons).&lt;/i&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Even though the use of a generic Logger&amp;lt;T&amp;gt; is a common design (with log4net as the grand godfather of this design), doesn&amp;#39;t always make it a good design. The need for having the logger contain information about its parent type, might indicate design problems. If you&amp;#39;re doing this, please take a look at &lt;a href="http://stackoverflow.com/a/9915056/264697"&gt;this Stackoverflow answer&lt;/a&gt;. It talks about logging in conjunction with the SOLID design principles.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Decorators&lt;a name="Decorators"&gt;&lt;/a&gt;&lt;a name="Generic_Decorators"&gt;&lt;/a&gt;&lt;/h3&gt;The &lt;a href="http://en.wikipedia.org/wiki/SOLID"&gt;SOLID&lt;/a&gt; principles give us important guidance when it comes to writing maintainable software. The &amp;#39;O&amp;#39; of the &amp;#39;SOLID&amp;#39; acronym stands for the &lt;a href="http://en.wikipedia.org/wiki/Open/closed_principle"&gt;Open/closed Principle&lt;/a&gt; which states that classes should be open for extension, but closed for modification. Designing systems around the Open/closed principle means that new behavior can be plugged into the system, without the need to change any existing parts, making the change of breaking existing code much smaller.&lt;br /&gt;&lt;br /&gt;One of the ways to add new functionality (such as &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=http%3a%2f%2fen.wikipedia.org%2fwiki%2fCross-cutting_concern&amp;referringTitle=Advanced-scenarios"&gt;cross-cutting concerns&lt;/a&gt;) to types is by the use of the &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;decorator pattern&lt;/a&gt;. The decorator pattern can be used to extend (decorate) the functionality of a certain object at run-time. Especially when using generic interfaces, the concept of decorators gets really powerful. Take for instance the examples given in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Registration_Of_Open_Generic_Types"&gt;Registration of open generic types&lt;/a&gt; section of this page or for instance the use of an generic &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; interface.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: &lt;a href="http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91"&gt;This article&lt;/a&gt; describes an architecture based on the use of the ICommandHandler&amp;lt;TCommand&amp;gt; interface.&lt;/i&gt;&lt;/blockquote&gt;
Take the plausible scenario where we want to validate all commands that get executed by an &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; implementation. The Open/Closed principle states that we want to do this, without having to alter each and every implementation. We can do this using a (single) decorator:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;
    : ICommandHandler&amp;lt;TCommand&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IValidator validator;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ICommandHandler&amp;lt;TCommand&amp;gt; handler;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationCommandHandlerDecorator(
        IValidator validator, 
        ICommandHandler&amp;lt;TCommand&amp;gt; handler)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validator = validator;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handler = handler;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; ICommandHandler&amp;lt;TCommand&amp;gt;.Handle(TCommand command)
    {
        &lt;span style="color:Green;"&gt;// validate the supplied command (throws when invalid).&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validator.ValidateObject(command);
		
        &lt;span style="color:Green;"&gt;// forward the (valid) command to the real&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// command handler.&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handler.Handle(command);
    }
}
&lt;/pre&gt;&lt;/div&gt;The &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; class is an implementation of the &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; interface, but it also wraps / decorates an &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; instance. Instead of injecting the real implementation directly into a consumer, we can (let Simple Injector) inject a validator decorator that wraps the real implementation.&lt;br /&gt;&lt;br /&gt;The &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; depends on an &lt;i&gt;IValidator&lt;/i&gt; interface. An implementation that used Microsoft Data Annotations might look like this:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.ComponentModel.DataAnnotations;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; DataAnnotationsValidator : IValidator
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IServiceProvider container;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; DataAnnotationsValidator(Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }
    
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IValidator.ValidateObject(&lt;span style="color:Blue;"&gt;object&lt;/span&gt; instance)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; context = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ValidationContext(instance,
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container, &lt;span style="color:Blue;"&gt;null&lt;/span&gt;);

        &lt;span style="color:Green;"&gt;// Throws an exception when instance is invalid.&lt;/span&gt;
        Validator.ValidateObject(instance, context);    
    }
}
&lt;/pre&gt;&lt;/div&gt;The implementations of the &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt; interface can be registered using the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_OpenGenericBatchRegistrationExtensions_RegisterManyForOpenGeneric.htm"&gt;RegisterManyForOpenGeneric&lt;/a&gt; extension method:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;By using the following extension method from the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace, you can wrap the &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; around each and every &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; implementation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;Multiple decorators can be wrapped by calling the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterDecorator.htm"&gt;RegisterDecorator&lt;/a&gt; method multiple times, as the following registration shows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TransactionCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DeadlockRetryCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;The decorators are applied in the order in which they are registered, which means that the first decorator (&lt;i&gt;TransactionCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; in this case) wraps the real instance, the second decorator (&lt;i&gt;DeadlockRetryCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; in this case) wraps the first decorator, and so on.&lt;br /&gt;&lt;br /&gt;There&amp;#39;s an overload of the &lt;b&gt;RegisterDecorator&lt;/b&gt; available that allows you to supply a predicate to determine whether that decorator should be applied to a specific service type. Using a given context you can determine whether the decorator should be applied. Here is an example:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AccessValidationCommandHandlerDecorator&amp;lt;&amp;gt;),
    context =&amp;gt; !context.ImplementationType.Namespace.EndsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Admins&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;The given context contains several properties that allows you to analyze whether a decorator should be applied to a given service type, such as the current closed generic service type (using the &lt;i&gt;ServiceType&lt;/i&gt; property) and the concrete type that will be created (using the &lt;i&gt;ImplementationType&lt;/i&gt; property). The predicate will (under normal circumstances) be called only once per generic type, so there is no performance implication for using it.&lt;br /&gt;
&lt;h4&gt;Decorators with Func&amp;lt;T&amp;gt; factories&lt;a name="Decorators_With_Func_Factories"&gt;&lt;/a&gt;&lt;/h4&gt;In certain scenarios, it is needed to postpone building part of the object graph. For instance when a service needs to control the lifetime of a dependency, need multiple instances, when instances need to be &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Multi_Threaded_Applications"&gt;executed on a different thread&lt;/a&gt;, or when instances need to be created in a certain &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Scoped"&gt;scope&lt;/a&gt; or (security) context.&lt;br /&gt;&lt;br /&gt;When building a &amp;#39;normal&amp;#39; object graph with dependencies, you can easily delay building a part of the graph by letting a service depend on a factory. This allows building that part of the object graph to be postponed until the time the type starts using the factory. When working with decorators however, injecting a factory to postpone the creation of the decorated instance will not work. Take for instance a &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; that allows executing a command handler on a different thread. We could let the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; depend on a &lt;i&gt;CommandHandlerFactory&amp;lt;T&amp;gt;&lt;/i&gt;, and let this factory call back into the container to retrieve a new &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt;. Unfortunately this would fail, since requesting an &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt; would again wrap this instance with a new &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt;, and we&amp;#39;d end up causing a stack overflow.&lt;br /&gt;&lt;br /&gt;Since this is a scenario that is really hard to solve without framework support, &lt;b&gt;Simple Injector&lt;/b&gt; allows injecting a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate into registered decorators. This delegate functions as a factory for the creation of the decorated instance. Taking the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; as example, it could be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;
    : ICommandHandler&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;ICommandHandler&amp;lt;T&amp;gt;&amp;gt; factory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AsyncCommandHandlerDecorator(
        Func&amp;lt;ICommandHandler&amp;lt;T&amp;gt;&amp;gt; factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
	
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(T command)
    {
        &lt;span style="color:Green;"&gt;// Execute on different thread.&lt;/span&gt;
        ThreadPool.QueueUserWorkItem(_ =&amp;gt;
        {
            &lt;span style="color:Green;"&gt;// Create new handler in this thread.&lt;/span&gt;
            &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory();
            handler.Handle(command)
        });
    }
}
&lt;/pre&gt;&lt;/div&gt;This special decorator can be registered just as any other decorator:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;However, since the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; solely has singleton dependencies (the &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; is a singleton), and creates a new decorated instance each time it’s called, we can even register it as a singleton itself:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;When mixing this with other (synchronous) decorators, you&amp;#39;ll get an extremely powerful and pluggable system:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TransactionCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DeadlockRetryCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterSingleDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This configuration has an interesting mix of decorator registrations. The registration of the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; allows (some of) the command handlers to be executed on the background (while others -who&amp;#39;s name does not start with &amp;#39;Async&amp;#39;- still run synchronously), but before execution, all commands are validated synchronously (to allow communicating validation errors to the caller). And all handlers (even the asynchronous ones) are executed in a transaction and the operation is retried when the database rolled back because of a deadlock).&lt;br /&gt;
&lt;h4&gt;Decorated collections&lt;a name="Decorated_Collections"&gt;&lt;/a&gt;&lt;/h4&gt;When registering a decorator, Simple Injector will automatically decorate any collection with elements of that service type:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterAll&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt;(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CustomerMovedEventHandler),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(NotifyStaffWhenCustomerMovedEventHandler));
    
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationEventHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; SomeCondition);
&lt;/pre&gt;&lt;/div&gt;The previous registration registers a collection of &lt;i&gt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&lt;/i&gt; services. Those services are decorated with a &lt;i&gt;ValidationEventHandlerDecorator&amp;lt;TEvent&amp;gt;&lt;/i&gt; when the supplied predicate holds.&lt;br /&gt;&lt;br /&gt;For collections of elements that are created by the container (container controlled), the predicate is checked for each element in the collection. For collections of uncontrolled elements (a list of items that is not created by the container), the predicate is checked once for the whole collection. This means that controlled collections can be partially decorated. Taking the previous example for instance, you could let the &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; be decorated, while leaving the &lt;i&gt;NotifyStaffWhenCustomerMovedEventHandler&lt;/i&gt; undecorated (determined by the supplied predicate).&lt;br /&gt;&lt;br /&gt;When a collection is uncontrolled, it means that the lifetime of its elements are unknown to the container. The following registration is an example of an uncontrolled collection:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
IEnumerable&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt; handlers =
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;[]
    {
        &lt;span style="color:Blue;"&gt;new&lt;/span&gt; CustomerMovedEventHandler(),
        &lt;span style="color:Blue;"&gt;new&lt;/span&gt; NotifyStaffWhenCustomerMovedEventHandler(),
    };

container.RegisterAll&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt;(handlers);
&lt;/pre&gt;&lt;/div&gt;Although this registration contains a list of singletons, the container has no way of knowing this. The collection could easily have been a dynamic (an ever changing) collection. In this case, the container calls the registered predicate once (and supplies the predicate with the &lt;i&gt;IEventHandler&amp;lt;CusotmerMovedEvent&amp;gt;&lt;/i&gt; type)  and if the predicate returns true, each element in the collection is decorated with a decorator instance.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Warning&lt;/b&gt;: In general you should prevent registering uncontrolled collections. The container knows nothing about them, and can&amp;#39;t help you in doing &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;diagnostics&lt;/a&gt;. Since the lifetime of those items is unknown, the container will be unable to wrap a decorator with a lifestyle other than transient. Best practice is to register container-controlled collections which is done by using one of the &lt;b&gt;RegisterAll&lt;/b&gt; overloads that take a collection of &lt;b&gt;System.Type&lt;/b&gt; instances.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Interception&lt;a name="Interception"&gt;&lt;/a&gt;&lt;/h3&gt;Interception is the ability to intercept a call from a consumer to a service, and add or change behavior. The &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;decorator pattern&lt;/a&gt; describes a form of interception, but when it comes to applying cross-cutting concerns, you might end up writing decorators for many service interfaces, but with the exact same code. If this is happening, it is time to explore the possibilities of interception.&lt;br /&gt;&lt;br /&gt;Using the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets, you can add the ability to do interception with the &lt;b&gt;Simple Injector&lt;/b&gt;. Using the given code, you can for instance define a &lt;i&gt;MonitoringInterceptor&lt;/i&gt; that allows logging the execution time of the called service method:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MonitoringInterceptor : IInterceptor
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;

    &lt;span style="color:Green;"&gt;// Using constructor injection on the interceptor&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MonitoringInterceptor(ILogger logger)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = logger;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Intercept(IInvocation invocation)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; watch = Stopwatch.StartNew();

        &lt;span style="color:Green;"&gt;// Calls the decorated instance.&lt;/span&gt;
        invocation.Proceed();

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; decoratedType =
            invocation.InvocationTarget.GetType();
        
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger.Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt;.Format(
            &lt;span style="color:#A31515;"&gt;&amp;quot;{0} executed in {1} ms.&amp;quot;&lt;/span&gt;,
            decoratedType.Name,
            watch.ElapsedMiliseconds));
    }
}
&lt;/pre&gt;&lt;/div&gt;This interceptor can be registered to be wrapped around a concrete implementation. Using the given extension methods, this can be done as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.InterceptWith&amp;lt;MonitoringInterceptor&amp;gt;(type =&amp;gt; type == &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IUserRepository));
&lt;/pre&gt;&lt;/div&gt;This registration ensures that every time an &lt;i&gt;IUserRepository&lt;/i&gt; interface is requested, an interception proxy is returned that wraps that instance and uses the &lt;i&gt;MonitoringInterceptor&lt;/i&gt; to extend the behavior.&lt;br /&gt;&lt;br /&gt;The current example doesn&amp;#39;t add much compared to simply using a decorator. When having many interface service types that need to be decorated with the same behavior however, it gets different:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.InterceptWith&amp;lt;MonitoringInterceptor&amp;gt;(t =&amp;gt; t.Name.EndsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Repository&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets use .NET&amp;#39;s &lt;/i&gt;System.Runtime.Remoting.Proxies.RealProxy&lt;i&gt; class to generate interception proxies. The &lt;/i&gt;RealProxy&lt;i&gt; only allows to proxy interfaces.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: the interfaces in the given &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets are a simplified version of the Castle Project interception facility. If you need to create lots different interceptors, you might benefit from using the interception abilities of the Castle Project. Also please note that the given snippets use dynamic proxies to do the interception, while Castle uses lightweight code generation (LCG). LCG allows much better performance than the use of dynamic proxies.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Don&amp;#39;t use interception for intercepting types that all implement the same generic interface, such as ICommandHandler&amp;lt;T&amp;gt; or IValidator&amp;lt;T&amp;gt;. Try using decorator classes instead, as shown in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Decorators"&gt;Decorators&lt;/a&gt; section on this page.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Property injection&lt;a name="Implicit_Property_Injection"&gt;&lt;/a&gt;&lt;a name="Implicit-Property-Injection"&gt;&lt;/a&gt;&lt;a name="Property-Injection"&gt;&lt;/a&gt;&lt;/h3&gt;Simple Injector does not inject any properties into types that get resolved by the container. In general there are two ways of doing property injection, and both are not supported by default for reasons explained below.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Implicit property injection&lt;/b&gt;&lt;br /&gt;Some containers (such as Castle Windsor) implicitly inject public writable properties by default for any instance you resolve. They do this by mapping those properties to configured types. When no such registration exists, or when the property doesn’t have a public setter, the property will be skipped. Simple Injector does not do this, and for good reason. We think that &lt;i&gt;implicit property injection&lt;/i&gt; is simply too uuhh...  implicit :-). Silently skipping properties that can&amp;#39;t be mapped can lead to a DI configuration that can&amp;#39;t be verified easily and can therefore result in an application that fails at runtime instead of failing when the container is verified.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Explicit property injection&lt;/b&gt;&lt;br /&gt;We strongly feel that explicit property injection is a much better way to go. With explicit property injection the container is forced to inject a property and the process will fail immediately when a property can&amp;#39;t be mapped or injected. Some containers (such as Unity and Ninject) allow explicit property injection by default by allowing properties to be decorated with attributes. Problem with this is that this forces the application to take a dependency on the framework, which is something that should be prevented.&lt;br /&gt;&lt;br /&gt;Because Simple Injector does not encourage its users to take a dependency on the container (except for the startup path of course), Simple Injector does not contain any attributes that allow explicit property injection and it can therefore not explicitly inject properties out-of-the-box.&lt;br /&gt;&lt;br /&gt;Besides this, the use of property injection should be very exceptional and in general constructor injection should always be used. If a constructor gets too many parameters (constructor over-injection anti-pattern), it is an indication of a violation of the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; (SRP). SRP violations often lead to maintainability issues. So instead of patching constructor over-injection with property injection, the root cause should be analyzed and the type should be refactored, probably with &lt;a href="http://blog.ploeh.dk/2010/02/02/RefactoringtoAggregateServices/"&gt;Facade Services&lt;/a&gt;. Another common reason to use properties is because those dependencies are optional. Instead of using optional property dependencies, best practice is to inject empty implementations (a.k.a. &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object pattern&lt;/a&gt;) into the constructor.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Enabling property injection&lt;/b&gt;&lt;br /&gt;Simple Injector contains two ways to enable property injection. First of all the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Configuring_Property_Injection"&gt;RegisterInitializer&amp;#60;T&amp;#62;&lt;/a&gt; method can be used to inject properties (especially configuration values) on a per-type basis. Take for instance the following code snippet:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterInitializer&amp;lt;HandlerBase&amp;gt;(handlerToInitialize =&amp;gt;
{
    handlerToInitialize.ExecuteAsynchronously = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;
});
&lt;/pre&gt;&lt;/div&gt;In the previous example a &lt;i&gt;Action&amp;lt;T&amp;gt;&lt;/i&gt; delegate is registered that will be called every time the container creates a type that inherits from &lt;i&gt;HandlerBase&lt;/i&gt;. In this case, the handler will set a configuration value on that class.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: although this method can also be used injecting services, please note that the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;Diagnostic Services&lt;/a&gt; will be unable to see and analyze this dependency.&lt;/i&gt;&lt;/blockquote&gt;
The second way to inject properties is by implementing a custom &lt;b&gt;IPropertySelectionBehavior&lt;/b&gt;. The &lt;i&gt;property selection behavior&lt;/i&gt; is a general extension point provided by the container, to override the library&amp;#39;s default behavior. The following code for instance enables explicit property injection using attributes, using any customly defined attribute:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Reflection;

&lt;span style="color:Blue;"&gt;using&lt;/span&gt; SimpleInjector.Advanced;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AttributedPropertyInjectionExtensions
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; AutowirePropertiesWithAttribute&amp;lt;TAttribute&amp;gt;(
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt; ContainerOptions options)
        &lt;span style="color:Blue;"&gt;where&lt;/span&gt; TAttribute : Attribute
    {
        options.PropertySelectionBehavior =
            &lt;span style="color:Blue;"&gt;new&lt;/span&gt; AttributePropertyInjectionBehavior(
                options.PropertySelectionBehavior, 
                &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TAttribute));
    }

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AttributePropertyInjectionBehavior 
        : IPropertySelectionBehavior
    {
        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IPropertySelectionBehavior behavior;
        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Type attribute;

        &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AttributePropertyInjectionBehavior(
            IPropertySelectionBehavior baseBehavior, 
            Type attributeType)
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.behavior = baseBehavior;
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.attribute = attributeType;
        }

        &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;bool&lt;/span&gt; SelectProperty(Type type, PropertyInfo prop)
        {
            &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.IsPropertyDecorated(prop) ||
                &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.behavior.SelectProperty(type, prop);
        }

        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;bool&lt;/span&gt; IsPropertyDecorated(PropertyInfo p)
        {
            &lt;span style="color:Blue;"&gt;return&lt;/span&gt; p.GetCustomAttributes(&lt;span style="color:Blue;"&gt;this&lt;/span&gt;.attribute, &lt;span style="color:Blue;"&gt;true&lt;/span&gt;).Any();
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;The previous code snippet can be used as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();
container.Options.AutowirePropertiesWithAttribute&amp;lt;MyInjectAttribute&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;This enables explicit property injection on all properties that are marked with the &lt;b&gt;MyInjectAttribute&lt;/b&gt; and it will fail when the property cannot be injected for whatever reason.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: Properties injected by the container through the &lt;b&gt;IPropertySelectionBehavior&lt;/b&gt; will be analyzed by the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;Diagnostic Services&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The &lt;b&gt;IPropertySelectionBehavior&lt;/b&gt; extension mechanism can also be used to implement implicit property injection. Doing so however is not advised because of the reasons given above.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Covariance and Contravariance&lt;a name="Covariance_Contravariance"&gt;&lt;/a&gt;&lt;a name="Covariance-Contravariance"&gt;&lt;/a&gt;&lt;/h3&gt;Since version 4.0 of the .NET framework, the type system allows &lt;a href="http://msdn.microsoft.com/en-us/library/dd799517.aspx"&gt;Covariance and Contravariance in Generics&lt;/a&gt; (especially interfaces and delegates). This allows for instance, to use a &lt;i&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/i&gt; as an &lt;i&gt;IEnumerable&amp;lt;object&amp;gt;&lt;/i&gt; (covariance), or to use an &lt;i&gt;Action&amp;lt;object&amp;gt;&lt;/i&gt; as an &lt;i&gt;Action&amp;lt;string&amp;gt;&lt;/i&gt; (contravariance).&lt;br /&gt;&lt;br /&gt;In some circumstances, the application design can benefit from the use of covariance and contravariance (or variance for short) and it would be beneficial when the IoC container returns services that are &amp;#39;compatible&amp;#39; to the requested service, even although the requested service is not registered. To stick with the previous example, the container could return an &lt;i&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/i&gt; even when an &lt;i&gt;IEnumerable&amp;lt;object&amp;gt;&lt;/i&gt; is requested.&lt;br /&gt;&lt;br /&gt;By default, the Simple Injector does not return variant implementations of given services, but the Simple Injector can be extended to behave this way. The actual way to write this extension depends on the requirements of the application.&lt;br /&gt;&lt;br /&gt;Take a look at the following application design around the &lt;i&gt;IEventHandler&amp;lt;in TEvent&amp;gt;&lt;/i&gt; interface:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IEventHandler&amp;lt;&lt;span style="color:Blue;"&gt;in&lt;/span&gt; TEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(TEvent e);
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedEvent 
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;int&lt;/span&gt; CustomerId { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Address NewAddress { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedAbroadEvent : CustomerMovedEvent
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Country Country { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedEventHandler : IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(CustomerMovedEvent e) { ... }
}
&lt;/pre&gt;&lt;/div&gt;The design contains two event classes &lt;i&gt;CustomerMovedEvent&lt;/i&gt; and &lt;i&gt;CustomerMovedAbroadEvent&lt;/i&gt; (where &lt;i&gt;CustomerMovedAbroadEvent&lt;/i&gt; inherits from &lt;i&gt;CustomerMovedEvent&lt;/i&gt;) one concrete event handler &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; and a generic interface for event handlers.&lt;br /&gt;&lt;br /&gt;We can configure the container in such way that not only a request for &lt;i&gt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&lt;/i&gt; results in a &lt;i&gt;CustomerMovedEventHandler,&lt;/i&gt; but also a request for &lt;i&gt;IEventHandler&amp;lt;CustomerMovedAbroadEvent&amp;gt;&lt;/i&gt; results in that same &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; (because &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; also accepts &lt;i&gt;CustomerMovedAbroadEvents&lt;/i&gt;).&lt;br /&gt;&lt;br /&gt;There are several ways to achieve this, but one of the ways is the following:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;CustomerMovedEventHandler&amp;gt;();

container.RegisterSingleOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ContravarianceEventHandler&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This registration depends on the custom &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; that should be placed close to the registration itself:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ContravarianceEventHandler&amp;lt;TEvent&amp;gt;
    : IEventHandler&amp;lt;TEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; Func&amp;lt;IEventHandler&amp;lt;TEvent&amp;gt;&amp;gt; handlerFactory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ContravarianceEventHandler(Container container)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handlerType = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;TEvent&amp;gt;);

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; registration = (
            &lt;span style="color:Blue;"&gt;from&lt;/span&gt; r &lt;span style="color:Blue;"&gt;in&lt;/span&gt; container.GetCurrentRegistrations()
            &lt;span style="color:Blue;"&gt;let&lt;/span&gt; assignableInterfaces = (
                &lt;span style="color:Blue;"&gt;from&lt;/span&gt; iface &lt;span style="color:Blue;"&gt;in&lt;/span&gt; r.ServiceType.GetInterfaces()
                &lt;span style="color:Blue;"&gt;where&lt;/span&gt; handlerType.IsAssignableFrom(iface)
                &lt;span style="color:Blue;"&gt;select&lt;/span&gt; iface)
            &lt;span style="color:Blue;"&gt;where&lt;/span&gt; assignableInterfaces.Any()
            &lt;span style="color:Blue;"&gt;select&lt;/span&gt; r)
            .Single();

        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handlerFactory =
            () =&amp;gt; (IEventHandler&amp;lt;TEvent&amp;gt;)r.GetInstance();
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IEventHandler&amp;lt;TEvent&amp;gt;.Handle(TEvent e)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handlerFactory().Handle(e);
    }
}
&lt;/pre&gt;&lt;/div&gt;The registration ensures that every time an &lt;i&gt;IEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; is requested, a &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; is returned. The &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; will on creation query the container for a single service type that implements &lt;i&gt;IEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt;. Because the &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; is not registered by its interface, but by itself, the &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; will find that type.&lt;br /&gt;&lt;br /&gt;This is just one example and one way of adding variance support. For a more elaborate discussion on this subject, please read the following article: &lt;a href="http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=90"&gt;Adding Covariance and Contravariance to the Simple Injector&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;Registering plugins dynamically&lt;a name="Plugins"&gt;&lt;/a&gt;&lt;/h3&gt;Applications with a plugin architecture often allow special plugin assemblies to be dropped in a special folder and to be picked up by the application, without the need of a recompile. Although Simple Injector does not support this out of the box, registering plugins from dynamically loaded assemblies can be implemented in a few lines of code. Here is an example:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;string&lt;/span&gt; pluginDirectory =
    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, &lt;span style="color:#A31515;"&gt;&amp;quot;Plugins&amp;quot;&lt;/span&gt;);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; pluginAssemblies =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; file &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; DirectoryInfo(pluginDirectory).GetFiles()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; file.Extension == &lt;span style="color:#A31515;"&gt;&amp;quot;.dll&amp;quot;&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; Assembly.LoadFile(file.FullName);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; pluginTypes =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; dll &lt;span style="color:Blue;"&gt;in&lt;/span&gt; pluginAssemblies
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; type &lt;span style="color:Blue;"&gt;in&lt;/span&gt; dll.GetExportedTypes()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IPlugin).IsAssignableFrom(type)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; !type.IsAbstract
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; !type.IsGenericTypeDefinition
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; type;

container.RegisterAll&amp;lt;IPlugin&amp;gt;(pluginTypes);
&lt;/pre&gt;&lt;/div&gt;The given example makes use of an &lt;i&gt;IPlugin&lt;/i&gt; interface that is known to the application, and probably located in a shared assembly. The dynamically loaded plugin .dll files can contain multiple classes that implement &lt;i&gt;IPlugin&lt;/i&gt;, and all publicly exposed concrete types that implements &lt;i&gt;IPlugin&lt;/i&gt; will be registered using the &lt;i&gt;RegisterAll&lt;/i&gt; method and can get resolved using the default auto-wiring behavior of the container, meaning that the plugin must have a single public constructor and all constructor arguments must be resolvable by the container. The plugins can get resolved using &lt;i&gt;container.GetAllInstances&amp;lt;IPlugin&amp;gt;()&lt;/i&gt; or by adding an &lt;i&gt;IEnumerable&amp;lt;IPlugin&amp;gt;&lt;/i&gt; argument to a constructor.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Resolving instances by key&lt;a name="Resolving_Instances_By_Key"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolve_Instances_By_Key"&gt;Resolve Instances By Key&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios"&gt;How to&lt;/a&gt; documentation page, for the information you are looking for.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Resolving array and lists&lt;a name="Resolving_Arrays_And_Lists"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolve_Arrays_And_Lists"&gt;Resolve Arrays and Lists&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios"&gt;How to&lt;/a&gt; page, for the information you are looking for.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Lifetime scoping&lt;a name="Lifetime_scoping"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#PerLifetimeScope"&gt;Per Lifetime Scope&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios"&gt;Object Lifestyle Management&lt;/a&gt; documentation page for more information.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Thu, 04 Apr 2013 09:40:40 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Advanced-scenarios 20130404094040A</guid></item><item><title>Updated Wiki: Advanced-scenarios</title><link>https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;version=52</link><description>&lt;div class="wikidoc"&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: this documentation is specific for &lt;b&gt;Simple Injector version 2.0&lt;/b&gt; and up. Look &lt;a href="http://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;amp;version=48"&gt;here&lt;/a&gt; for 1.x specific documentation.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h2&gt;Advanced scenarios with the Simple Injector&lt;/h2&gt;
Although its name might not indicate it, &lt;b&gt;Simple Injector&lt;/b&gt; is capable of handling many complex scenarios. Sometimes by writing some custom code, taking code from this wiki, or by using the extension points from the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace of the core library.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: After including the &lt;b&gt;SimpleInjector.dll&lt;/b&gt; to your project, you will have to add the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace to your code to be able to use most features that are presented in this wiki page.&lt;/i&gt;&lt;/blockquote&gt;
This page discusses the following subjects:
&lt;ul&gt;&lt;li&gt;&lt;a href="#Batch-Registration"&gt;Batch registration &amp;#47; Automatic registration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Registration-Of-Open-Generic-Types"&gt;Registration of open generic types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Unregistered-Type-Resolution"&gt;Unregistered type resolution&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Context-Based-Injection"&gt;Context based injection &amp;#47; Contextual binding&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="#Decorators"&gt;Decorators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Interception"&gt;Interception&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Property-Injection"&gt;Property injection&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Covariance-Contravariance"&gt;Covariance and Contravariance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Plugins"&gt;Registering plugins dynamically&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Batch registration / Automatic registration&lt;a name="Batch_Registration"&gt;&lt;/a&gt;&lt;a name="Batch-Registration"&gt;&lt;/a&gt;&lt;/h3&gt;Batch registration or automatic registration is a way to register all types in a given source based on some sort of convention. This saves you from adding a new line to your configuration for each new type that you register, as in the following example: &lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IUserRepository, SqlUserRepository&amp;gt;();
container.Register&amp;lt;ICustomerRepository, SqlCustomerRepository&amp;gt;();
container.Register&amp;lt;IOrderRepository, SqlOrderRepository&amp;gt;();
container.Register&amp;lt;IProductRepository, SqlProductRepository&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;To prevent having to change the container configuration too often, we can use the non-generic registration overloads, combined with a simple LINQ query:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; repositoryAssembly = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(SqlUserRepository).Assembly;

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; registrations =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; type &lt;span style="color:Blue;"&gt;in&lt;/span&gt; repositoryAssembly.GetExportedTypes()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; type.Namespace == &lt;span style="color:#A31515;"&gt;&amp;quot;MyComp.MyProd.BL.SqlRepositories&amp;quot;&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; type.GetInterfaces().Any()
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt;
    {
        Service = type.GetInterfaces().Single(),
        Implementation = type
    };

&lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; reg &lt;span style="color:Blue;"&gt;in&lt;/span&gt; registrations)
{
    container.Register(reg.Service, reg.Implementation, Lifestyle.Transient);
}
&lt;/pre&gt;&lt;/div&gt;Although many DI frameworks contain an advanced API for doing convention based registration, we found that doing this with custom LINQ queries is often easier to write, easier to understand, and even more flexible than using such an API.&lt;br /&gt;&lt;br /&gt;Another interesting scenario is that of registering implementations of some generic interface. Say for instance that your application contains the following interface:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IValidate&amp;lt;T&amp;gt;
{
    ValidationResults Validate(T instance);
}
&lt;/pre&gt;&lt;/div&gt;Your application might contain many implementations of this interface, for instance for validating customers, employees, orders, products, etc. You would normally end up with a registration as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;Customer&amp;gt;, CustomerValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Employee&amp;gt;, EmployeeValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Order&amp;gt;, OrderValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Product&amp;gt;, ProductValidator&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;Using the extension methods for batch registration of open generic types from the &lt;i&gt;SimpleInjector.Extesions&lt;/i&gt; namespace however, you can write it down to a single line:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;This call searches in the supplied assembly for all public types that implement the &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; interface and it registers each type by their specific interface (closed generic) interface. It even works for types that implement multiple closed versions of the given interface.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: There are &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_OpenGenericBatchRegistrationExtensions_RegisterManyForOpenGeneric.htm"&gt;overloads&lt;/a&gt; available that take a list of &lt;b&gt;System.Type&lt;/b&gt; instances, instead a list of &lt;b&gt;Assembly&lt;/b&gt; instances.&lt;/i&gt;&lt;/blockquote&gt;
This however is just one example of what you can do using batch registration. A more advanced scenario is the registration of multiple implementations of the same closed generic type to a single interface. Because there are many possible variations of this scenario, &lt;b&gt;Simple Injector&lt;/b&gt; does not contain any methods to do this. It does contain however, multiple overloads of the &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; that allows you to supply a callback delegate that lets you do the registration yourself. Take for instance the scenario where you have a &lt;i&gt;CustomerValidator&lt;/i&gt; and a &lt;i&gt;GoldCustomerValidator&lt;/i&gt; type, that each implement &lt;i&gt;IValidate&amp;lt;Customer&amp;gt;&lt;/i&gt;, and you want to register both of them. The previous registration would throw an exception telling you that you have multiple types implementing the same closed generic type. The following registration however, does enable this scenario:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;),
    AccessibilityOption.PublicTypesOnly,
    (serviceType, implTypes) =&amp;gt; container.RegisterAll(serviceType, implTypes),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly
);
&lt;/pre&gt;&lt;/div&gt;The previous code snippet registers all types from the given assembly that implement &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; and it registers them as a collection of instances. In other words, with the given example, &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; instances can be retrieved by calling &lt;i&gt;container.GetAllInstances&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;()&lt;/i&gt; or by using an &lt;i&gt;IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; argument in a constructor. However, using an &lt;i&gt;IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; dependency in your constructor or calling into the container directly, are generally not recommended approaches. Depending on a collection of types complicates your application design, and can often be simplified with the right configuration of your container. Your constructors should simply depend on a single &lt;i&gt;IValidator&amp;lt;T&amp;gt;&lt;/i&gt;. A better solution would therefore be to create and inject a &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; in that case:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CompositeValidator&amp;lt;T&amp;gt; : IValidate&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt; validators;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; CompositeValidator(IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt; validators)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validators = validators;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationResults Validate(T instance)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; allResults = ValidationResults.Valid;

        &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; validator &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validators)
        {
            &lt;span style="color:Blue;"&gt;var&lt;/span&gt; results = validator.Validate(instance);
            allResults = ValidationResults.Join(allResults, results);
        }

        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; allResults;
    }
}
&lt;/pre&gt;&lt;/div&gt;This &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; can be registered as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CompositeValidator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This maps the open generic &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; interface to the open generic &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; implementation. Because the &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; contains an &lt;i&gt;IEnumerable&amp;lt;IValidator&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; dependency, the registered types will be injected into its constructor. This allows you to let the rest of the application simply depend on the &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt;, while registering a collection of &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; implementations under the covers.&lt;br /&gt;&lt;br /&gt;The next section will explain mapping of open generic types, as the one we&amp;#39;ve just seen.&lt;br /&gt;
&lt;h3&gt;Registration of open generic types&lt;a name="Registration_Of_Open_Generic_Types"&gt;&lt;/a&gt;&lt;a name="Registration-Of-Open-Generic-Types"&gt;&lt;/a&gt;&lt;/h3&gt;When working with generic interfaces, we will often see many implementations of that interface being registered:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;Customer&amp;gt;, CustomerValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Employee&amp;gt;, EmployeeValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Order&amp;gt;, OrderValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Product&amp;gt;, ProductValidator&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;As the previous section explained, this can be rewritten to the following one-liner:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;Sometimes you&amp;#39;ll find that many implementations of the given generic interface are no-ops. The &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; is a good example, because it is very likely that not all entities will need validation. Still we would like to prevent from having to write an empty validation class per entity or having to write special application logic to check if an entity has validation. In this situation, we will probably want to use the registration as described in the previous section, and fallback to returning the default implementation when no such registration exists for a given type. Such default implementation could look like this, for instance:&lt;br /&gt; &lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Implementation of the Null Object pattern.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;class&lt;/span&gt; NullValidator&amp;lt;T&amp;gt; : IValidate&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationResults Validate(T instance)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; ValidationResults.Valid;
    }
}
&lt;/pre&gt;&lt;/div&gt;We can use this &lt;i&gt;NullValidator&amp;lt;T&amp;gt;&lt;/i&gt; for all entities that don&amp;#39;t need validation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;OrderLine&amp;gt;, NullValidator&amp;lt;OrderLine&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Address&amp;gt;, NullValidator&amp;lt;Address&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;UploadImage&amp;gt;, NullValidator&amp;lt;UploadImage&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Mothership&amp;gt;, NullValidator&amp;lt;Mothership&amp;gt;&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;This is of course not very practical. Falling back to such a default implementation is a typical scenario for &lt;i&gt;unregistered type resolution&lt;/i&gt;. The &lt;b&gt;Simple Injector&lt;/b&gt; contains an event that you can hook to that allows to fallback to a default implementation. On top of this event, the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Methods_T_SimpleInjector_Extensions_OpenGenericRegistrationExtensions.htm"&gt;RegisterOpenGeneric&lt;/a&gt; extension method available. The given situation would be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(NullValidator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;The result of this registration is that for instance an &lt;i&gt;NullValidator&amp;lt;Department&amp;gt;&lt;/i&gt; instance is returned when an &lt;i&gt;IValidate&amp;lt;Department&amp;gt;&lt;/i&gt; is requested.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Because the use of unregistered type resolution, a NullValidator&amp;lt;T&amp;gt; will only get returned for types that are not registered, therefore allowing the default implementation to be overridden for a specific implementation. The &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; method, for instance, doesn’t use unregistered type resolution, but simply registers all concrete types it finds in the given assemblies. Those types will therefore always be returned, giving a very convenient and easy to grasp mix.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Unregistered type resolution&lt;a name="Unregistered_Type_Resolution"&gt;&lt;/a&gt;&lt;a name="Unregistered-Type-Resolution"&gt;&lt;/a&gt;&lt;/h3&gt;Unregistered type resolution is the ability to get notified by the container when a type is requested that is currently unregistered in the container. This gives the user the change of registering that type. The &lt;b&gt;Simple Injector&lt;/b&gt; supports this scenario with the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/E_SimpleInjector_Container_ResolveUnregisteredType.htm"&gt;ResolveUnregisteredType&lt;/a&gt; event. Unregistered type resolution enables many advanced scenarios. The framework itself uses this event for implementing the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Registration_Of_Open_Generic_Types"&gt;registration of open generic types&lt;/a&gt;. Other examples of possible scenarios that can be built on top of this event are &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolving_Arrays_And_Lists"&gt;resolving array and lists&lt;/a&gt; and &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Covariance-Contravariance"&gt;covariance and contravariance&lt;/a&gt;. Those scenarios are described here in the advanced scenarios page.&lt;br /&gt;&lt;br /&gt;For more information about how to use this event, please look at the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/E_SimpleInjector_Container_ResolveUnregisteredType.htm"&gt;ResolveUnregisteredType event documentation&lt;/a&gt; in the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/"&gt;reference library&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;Context based injection&lt;a name="Context_Based_Injection"&gt;&lt;/a&gt;&lt;a name="Contextual_Binding"&gt;&lt;/a&gt;&lt;a name="Context-Based-Injection"&gt;&lt;/a&gt;&lt;/h3&gt;Context based injection is the ability to inject a dependency based on the context it lives in. This context is often supplied by the container. Some DI frameworks contain a feature that allows this, while others don’t. The &lt;b&gt;Simple Injector&lt;/b&gt; does &lt;i&gt;not&lt;/i&gt; does not contain such a feature out of the box, but this ability can easily be added by using the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ContextDependentExtensions&amp;referringTitle=Advanced-scenarios"&gt;context based injection extension method&lt;/a&gt; code snippet.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: In many cases context based injection is not the best solution, and the design should be reevaluated. In some narrow cases however it can make sense.&lt;/i&gt;&lt;/blockquote&gt;
The most common scenario is to base the type of the injected dependency on the type of the consumer. Take for instance the following &lt;i&gt;ILogger&lt;/i&gt; interface with a generic &lt;i&gt;Logger&amp;lt;T&amp;gt;&lt;/i&gt; class that needs to be injected into several consumers. &lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; ILogger
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message);
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Logger&amp;lt;T&amp;gt; : ILogger
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message) { }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Consumer1
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Consumer1(ILogger logger) { }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Consumer2
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Consumer2(ILogger logger) { }
}
&lt;/pre&gt;&lt;/div&gt;In this case we want to inject a &lt;i&gt;Logger&amp;lt;Consumer1&amp;gt;&lt;/i&gt; into &lt;i&gt;Consumer1&lt;/i&gt; and a &lt;i&gt;Logger&amp;lt;Consumer2&amp;gt;&lt;/i&gt; into &lt;i&gt;Consumer2&lt;/i&gt;. By using the previous &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ContextDependentExtensions&amp;referringTitle=Advanced-scenarios"&gt;extension method&lt;/a&gt;, we can accomplish this as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterWithContext&amp;lt;ILogger&amp;gt;(dependencyContext =&amp;gt;
{
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; type = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Logger&amp;lt;&amp;gt;).MakeGenericType(
        dependencyContext.ImplementationType);
    
    &lt;span style="color:Blue;"&gt;return&lt;/span&gt; (ILogger)container.GetInstance(type);
});
&lt;/pre&gt;&lt;/div&gt;In the previous code snippet we registered a &lt;i&gt;Func&amp;lt;DependencyContext, ILogger&amp;gt;&lt;/i&gt; delegate, that will get called each time a &lt;i&gt;ILogger&lt;/i&gt; dependency gets resolved. The &lt;i&gt;DependencyContext&lt;/i&gt; instance that gets supplied to that instance, contains the &lt;i&gt;ServiceType&lt;/i&gt; and &lt;i&gt;ImplementationType&lt;/i&gt; into which the &lt;i&gt;ILogger&lt;/i&gt; is getting injected.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Although building a generic type using MakeGenericType is relatively slow, the call to the Func&amp;lt;DependencyContext, TService&amp;gt; delegate itself is about as cheap as calling a Func&amp;lt;TService&amp;gt; delegate. If performance of the MakeGenericType gets a problem, you can always cache the generated types, cache InstanceProducer instances, or cache ILogger instances (note that caching the ILogger instances will make them singletons).&lt;/i&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Even though the use of a generic Logger&amp;lt;T&amp;gt; is a common design (with log4net as the grand godfather of this design), doesn&amp;#39;t always make it a good design. The need for having the logger contain information about its parent type, might indicate design problems. If you&amp;#39;re doing this, please take a look at &lt;a href="http://stackoverflow.com/a/9915056/264697"&gt;this Stackoverflow answer&lt;/a&gt;. It talks about logging in conjunction with the SOLID design principles.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Decorators&lt;a name="Decorators"&gt;&lt;/a&gt;&lt;a name="Generic_Decorators"&gt;&lt;/a&gt;&lt;/h3&gt;The &lt;a href="http://en.wikipedia.org/wiki/SOLID"&gt;SOLID&lt;/a&gt; principles give us important guidance when it comes to writing maintainable software. The &amp;#39;O&amp;#39; of the &amp;#39;SOLID&amp;#39; acronym stands for the &lt;a href="http://en.wikipedia.org/wiki/Open/closed_principle"&gt;Open/closed Principle&lt;/a&gt; which states that classes should be open for extension, but closed for modification. Designing systems around the Open/closed principle means that new behavior can be plugged into the system, without the need to change any existing parts, making the change of breaking existing code much smaller.&lt;br /&gt;&lt;br /&gt;One of the ways to add new functionality (such as &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=http%3a%2f%2fen.wikipedia.org%2fwiki%2fCross-cutting_concern&amp;referringTitle=Advanced-scenarios"&gt;cross-cutting concerns&lt;/a&gt;) to types is by the use of the &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;decorator pattern&lt;/a&gt;. The decorator pattern can be used to extend (decorate) the functionality of a certain object at run-time. Especially when using generic interfaces, the concept of decorators gets really powerful. Take for instance the examples given in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Registration_Of_Open_Generic_Types"&gt;Registration of open generic types&lt;/a&gt; section of this page or for instance the use of an generic &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; interface.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: &lt;a href="http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91"&gt;This article&lt;/a&gt; describes an architecture based on the use of the ICommandHandler&amp;lt;TCommand&amp;gt; interface.&lt;/i&gt;&lt;/blockquote&gt;
Take the plausible scenario where we want to validate all commands that get executed by an &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; implementation. The Open/Closed principle states that we want to do this, without having to alter each and every implementation. We can do this using a (single) decorator:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;
    : ICommandHandler&amp;lt;TCommand&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IValidator validator;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ICommandHandler&amp;lt;TCommand&amp;gt; handler;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationCommandHandlerDecorator(
        IValidator validator, 
        ICommandHandler&amp;lt;TCommand&amp;gt; handler)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validator = validator;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handler = handler;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; ICommandHandler&amp;lt;TCommand&amp;gt;.Handle(TCommand command)
    {
        &lt;span style="color:Green;"&gt;// validate the supplied command (throws when invalid).&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validator.ValidateObject(command);
		
        &lt;span style="color:Green;"&gt;// forward the (valid) command to the real&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// command handler.&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handler.Handle(command);
    }
}
&lt;/pre&gt;&lt;/div&gt;The &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; class is an implementation of the &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; interface, but it also wraps / decorates an &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; instance. Instead of injecting the real implementation directly into a consumer, we can (let Simple Injector) inject a validator decorator that wraps the real implementation.&lt;br /&gt;&lt;br /&gt;The &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; depends on an &lt;i&gt;IValidator&lt;/i&gt; interface. An implementation that used Microsoft Data Annotations might look like this:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.ComponentModel.DataAnnotations;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; DataAnnotationsValidator : IValidator
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IServiceProvider container;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; DataAnnotationsValidator(Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }
    
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IValidator.ValidateObject(&lt;span style="color:Blue;"&gt;object&lt;/span&gt; instance)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; context = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ValidationContext(instance,
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container, &lt;span style="color:Blue;"&gt;null&lt;/span&gt;);

        &lt;span style="color:Green;"&gt;// Throws an exception when instance is invalid.&lt;/span&gt;
        Validator.ValidateObject(instance, context);    
    }
}
&lt;/pre&gt;&lt;/div&gt;The implementations of the &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt; interface can be registered using the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_OpenGenericBatchRegistrationExtensions_RegisterManyForOpenGeneric.htm"&gt;RegisterManyForOpenGeneric&lt;/a&gt; extension method:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;By using the following extension method from the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace, you can wrap the &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; around each and every &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; implementation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;Multiple decorators can be wrapped by calling the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterDecorator.htm"&gt;RegisterDecorator&lt;/a&gt; method multiple times, as the following registration shows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TransactionCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DeadlockRetryCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;The decorators are applied in the order in which they are registered, which means that the first decorator (&lt;i&gt;TransactionCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; in this case) wraps the real instance, the second decorator (&lt;i&gt;DeadlockRetryCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; in this case) wraps the first decorator, and so on.&lt;br /&gt;&lt;br /&gt;There&amp;#39;s an overload of the &lt;b&gt;RegisterDecorator&lt;/b&gt; available that allows you to supply a predicate to determine whether that decorator should be applied to a specific service type. Using a given context you can determine whether the decorator should be applied. Here is an example:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AccessValidationCommandHandlerDecorator&amp;lt;&amp;gt;),
    context =&amp;gt; !context.ImplementationType.Namespace.EndsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Admins&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;The given context contains several properties that allows you to analyze whether a decorator should be applied to a given service type, such as the current closed generic service type (using the &lt;i&gt;ServiceType&lt;/i&gt; property) and the concrete type that will be created (using the &lt;i&gt;ImplementationType&lt;/i&gt; property). The predicate will (under normal circumstances) be called only once per generic type, so there is no performance implication for using it.&lt;br /&gt;
&lt;h4&gt;Decorators with Func&amp;lt;T&amp;gt; factories&lt;a name="Decorators_With_Func_Factories"&gt;&lt;/a&gt;&lt;/h4&gt;In certain scenarios, it is needed to postpone building part of the object graph. For instance when a service needs to control the lifetime of a dependency, need multiple instances, when instances need to be &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Multi_Threaded_Applications"&gt;executed on a different thread&lt;/a&gt;, or when instances need to be created in a certain &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Scoped"&gt;scope&lt;/a&gt; or (security) context.&lt;br /&gt;&lt;br /&gt;When building a &amp;#39;normal&amp;#39; object graph with dependencies, you can easily delay building a part of the graph by letting a service depend on a factory. This allows building that part of the object graph to be postponed until the time the type starts using the factory. When working with decorators however, injecting a factory to postpone the creation of the decorated instance will not work. Take for instance a &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; that allows executing a command handler on a different thread. We could let the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; depend on a &lt;i&gt;CommandHandlerFactory&amp;lt;T&amp;gt;&lt;/i&gt;, and let this factory call back into the container to retrieve a new &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt;. Unfortunately this would fail, since requesting an &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt; would again wrap this instance with a new &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt;, and we&amp;#39;d end up causing a stack overflow.&lt;br /&gt;&lt;br /&gt;Since this is a scenario that is really hard to solve without framework support, &lt;b&gt;Simple Injector&lt;/b&gt; allows injecting a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate into registered decorators. This delegate functions as a factory for the creation of the decorated instance. Taking the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; as example, it could be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;
    : ICommandHandler&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;ICommandHandler&amp;lt;T&amp;gt;&amp;gt; factory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AsyncCommandHandlerDecorator(
        Func&amp;lt;ICommandHandler&amp;lt;T&amp;gt;&amp;gt; factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
	
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(T command)
    {
        &lt;span style="color:Green;"&gt;// Execute on different thread.&lt;/span&gt;
        ThreadPool.QueueUserWorkItem(_ =&amp;gt;
        {
            &lt;span style="color:Green;"&gt;// Create new handler in this thread.&lt;/span&gt;
            &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory();
            handler.Handle(command)
        });
    }
}
&lt;/pre&gt;&lt;/div&gt;This special decorator can be registered just as any other decorator:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;However, since the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; solely has singleton dependencies (the &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; is a singleton), and creates a new decorated instance each time it’s called, we can even register it as a singleton itself:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;When mixing this with other (synchronous) decorators, you&amp;#39;ll get an extremely powerful and pluggable system:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TransactionCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DeadlockRetryCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterSingleDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This configuration has an interesting mix of decorator registrations. The registration of the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; allows (some of) the command handlers to be executed on the background (while others -who&amp;#39;s name does not start with &amp;#39;Async&amp;#39;- still run synchronously), but before execution, all commands are validated synchronously (to allow communicating validation errors to the caller). And all handlers (even the asynchronous ones) are executed in a transaction and the operation is retried when the database rolled back because of a deadlock).&lt;br /&gt;
&lt;h4&gt;Decorated collections&lt;a name="Decorated_Collections"&gt;&lt;/a&gt;&lt;/h4&gt;When registering a decorator, Simple Injector will automatically decorate any collection with elements of that service type:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterAll&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt;(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CustomerMovedEventHandler),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(NotifyStaffWhenCustomerMovedEventHandler));
    
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationEventHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; SomeCondition);
&lt;/pre&gt;&lt;/div&gt;The previous registration registers a collection of &lt;i&gt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&lt;/i&gt; services. Those services are decorated with a &lt;i&gt;ValidationEventHandlerDecorator&amp;lt;TEvent&amp;gt;&lt;/i&gt; when the supplied predicate holds.&lt;br /&gt;&lt;br /&gt;For collections of elements that are created by the container (container controlled), the predicate is checked for each element in the collection. For collections of uncontrolled elements (a list of items that is not created by the container), the predicate is checked once for the whole collection. This means that controlled collections can be partially decorated. Taking the previous example for instance, you could let the &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; be decorated, while leaving the &lt;i&gt;NotifyStaffWhenCustomerMovedEventHandler&lt;/i&gt; undecorated (determined by the supplied predicate).&lt;br /&gt;&lt;br /&gt;When a collection is uncontrolled, it means that the lifetime of its elements are unknown to the container. The following registration is an example of an uncontrolled collection:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
IEnumerable&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt; handlers =
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;[]
    {
        &lt;span style="color:Blue;"&gt;new&lt;/span&gt; CustomerMovedEventHandler(),
        &lt;span style="color:Blue;"&gt;new&lt;/span&gt; NotifyStaffWhenCustomerMovedEventHandler(),
    };

container.RegisterAll&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt;(handlers);
&lt;/pre&gt;&lt;/div&gt;Although this registration contains a list of singletons, the container has no way of knowing this. The collection could easily have been a dynamic (an ever changing) collection. In this case, the container calls the registered predicate once (and supplies the predicate with the &lt;i&gt;IEventHandler&amp;lt;CusotmerMovedEvent&amp;gt;&lt;/i&gt; type)  and if the predicate returns true, each element in the collection is decorated with a decorator instance.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Warning&lt;/b&gt;: In general you should prevent registering uncontrolled collections. The container knows nothing about them, and can&amp;#39;t help you in doing &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;diagnostics&lt;/a&gt;. Since the lifetime of those items is unknown, the container will be unable to wrap a decorator with a lifestyle other than transient. Best practice is to register container-controlled collections which is done by using one of the &lt;b&gt;RegisterAll&lt;/b&gt; overloads that take a collection of &lt;b&gt;System.Type&lt;/b&gt; instances.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Interception&lt;a name="Interception"&gt;&lt;/a&gt;&lt;/h3&gt;Interception is the ability to intercept a call from a consumer to a service, and add or change behavior. The &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;decorator pattern&lt;/a&gt; describes a form of interception, but when it comes to applying cross-cutting concerns, you might end up writing decorators for many service interfaces, but with the exact same code. If this is happening, it is time to explore the possibilities of interception.&lt;br /&gt;&lt;br /&gt;Using the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets, you can add the ability to do interception with the &lt;b&gt;Simple Injector&lt;/b&gt;. Using the given code, you can for instance define a &lt;i&gt;MonitoringInterceptor&lt;/i&gt; that allows logging the execution time of the called service method:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MonitoringInterceptor : IInterceptor
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;

    &lt;span style="color:Green;"&gt;// Using constructor injection on the interceptor&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MonitoringInterceptor(ILogger logger)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = logger;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Intercept(IInvocation invocation)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; watch = Stopwatch.StartNew();

        &lt;span style="color:Green;"&gt;// Calls the decorated instance.&lt;/span&gt;
        invocation.Proceed();

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; decoratedType =
            invocation.InvocationTarget.GetType();
        
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger.Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt;.Format(
            &lt;span style="color:#A31515;"&gt;&amp;quot;{0} executed in {1} ms.&amp;quot;&lt;/span&gt;,
            decoratedType.Name,
            watch.ElapsedMiliseconds));
    }
}
&lt;/pre&gt;&lt;/div&gt;This interceptor can be registered to be wrapped around a concrete implementation. Using the given extension methods, this can be done as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.InterceptWith&amp;lt;MonitoringInterceptor&amp;gt;(type =&amp;gt; type == &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IUserRepository));
&lt;/pre&gt;&lt;/div&gt;This registration ensures that every time an &lt;i&gt;IUserRepository&lt;/i&gt; interface is requested, an interception proxy is returned that wraps that instance and uses the &lt;i&gt;MonitoringInterceptor&lt;/i&gt; to extend the behavior.&lt;br /&gt;&lt;br /&gt;The current example doesn&amp;#39;t add much compared to simply using a decorator. When having many interface service types that need to be decorated with the same behavior however, it gets different:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.InterceptWith&amp;lt;MonitoringInterceptor&amp;gt;(t =&amp;gt; t.Name.EndsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Repository&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets use .NET&amp;#39;s &lt;/i&gt;System.Runtime.Remoting.Proxies.RealProxy&lt;i&gt; class to generate interception proxies. The &lt;/i&gt;RealProxy&lt;i&gt; only allows to proxy interfaces.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: the interfaces in the given &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets are a simplified version of the Castle Project interception facility. If you need to create lots different interceptors, you might benefit from using the interception abilities of the Castle Project. Also please note that the given snippets use dynamic proxies to do the interception, while Castle uses lightweight code generation (LCG). LCG allows much better performance than the use of dynamic proxies.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Don&amp;#39;t use interception for intercepting types that all implement the same generic interface, such as ICommandHandler&amp;lt;T&amp;gt; or IValidator&amp;lt;T&amp;gt;. Try using decorator classes instead, as shown in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Decorators"&gt;Decorators&lt;/a&gt; section on this page.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Property injection&lt;a name="Implicit_Property_Injection"&gt;&lt;/a&gt;&lt;a name="Implicit-Property-Injection"&gt;&lt;/a&gt;&lt;a name="Property-Injection"&gt;&lt;/a&gt;&lt;/h3&gt;Simple Injector does not inject any properties into types that get resolved by the container. In general there are two ways of doing property injection, and both are not supported by default for reasons explained below.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Implicit property injection&lt;/b&gt;&lt;br /&gt;Some containers (such as Castle Windsor) implicitly inject public writable properties by default for any instance you resolve. They do this by mapping those properties to configured types. When no such registration exists, or when the property doesn’t have a public setter, the property will be skipped. Simple Injector does not do this, and for good reason. We think that &lt;i&gt;implicit property injection&lt;/i&gt; is simply too uuhh...  implicit :-). Silently skipping properties that can&amp;#39;t be mapped can lead to a DI configuration that can&amp;#39;t be verified easily and can therefore result in an application that fails at runtime instead of failing when the container is verified.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Explicit property injection&lt;/b&gt;&lt;br /&gt;We strongly feel that explicit property injection is a much better way to go. With explicit property injection the container is forced to inject a property and the process will fail immediately when a property can&amp;#39;t be mapped or injected. Some containers (such as Unity and Ninject) allow explicit property injection by default by allowing properties to be decorated with attributes. Problem with this is that this forces the application to take a dependency on the framework, which is something that should be prevented.&lt;br /&gt;&lt;br /&gt;Because Simple Injector does not encourage its users to take a dependency on the container (except for the startup path of course), Simple Injector does not contain any attributes that allow explicit property injection and it can therefore not explicitly inject properties out-of-the-box.&lt;br /&gt;&lt;br /&gt;Besides this, the use of property injection should be very exceptional and in general constructor injection should always be used. If a constructor gets too many parameters (constructor over-injection anti-pattern), it is an indication of a violation of the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; (SRP). SRP violations often lead to maintainability issues. So instead of patching constructor over-injection with property injection, the root cause should be analyzed and the type should be refactored, probably with &lt;a href="http://blog.ploeh.dk/2010/02/02/RefactoringtoAggregateServices/"&gt;Facade Services&lt;/a&gt;. Another common reason to use properties is because those dependencies are optional. Instead of using optional property dependencies, best practice is to inject empty implementations (a.k.a. &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object pattern&lt;/a&gt;) into the constructor.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Enabling property injection&lt;/b&gt;&lt;br /&gt;Simple Injector contains two ways to enable property injection. First of all the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Configuring_Property_Injection"&gt;RegisterInitializer&amp;#60;T&amp;#62;&lt;/a&gt; method can be used to inject properties (especially configuration values) on a per-type basis. Take for instance the following code snippet:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterInitializer&amp;lt;HandlerBase&amp;gt;(handlerToInitialize =&amp;gt;
{
    handlerToInitialize.ExecuteAsynchronously = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;
});
&lt;/pre&gt;&lt;/div&gt;In the previous example a &lt;i&gt;Action&amp;lt;T&amp;gt;&lt;/i&gt; delegate is registered that will be called every time the container creates a type that inherits from &lt;i&gt;HandlerBase&lt;/i&gt;. In this case, the handler will set a configuration value on that class.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: although this method can also be used injecting services, please note that the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;Diagnostic Services&lt;/a&gt; will be unable to see and analyze this dependency.&lt;/i&gt;&lt;/blockquote&gt;
The second way to inject properties is by implementing a custom &lt;b&gt;IPropertySelectionBehavior&lt;/b&gt;. The &lt;i&gt;property selection behavior&lt;/i&gt; is a general extension point provided by the container, to override the library&amp;#39;s default behavior. The following code for instance enables explicit property injection using attributes, using any customly defined attribute:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Reflection;

&lt;span style="color:Blue;"&gt;using&lt;/span&gt; SimpleInjector.Advanced;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AttributedPropertyInjectionExtensions
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; AutowirePropertiesWithAttribute&amp;lt;TAttribute&amp;gt;(
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt; ContainerOptions options)
        &lt;span style="color:Blue;"&gt;where&lt;/span&gt; TAttribute : Attribute
    {
        options.PropertySelectionBehavior =
            &lt;span style="color:Blue;"&gt;new&lt;/span&gt; AttributePropertyInjectionBehavior(
                options.PropertySelectionBehavior, 
                &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TAttribute));
    }

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AttributePropertyInjectionBehavior 
        : IPropertySelectionBehavior
    {
        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IPropertySelectionBehavior behavior;
        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Type attribute;

        &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AttributePropertyInjectionBehavior(
            IPropertySelectionBehavior baseBehavior, 
            Type attributeType)
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.behavior = baseBehavior;
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.attribute = attributeType;
        }

        &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;bool&lt;/span&gt; SelectProperty(Type type, PropertyInfo prop)
        {
            &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.IsPropertyDecorated(prop) ||
                &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.behavior.SelectProperty(type, prop);
        }

        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;bool&lt;/span&gt; IsPropertyDecorated(PropertyInfo p)
        {
            &lt;span style="color:Blue;"&gt;return&lt;/span&gt; p.GetCustomAttributes(&lt;span style="color:Blue;"&gt;this&lt;/span&gt;.attribute, &lt;span style="color:Blue;"&gt;true&lt;/span&gt;).Any();
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;The previous code snippet can be used as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();
container.Options.AutowirePropertiesWithAttribute&amp;lt;MyInjectAttribute&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;This enables explicit property injection on all properties that are marked with the &lt;b&gt;MyInjectAttribute&lt;/b&gt; and it will fail when the property cannot be injected for whatever reason.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: Properties injected by the container through the &lt;b&gt;IPropertySelectionBehavior&lt;/b&gt; will be analyzed by the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;Diagnostic Services&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;_&lt;b&gt;Note&lt;/b&gt;: The &lt;b&gt;IPropertySelectionBehavior&lt;/b&gt; extension mechanism can also be used to implement implicit property injection. Doing so however is not advised because of the reasons given above.&lt;/blockquote&gt;
&lt;h3&gt;Covariance and Contravariance&lt;a name="Covariance_Contravariance"&gt;&lt;/a&gt;&lt;a name="Covariance-Contravariance"&gt;&lt;/a&gt;&lt;/h3&gt;Since version 4.0 of the .NET framework, the type system allows &lt;a href="http://msdn.microsoft.com/en-us/library/dd799517.aspx"&gt;Covariance and Contravariance in Generics&lt;/a&gt; (especially interfaces and delegates). This allows for instance, to use a &lt;i&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/i&gt; as an &lt;i&gt;IEnumerable&amp;lt;object&amp;gt;&lt;/i&gt; (covariance), or to use an &lt;i&gt;Action&amp;lt;object&amp;gt;&lt;/i&gt; as an &lt;i&gt;Action&amp;lt;string&amp;gt;&lt;/i&gt; (contravariance).&lt;br /&gt;&lt;br /&gt;In some circumstances, the application design can benefit from the use of covariance and contravariance (or variance for short) and it would be beneficial when the IoC container returns services that are &amp;#39;compatible&amp;#39; to the requested service, even although the requested service is not registered. To stick with the previous example, the container could return an &lt;i&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/i&gt; even when an &lt;i&gt;IEnumerable&amp;lt;object&amp;gt;&lt;/i&gt; is requested.&lt;br /&gt;&lt;br /&gt;By default, the Simple Injector does not return variant implementations of given services, but the Simple Injector can be extended to behave this way. The actual way to write this extension depends on the requirements of the application.&lt;br /&gt;&lt;br /&gt;Take a look at the following application design around the &lt;i&gt;IEventHandler&amp;lt;in TEvent&amp;gt;&lt;/i&gt; interface:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IEventHandler&amp;lt;&lt;span style="color:Blue;"&gt;in&lt;/span&gt; TEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(TEvent e);
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedEvent 
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;int&lt;/span&gt; CustomerId { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Address NewAddress { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedAbroadEvent : CustomerMovedEvent
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Country Country { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedEventHandler : IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(CustomerMovedEvent e) { ... }
}
&lt;/pre&gt;&lt;/div&gt;The design contains two event classes &lt;i&gt;CustomerMovedEvent&lt;/i&gt; and &lt;i&gt;CustomerMovedAbroadEvent&lt;/i&gt; (where &lt;i&gt;CustomerMovedAbroadEvent&lt;/i&gt; inherits from &lt;i&gt;CustomerMovedEvent&lt;/i&gt;) one concrete event handler &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; and a generic interface for event handlers.&lt;br /&gt;&lt;br /&gt;We can configure the container in such way that not only a request for &lt;i&gt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&lt;/i&gt; results in a &lt;i&gt;CustomerMovedEventHandler,&lt;/i&gt; but also a request for &lt;i&gt;IEventHandler&amp;lt;CustomerMovedAbroadEvent&amp;gt;&lt;/i&gt; results in that same &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; (because &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; also accepts &lt;i&gt;CustomerMovedAbroadEvents&lt;/i&gt;).&lt;br /&gt;&lt;br /&gt;There are several ways to achieve this, but one of the ways is the following:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;CustomerMovedEventHandler&amp;gt;();

container.RegisterSingleOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ContravarianceEventHandler&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This registration depends on the custom &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; that should be placed close to the registration itself:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ContravarianceEventHandler&amp;lt;TEvent&amp;gt;
    : IEventHandler&amp;lt;TEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; Func&amp;lt;IEventHandler&amp;lt;TEvent&amp;gt;&amp;gt; handlerFactory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ContravarianceEventHandler(Container container)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handlerType = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;TEvent&amp;gt;);

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; registration = (
            &lt;span style="color:Blue;"&gt;from&lt;/span&gt; r &lt;span style="color:Blue;"&gt;in&lt;/span&gt; container.GetCurrentRegistrations()
            &lt;span style="color:Blue;"&gt;let&lt;/span&gt; assignableInterfaces = (
                &lt;span style="color:Blue;"&gt;from&lt;/span&gt; iface &lt;span style="color:Blue;"&gt;in&lt;/span&gt; r.ServiceType.GetInterfaces()
                &lt;span style="color:Blue;"&gt;where&lt;/span&gt; handlerType.IsAssignableFrom(iface)
                &lt;span style="color:Blue;"&gt;select&lt;/span&gt; iface)
            &lt;span style="color:Blue;"&gt;where&lt;/span&gt; assignableInterfaces.Any()
            &lt;span style="color:Blue;"&gt;select&lt;/span&gt; r)
            .Single();

        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handlerFactory =
            () =&amp;gt; (IEventHandler&amp;lt;TEvent&amp;gt;)r.GetInstance();
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IEventHandler&amp;lt;TEvent&amp;gt;.Handle(TEvent e)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handlerFactory().Handle(e);
    }
}
&lt;/pre&gt;&lt;/div&gt;The registration ensures that every time an &lt;i&gt;IEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; is requested, a &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; is returned. The &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; will on creation query the container for a single service type that implements &lt;i&gt;IEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt;. Because the &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; is not registered by its interface, but by itself, the &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; will find that type.&lt;br /&gt;&lt;br /&gt;This is just one example and one way of adding variance support. For a more elaborate discussion on this subject, please read the following article: &lt;a href="http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=90"&gt;Adding Covariance and Contravariance to the Simple Injector&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;Registering plugins dynamically&lt;a name="Plugins"&gt;&lt;/a&gt;&lt;/h3&gt;Applications with a plugin architecture often allow special plugin assemblies to be dropped in a special folder and to be picked up by the application, without the need of a recompile. Although Simple Injector does not support this out of the box, registering plugins from dynamically loaded assemblies can be implemented in a few lines of code. Here is an example:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;string&lt;/span&gt; pluginDirectory =
    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, &lt;span style="color:#A31515;"&gt;&amp;quot;Plugins&amp;quot;&lt;/span&gt;);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; pluginAssemblies =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; file &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; DirectoryInfo(pluginDirectory).GetFiles()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; file.Extension == &lt;span style="color:#A31515;"&gt;&amp;quot;.dll&amp;quot;&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; Assembly.LoadFile(file.FullName);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; pluginTypes =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; dll &lt;span style="color:Blue;"&gt;in&lt;/span&gt; pluginAssemblies
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; type &lt;span style="color:Blue;"&gt;in&lt;/span&gt; dll.GetExportedTypes()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IPlugin).IsAssignableFrom(type)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; !type.IsAbstract
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; !type.IsGenericTypeDefinition
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; type;

container.RegisterAll&amp;lt;IPlugin&amp;gt;(pluginTypes);
&lt;/pre&gt;&lt;/div&gt;The given example makes use of an &lt;i&gt;IPlugin&lt;/i&gt; interface that is known to the application, and probably located in a shared assembly. The dynamically loaded plugin .dll files can contain multiple classes that implement &lt;i&gt;IPlugin&lt;/i&gt;, and all publicly exposed concrete types that implements &lt;i&gt;IPlugin&lt;/i&gt; will be registered using the &lt;i&gt;RegisterAll&lt;/i&gt; method and can get resolved using the default auto-wiring behavior of the container, meaning that the plugin must have a single public constructor and all constructor arguments must be resolvable by the container. The plugins can get resolved using &lt;i&gt;container.GetAllInstances&amp;lt;IPlugin&amp;gt;()&lt;/i&gt; or by adding an &lt;i&gt;IEnumerable&amp;lt;IPlugin&amp;gt;&lt;/i&gt; argument to a constructor.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Resolving instances by key&lt;a name="Resolving_Instances_By_Key"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolve_Instances_By_Key"&gt;Resolve Instances By Key&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios"&gt;How to&lt;/a&gt; documentation page, for the information you are looking for.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Resolving array and lists&lt;a name="Resolving_Arrays_And_Lists"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolve_Arrays_And_Lists"&gt;Resolve Arrays and Lists&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios"&gt;How to&lt;/a&gt; page, for the information you are looking for.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Lifetime scoping&lt;a name="Lifetime_scoping"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#PerLifetimeScope"&gt;Per Lifetime Scope&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios"&gt;Object Lifestyle Management&lt;/a&gt; documentation page for more information.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Thu, 04 Apr 2013 09:40:16 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Advanced-scenarios 20130404094016A</guid></item><item><title>Updated Wiki: Advanced-scenarios</title><link>https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;version=51</link><description>&lt;div class="wikidoc"&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: this documentation is specific for &lt;b&gt;Simple Injector version 2.0&lt;/b&gt; and up. Look &lt;a href="http://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;amp;version=48"&gt;here&lt;/a&gt; for 1.x specific documentation.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h2&gt;Advanced scenarios with the Simple Injector&lt;/h2&gt;
Although its name might not indicate it, &lt;b&gt;Simple Injector&lt;/b&gt; is capable of handling many complex scenarios. Sometimes by writing some custom code, taking code from this wiki, or by using the extension points from the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace of the core library.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: After including the &lt;b&gt;SimpleInjector.dll&lt;/b&gt; to your project, you will have to add the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace to your code to be able to use most features that are presented in this wiki page.&lt;/i&gt;&lt;/blockquote&gt;
This page discusses the following subjects:
&lt;ul&gt;&lt;li&gt;&lt;a href="#Batch-Registration"&gt;Batch registration &amp;#47; Automatic registration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Registration-Of-Open-Generic-Types"&gt;Registration of open generic types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Unregistered-Type-Resolution"&gt;Unregistered type resolution&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Context-Based-Injection"&gt;Context based injection &amp;#47; Contextual binding&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="#Decorators"&gt;Decorators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Interception"&gt;Interception&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Property-Injection"&gt;Property injection&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Covariance-Contravariance"&gt;Covariance and Contravariance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#Plugins"&gt;Registering plugins dynamically&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Batch registration / Automatic registration&lt;a name="Batch_Registration"&gt;&lt;/a&gt;&lt;a name="Batch-Registration"&gt;&lt;/a&gt;&lt;/h3&gt;Batch registration or automatic registration is a way to register all types in a given source based on some sort of convention. This saves you from adding a new line to your configuration for each new type that you register, as in the following example: &lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IUserRepository, SqlUserRepository&amp;gt;();
container.Register&amp;lt;ICustomerRepository, SqlCustomerRepository&amp;gt;();
container.Register&amp;lt;IOrderRepository, SqlOrderRepository&amp;gt;();
container.Register&amp;lt;IProductRepository, SqlProductRepository&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;To prevent having to change the container configuration too often, we can use the non-generic registration overloads, combined with a simple LINQ query:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; repositoryAssembly = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(SqlUserRepository).Assembly;

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; registrations =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; type &lt;span style="color:Blue;"&gt;in&lt;/span&gt; repositoryAssembly.GetExportedTypes()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; type.Namespace == &lt;span style="color:#A31515;"&gt;&amp;quot;MyComp.MyProd.BL.SqlRepositories&amp;quot;&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; type.GetInterfaces().Any()
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt;
    {
        Service = type.GetInterfaces().Single(),
        Implementation = type
    };

&lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; reg &lt;span style="color:Blue;"&gt;in&lt;/span&gt; registrations)
{
    container.Register(reg.Service, reg.Implementation, Lifestyle.Transient);
}
&lt;/pre&gt;&lt;/div&gt;Although many DI frameworks contain an advanced API for doing convention based registration, we found that doing this with custom LINQ queries is often easier to write, easier to understand, and even more flexible than using such an API.&lt;br /&gt;&lt;br /&gt;Another interesting scenario is that of registering implementations of some generic interface. Say for instance that your application contains the following interface:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IValidate&amp;lt;T&amp;gt;
{
    ValidationResults Validate(T instance);
}
&lt;/pre&gt;&lt;/div&gt;Your application might contain many implementations of this interface, for instance for validating customers, employees, orders, products, etc. You would normally end up with a registration as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;Customer&amp;gt;, CustomerValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Employee&amp;gt;, EmployeeValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Order&amp;gt;, OrderValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Product&amp;gt;, ProductValidator&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;Using the extension methods for batch registration of open generic types from the &lt;i&gt;SimpleInjector.Extesions&lt;/i&gt; namespace however, you can write it down to a single line:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;This call searches in the supplied assembly for all public types that implement the &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; interface and it registers each type by their specific interface (closed generic) interface. It even works for types that implement multiple closed versions of the given interface.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: There are &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_OpenGenericBatchRegistrationExtensions_RegisterManyForOpenGeneric.htm"&gt;overloads&lt;/a&gt; available that take a list of &lt;b&gt;System.Type&lt;/b&gt; instances, instead a list of &lt;b&gt;Assembly&lt;/b&gt; instances.&lt;/i&gt;&lt;/blockquote&gt;
This however is just one example of what you can do using batch registration. A more advanced scenario is the registration of multiple implementations of the same closed generic type to a single interface. Because there are many possible variations of this scenario, &lt;b&gt;Simple Injector&lt;/b&gt; does not contain any methods to do this. It does contain however, multiple overloads of the &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; that allows you to supply a callback delegate that lets you do the registration yourself. Take for instance the scenario where you have a &lt;i&gt;CustomerValidator&lt;/i&gt; and a &lt;i&gt;GoldCustomerValidator&lt;/i&gt; type, that each implement &lt;i&gt;IValidate&amp;lt;Customer&amp;gt;&lt;/i&gt;, and you want to register both of them. The previous registration would throw an exception telling you that you have multiple types implementing the same closed generic type. The following registration however, does enable this scenario:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;),
    AccessibilityOption.PublicTypesOnly,
    (serviceType, implTypes) =&amp;gt; container.RegisterAll(serviceType, implTypes),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly
);
&lt;/pre&gt;&lt;/div&gt;The previous code snippet registers all types from the given assembly that implement &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; and it registers them as a collection of instances. In other words, with the given example, &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; instances can be retrieved by calling &lt;i&gt;container.GetAllInstances&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;()&lt;/i&gt; or by using an &lt;i&gt;IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; argument in a constructor. However, using an &lt;i&gt;IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; dependency in your constructor or calling into the container directly, are generally not recommended approaches. Depending on a collection of types complicates your application design, and can often be simplified with the right configuration of your container. Your constructors should simply depend on a single &lt;i&gt;IValidator&amp;lt;T&amp;gt;&lt;/i&gt;. A better solution would therefore be to create and inject a &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; in that case:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CompositeValidator&amp;lt;T&amp;gt; : IValidate&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt; validators;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; CompositeValidator(IEnumerable&amp;lt;IValidate&amp;lt;T&amp;gt;&amp;gt; validators)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validators = validators;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationResults Validate(T instance)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; allResults = ValidationResults.Valid;

        &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; validator &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validators)
        {
            &lt;span style="color:Blue;"&gt;var&lt;/span&gt; results = validator.Validate(instance);
            allResults = ValidationResults.Join(allResults, results);
        }

        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; allResults;
    }
}
&lt;/pre&gt;&lt;/div&gt;This &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; can be registered as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CompositeValidator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This maps the open generic &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; interface to the open generic &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; implementation. Because the &lt;i&gt;CompositeValidator&amp;lt;T&amp;gt;&lt;/i&gt; contains an &lt;i&gt;IEnumerable&amp;lt;IValidator&amp;lt;T&amp;gt;&amp;gt;&lt;/i&gt; dependency, the registered types will be injected into its constructor. This allows you to let the rest of the application simply depend on the &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt;, while registering a collection of &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; implementations under the covers.&lt;br /&gt;&lt;br /&gt;The next section will explain mapping of open generic types, as the one we&amp;#39;ve just seen.&lt;br /&gt;
&lt;h3&gt;Registration of open generic types&lt;a name="Registration_Of_Open_Generic_Types"&gt;&lt;/a&gt;&lt;a name="Registration-Of-Open-Generic-Types"&gt;&lt;/a&gt;&lt;/h3&gt;When working with generic interfaces, we will often see many implementations of that interface being registered:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;Customer&amp;gt;, CustomerValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Employee&amp;gt;, EmployeeValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Order&amp;gt;, OrderValidator&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Product&amp;gt;, ProductValidator&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;As the previous section explained, this can be rewritten to the following one-liner:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;Sometimes you&amp;#39;ll find that many implementations of the given generic interface are no-ops. The &lt;i&gt;IValidate&amp;lt;T&amp;gt;&lt;/i&gt; is a good example, because it is very likely that not all entities will need validation. Still we would like to prevent from having to write an empty validation class per entity or having to write special application logic to check if an entity has validation. In this situation, we will probably want to use the registration as described in the previous section, and fallback to returning the default implementation when no such registration exists for a given type. Such default implementation could look like this, for instance:&lt;br /&gt; &lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Implementation of the Null Object pattern.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;class&lt;/span&gt; NullValidator&amp;lt;T&amp;gt; : IValidate&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationResults Validate(T instance)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; ValidationResults.Valid;
    }
}
&lt;/pre&gt;&lt;/div&gt;We can use this &lt;i&gt;NullValidator&amp;lt;T&amp;gt;&lt;/i&gt; for all entities that don&amp;#39;t need validation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IValidate&amp;lt;OrderLine&amp;gt;, NullValidator&amp;lt;OrderLine&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Address&amp;gt;, NullValidator&amp;lt;Address&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;UploadImage&amp;gt;, NullValidator&amp;lt;UploadImage&amp;gt;&amp;gt;();
container.Register&amp;lt;IValidate&amp;lt;Mothership&amp;gt;, NullValidator&amp;lt;Mothership&amp;gt;&amp;gt;();
&lt;span style="color:Green;"&gt;// and the list goes on...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;This is of course not very practical. Falling back to such a default implementation is a typical scenario for &lt;i&gt;unregistered type resolution&lt;/i&gt;. The &lt;b&gt;Simple Injector&lt;/b&gt; contains an event that you can hook to that allows to fallback to a default implementation. On top of this event, the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Methods_T_SimpleInjector_Extensions_OpenGenericRegistrationExtensions.htm"&gt;RegisterOpenGeneric&lt;/a&gt; extension method available. The given situation would be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IValidate&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(NullValidator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;The result of this registration is that for instance an &lt;i&gt;NullValidator&amp;lt;Department&amp;gt;&lt;/i&gt; instance is returned when an &lt;i&gt;IValidate&amp;lt;Department&amp;gt;&lt;/i&gt; is requested.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Because the use of unregistered type resolution, a NullValidator&amp;lt;T&amp;gt; will only get returned for types that are not registered, therefore allowing the default implementation to be overridden for a specific implementation. The &lt;b&gt;RegisterManyForOpenGeneric&lt;/b&gt; method, for instance, doesn’t use unregistered type resolution, but simply registers all concrete types it finds in the given assemblies. Those types will therefore always be returned, giving a very convenient and easy to grasp mix.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Unregistered type resolution&lt;a name="Unregistered_Type_Resolution"&gt;&lt;/a&gt;&lt;a name="Unregistered-Type-Resolution"&gt;&lt;/a&gt;&lt;/h3&gt;Unregistered type resolution is the ability to get notified by the container when a type is requested that is currently unregistered in the container. This gives the user the change of registering that type. The &lt;b&gt;Simple Injector&lt;/b&gt; supports this scenario with the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/E_SimpleInjector_Container_ResolveUnregisteredType.htm"&gt;ResolveUnregisteredType&lt;/a&gt; event. Unregistered type resolution enables many advanced scenarios. The framework itself uses this event for implementing the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Registration_Of_Open_Generic_Types"&gt;registration of open generic types&lt;/a&gt;. Other examples of possible scenarios that can be built on top of this event are &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolving_Arrays_And_Lists"&gt;resolving array and lists&lt;/a&gt; and &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Covariance-Contravariance"&gt;covariance and contravariance&lt;/a&gt;. Those scenarios are described here in the advanced scenarios page.&lt;br /&gt;&lt;br /&gt;For more information about how to use this event, please look at the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/E_SimpleInjector_Container_ResolveUnregisteredType.htm"&gt;ResolveUnregisteredType event documentation&lt;/a&gt; in the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/"&gt;reference library&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;Context based injection&lt;a name="Context_Based_Injection"&gt;&lt;/a&gt;&lt;a name="Contextual_Binding"&gt;&lt;/a&gt;&lt;a name="Context-Based-Injection"&gt;&lt;/a&gt;&lt;/h3&gt;Context based injection is the ability to inject a dependency based on the context it lives in. This context is often supplied by the container. Some DI frameworks contain a feature that allows this, while others don’t. The &lt;b&gt;Simple Injector&lt;/b&gt; does &lt;i&gt;not&lt;/i&gt; does not contain such a feature out of the box, but this ability can easily be added by using the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ContextDependentExtensions&amp;referringTitle=Advanced-scenarios"&gt;context based injection extension method&lt;/a&gt; code snippet.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: In many cases context based injection is not the best solution, and the design should be reevaluated. In some narrow cases however it can make sense.&lt;/i&gt;&lt;/blockquote&gt;
The most common scenario is to base the type of the injected dependency on the type of the consumer. Take for instance the following &lt;i&gt;ILogger&lt;/i&gt; interface with a generic &lt;i&gt;Logger&amp;lt;T&amp;gt;&lt;/i&gt; class that needs to be injected into several consumers. &lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; ILogger
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message);
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Logger&amp;lt;T&amp;gt; : ILogger
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message) { }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Consumer1
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Consumer1(ILogger logger) { }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Consumer2
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Consumer2(ILogger logger) { }
}
&lt;/pre&gt;&lt;/div&gt;In this case we want to inject a &lt;i&gt;Logger&amp;lt;Consumer1&amp;gt;&lt;/i&gt; into &lt;i&gt;Consumer1&lt;/i&gt; and a &lt;i&gt;Logger&amp;lt;Consumer2&amp;gt;&lt;/i&gt; into &lt;i&gt;Consumer2&lt;/i&gt;. By using the previous &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ContextDependentExtensions&amp;referringTitle=Advanced-scenarios"&gt;extension method&lt;/a&gt;, we can accomplish this as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterWithContext&amp;lt;ILogger&amp;gt;(dependencyContext =&amp;gt;
{
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; type = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Logger&amp;lt;&amp;gt;).MakeGenericType(
        dependencyContext.ImplementationType);
    
    &lt;span style="color:Blue;"&gt;return&lt;/span&gt; (ILogger)container.GetInstance(type);
});
&lt;/pre&gt;&lt;/div&gt;In the previous code snippet we registered a &lt;i&gt;Func&amp;lt;DependencyContext, ILogger&amp;gt;&lt;/i&gt; delegate, that will get called each time a &lt;i&gt;ILogger&lt;/i&gt; dependency gets resolved. The &lt;i&gt;DependencyContext&lt;/i&gt; instance that gets supplied to that instance, contains the &lt;i&gt;ServiceType&lt;/i&gt; and &lt;i&gt;ImplementationType&lt;/i&gt; into which the &lt;i&gt;ILogger&lt;/i&gt; is getting injected.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Although building a generic type using MakeGenericType is relatively slow, the call to the Func&amp;lt;DependencyContext, TService&amp;gt; delegate itself is about as cheap as calling a Func&amp;lt;TService&amp;gt; delegate. If performance of the MakeGenericType gets a problem, you can always cache the generated types, cache InstanceProducer instances, or cache ILogger instances (note that caching the ILogger instances will make them singletons).&lt;/i&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Even though the use of a generic Logger&amp;lt;T&amp;gt; is a common design (with log4net as the grand godfather of this design), doesn&amp;#39;t always make it a good design. The need for having the logger contain information about its parent type, might indicate design problems. If you&amp;#39;re doing this, please take a look at &lt;a href="http://stackoverflow.com/a/9915056/264697"&gt;this Stackoverflow answer&lt;/a&gt;. It talks about logging in conjunction with the SOLID design principles.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Decorators&lt;a name="Decorators"&gt;&lt;/a&gt;&lt;a name="Generic_Decorators"&gt;&lt;/a&gt;&lt;/h3&gt;The &lt;a href="http://en.wikipedia.org/wiki/SOLID"&gt;SOLID&lt;/a&gt; principles give us important guidance when it comes to writing maintainable software. The &amp;#39;O&amp;#39; of the &amp;#39;SOLID&amp;#39; acronym stands for the &lt;a href="http://en.wikipedia.org/wiki/Open/closed_principle"&gt;Open/closed Principle&lt;/a&gt; which states that classes should be open for extension, but closed for modification. Designing systems around the Open/closed principle means that new behavior can be plugged into the system, without the need to change any existing parts, making the change of breaking existing code much smaller.&lt;br /&gt;&lt;br /&gt;One of the ways to add new functionality (such as &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=http%3a%2f%2fen.wikipedia.org%2fwiki%2fCross-cutting_concern&amp;referringTitle=Advanced-scenarios"&gt;cross-cutting concerns&lt;/a&gt;) to types is by the use of the &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;decorator pattern&lt;/a&gt;. The decorator pattern can be used to extend (decorate) the functionality of a certain object at run-time. Especially when using generic interfaces, the concept of decorators gets really powerful. Take for instance the examples given in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Registration_Of_Open_Generic_Types"&gt;Registration of open generic types&lt;/a&gt; section of this page or for instance the use of an generic &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; interface.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: &lt;a href="http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91"&gt;This article&lt;/a&gt; describes an architecture based on the use of the ICommandHandler&amp;lt;TCommand&amp;gt; interface.&lt;/i&gt;&lt;/blockquote&gt;
Take the plausible scenario where we want to validate all commands that get executed by an &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; implementation. The Open/Closed principle states that we want to do this, without having to alter each and every implementation. We can do this using a (single) decorator:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;
    : ICommandHandler&amp;lt;TCommand&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IValidator validator;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ICommandHandler&amp;lt;TCommand&amp;gt; handler;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ValidationCommandHandlerDecorator(
        IValidator validator, 
        ICommandHandler&amp;lt;TCommand&amp;gt; handler)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validator = validator;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handler = handler;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; ICommandHandler&amp;lt;TCommand&amp;gt;.Handle(TCommand command)
    {
        &lt;span style="color:Green;"&gt;// validate the supplied command (throws when invalid).&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.validator.ValidateObject(command);
		
        &lt;span style="color:Green;"&gt;// forward the (valid) command to the real&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// command handler.&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handler.Handle(command);
    }
}
&lt;/pre&gt;&lt;/div&gt;The &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; class is an implementation of the &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; interface, but it also wraps / decorates an &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; instance. Instead of injecting the real implementation directly into a consumer, we can (let Simple Injector) inject a validator decorator that wraps the real implementation.&lt;br /&gt;&lt;br /&gt;The &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; depends on an &lt;i&gt;IValidator&lt;/i&gt; interface. An implementation that used Microsoft Data Annotations might look like this:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.ComponentModel.DataAnnotations;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; DataAnnotationsValidator : IValidator
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IServiceProvider container;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; DataAnnotationsValidator(Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }
    
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IValidator.ValidateObject(&lt;span style="color:Blue;"&gt;object&lt;/span&gt; instance)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; context = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ValidationContext(instance,
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container, &lt;span style="color:Blue;"&gt;null&lt;/span&gt;);

        &lt;span style="color:Green;"&gt;// Throws an exception when instance is invalid.&lt;/span&gt;
        Validator.ValidateObject(instance, context);    
    }
}
&lt;/pre&gt;&lt;/div&gt;The implementations of the &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt; interface can be registered using the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_OpenGenericBatchRegistrationExtensions_RegisterManyForOpenGeneric.htm"&gt;RegisterManyForOpenGeneric&lt;/a&gt; extension method:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
&lt;/pre&gt;&lt;/div&gt;By using the following extension method from the &lt;b&gt;SimpleInjector.Extensions&lt;/b&gt; namespace, you can wrap the &lt;i&gt;ValidationCommandHandlerDecorator&amp;lt;TCommand&amp;gt;&lt;/i&gt; around each and every &lt;i&gt;ICommandHandler&amp;lt;TCommand&amp;gt;&lt;/i&gt; implementation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;Multiple decorators can be wrapped by calling the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterDecorator.htm"&gt;RegisterDecorator&lt;/a&gt; method multiple times, as the following registration shows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TransactionCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DeadlockRetryCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;The decorators are applied in the order in which they are registered, which means that the first decorator (&lt;i&gt;TransactionCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; in this case) wraps the real instance, the second decorator (&lt;i&gt;DeadlockRetryCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; in this case) wraps the first decorator, and so on.&lt;br /&gt;&lt;br /&gt;There&amp;#39;s an overload of the &lt;b&gt;RegisterDecorator&lt;/b&gt; available that allows you to supply a predicate to determine whether that decorator should be applied to a specific service type. Using a given context you can determine whether the decorator should be applied. Here is an example:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AccessValidationCommandHandlerDecorator&amp;lt;&amp;gt;),
    context =&amp;gt; !context.ImplementationType.Namespace.EndsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Admins&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;The given context contains several properties that allows you to analyze whether a decorator should be applied to a given service type, such as the current closed generic service type (using the &lt;i&gt;ServiceType&lt;/i&gt; property) and the concrete type that will be created (using the &lt;i&gt;ImplementationType&lt;/i&gt; property). The predicate will (under normal circumstances) be called only once per generic type, so there is no performance implication for using it.&lt;br /&gt;
&lt;h4&gt;Decorators with Func&amp;lt;T&amp;gt; factories&lt;a name="Decorators_With_Func_Factories"&gt;&lt;/a&gt;&lt;/h4&gt;In certain scenarios, it is needed to postpone building part of the object graph. For instance when a service needs to control the lifetime of a dependency, need multiple instances, when instances need to be &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Multi_Threaded_Applications"&gt;executed on a different thread&lt;/a&gt;, or when instances need to be created in a certain &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Scoped"&gt;scope&lt;/a&gt; or (security) context.&lt;br /&gt;&lt;br /&gt;When building a &amp;#39;normal&amp;#39; object graph with dependencies, you can easily delay building a part of the graph by letting a service depend on a factory. This allows building that part of the object graph to be postponed until the time the type starts using the factory. When working with decorators however, injecting a factory to postpone the creation of the decorated instance will not work. Take for instance a &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; that allows executing a command handler on a different thread. We could let the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; depend on a &lt;i&gt;CommandHandlerFactory&amp;lt;T&amp;gt;&lt;/i&gt;, and let this factory call back into the container to retrieve a new &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt;. Unfortunately this would fail, since requesting an &lt;i&gt;ICommandHandler&amp;lt;T&amp;gt;&lt;/i&gt; would again wrap this instance with a new &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt;, and we&amp;#39;d end up causing a stack overflow.&lt;br /&gt;&lt;br /&gt;Since this is a scenario that is really hard to solve without framework support, &lt;b&gt;Simple Injector&lt;/b&gt; allows injecting a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate into registered decorators. This delegate functions as a factory for the creation of the decorated instance. Taking the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; as example, it could be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;
    : ICommandHandler&amp;lt;T&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;ICommandHandler&amp;lt;T&amp;gt;&amp;gt; factory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AsyncCommandHandlerDecorator(
        Func&amp;lt;ICommandHandler&amp;lt;T&amp;gt;&amp;gt; factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
	
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(T command)
    {
        &lt;span style="color:Green;"&gt;// Execute on different thread.&lt;/span&gt;
        ThreadPool.QueueUserWorkItem(_ =&amp;gt;
        {
            &lt;span style="color:Green;"&gt;// Create new handler in this thread.&lt;/span&gt;
            &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory();
            handler.Handle(command)
        });
    }
}
&lt;/pre&gt;&lt;/div&gt;This special decorator can be registered just as any other decorator:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;However, since the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; solely has singleton dependencies (the &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; is a singleton), and creates a new decorated instance each time it’s called, we can even register it as a singleton itself:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingleDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;When mixing this with other (synchronous) decorators, you&amp;#39;ll get an extremely powerful and pluggable system:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterManyForOpenGeneric(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;).Assembly);
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TransactionCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DeadlockRetryCommandHandlerDecorator&amp;lt;&amp;gt;));

container.RegisterSingleDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncCommandHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; c.ImplementationType.Name.StartsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Async&amp;quot;&lt;/span&gt;));
	
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ICommandHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationCommandHandlerDecorator&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This configuration has an interesting mix of decorator registrations. The registration of the &lt;i&gt;AsyncCommandHandlerDecorator&amp;lt;T&amp;gt;&lt;/i&gt; allows (some of) the command handlers to be executed on the background (while others -who&amp;#39;s name does not start with &amp;#39;Async&amp;#39;- still run synchronously), but before execution, all commands are validated synchronously (to allow communicating validation errors to the caller). And all handlers (even the asynchronous ones) are executed in a transaction and the operation is retried when the database rolled back because of a deadlock).&lt;br /&gt;
&lt;h4&gt;Decorated collections&lt;a name="Decorated_Collections"&gt;&lt;/a&gt;&lt;/h4&gt;When registering a decorator, Simple Injector will automatically decorate any collection with elements of that service type:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterAll&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt;(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CustomerMovedEventHandler),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(NotifyStaffWhenCustomerMovedEventHandler));
    
container.RegisterDecorator(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;&amp;gt;),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ValidationEventHandlerDecorator&amp;lt;&amp;gt;),
    c =&amp;gt; SomeCondition);
&lt;/pre&gt;&lt;/div&gt;The previous registration registers a collection of &lt;i&gt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&lt;/i&gt; services. Those services are decorated with a &lt;i&gt;ValidationEventHandlerDecorator&amp;lt;TEvent&amp;gt;&lt;/i&gt; when the supplied predicate holds.&lt;br /&gt;&lt;br /&gt;For collections of elements that are created by the container (container controlled), the predicate is checked for each element in the collection. For collections of uncontrolled elements (a list of items that is not created by the container), the predicate is checked once for the whole collection. This means that controlled collections can be partially decorated. Taking the previous example for instance, you could let the &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; be decorated, while leaving the &lt;i&gt;NotifyStaffWhenCustomerMovedEventHandler&lt;/i&gt; undecorated (determined by the supplied predicate).&lt;br /&gt;&lt;br /&gt;When a collection is uncontrolled, it means that the lifetime of its elements are unknown to the container. The following registration is an example of an uncontrolled collection:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
IEnumerable&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt; handlers =
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;[]
    {
        &lt;span style="color:Blue;"&gt;new&lt;/span&gt; CustomerMovedEventHandler(),
        &lt;span style="color:Blue;"&gt;new&lt;/span&gt; NotifyStaffWhenCustomerMovedEventHandler(),
    };

container.RegisterAll&amp;lt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&amp;gt;(handlers);
&lt;/pre&gt;&lt;/div&gt;Although this registration contains a list of singletons, the container has no way of knowing this. The collection could easily have been a dynamic (an ever changing) collection. In this case, the container calls the registered predicate once (and supplies the predicate with the &lt;i&gt;IEventHandler&amp;lt;CusotmerMovedEvent&amp;gt;&lt;/i&gt; type)  and if the predicate returns true, each element in the collection is decorated with a decorator instance.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Warning&lt;/b&gt;: In general you should prevent registering uncontrolled collections. The container knows nothing about them, and can&amp;#39;t help you in doing &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;diagnostics&lt;/a&gt;. Since the lifetime of those items is unknown, the container will be unable to wrap a decorator with a lifestyle other than transient. Best practice is to register container-controlled collections which is done by using one of the &lt;b&gt;RegisterAll&lt;/b&gt; overloads that take a collection of &lt;b&gt;System.Type&lt;/b&gt; instances.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Interception&lt;a name="Interception"&gt;&lt;/a&gt;&lt;/h3&gt;Interception is the ability to intercept a call from a consumer to a service, and add or change behavior. The &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;decorator pattern&lt;/a&gt; describes a form of interception, but when it comes to applying cross-cutting concerns, you might end up writing decorators for many service interfaces, but with the exact same code. If this is happening, it is time to explore the possibilities of interception.&lt;br /&gt;&lt;br /&gt;Using the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets, you can add the ability to do interception with the &lt;b&gt;Simple Injector&lt;/b&gt;. Using the given code, you can for instance define a &lt;i&gt;MonitoringInterceptor&lt;/i&gt; that allows logging the execution time of the called service method:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MonitoringInterceptor : IInterceptor
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;

    &lt;span style="color:Green;"&gt;// Using constructor injection on the interceptor&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MonitoringInterceptor(ILogger logger)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = logger;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Intercept(IInvocation invocation)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; watch = Stopwatch.StartNew();

        &lt;span style="color:Green;"&gt;// Calls the decorated instance.&lt;/span&gt;
        invocation.Proceed();

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; decoratedType =
            invocation.InvocationTarget.GetType();
        
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger.Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt;.Format(
            &lt;span style="color:#A31515;"&gt;&amp;quot;{0} executed in {1} ms.&amp;quot;&lt;/span&gt;,
            decoratedType.Name,
            watch.ElapsedMiliseconds));
    }
}
&lt;/pre&gt;&lt;/div&gt;This interceptor can be registered to be wrapped around a concrete implementation. Using the given extension methods, this can be done as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.InterceptWith&amp;lt;MonitoringInterceptor&amp;gt;(type =&amp;gt; type == &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IUserRepository));
&lt;/pre&gt;&lt;/div&gt;This registration ensures that every time an &lt;i&gt;IUserRepository&lt;/i&gt; interface is requested, an interception proxy is returned that wraps that instance and uses the &lt;i&gt;MonitoringInterceptor&lt;/i&gt; to extend the behavior.&lt;br /&gt;&lt;br /&gt;The current example doesn&amp;#39;t add much compared to simply using a decorator. When having many interface service types that need to be decorated with the same behavior however, it gets different:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.InterceptWith&amp;lt;MonitoringInterceptor&amp;gt;(t =&amp;gt; t.Name.EndsWith(&lt;span style="color:#A31515;"&gt;&amp;quot;Repository&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets use .NET&amp;#39;s &lt;/i&gt;System.Runtime.Remoting.Proxies.RealProxy&lt;i&gt; class to generate interception proxies. The &lt;/i&gt;RealProxy&lt;i&gt; only allows to proxy interfaces.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: the interfaces in the given &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=InterceptionExtensions&amp;referringTitle=Advanced-scenarios"&gt;Interception extensions&lt;/a&gt; code snippets are a simplified version of the Castle Project interception facility. If you need to create lots different interceptors, you might benefit from using the interception abilities of the Castle Project. Also please note that the given snippets use dynamic proxies to do the interception, while Castle uses lightweight code generation (LCG). LCG allows much better performance than the use of dynamic proxies.&lt;/i&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Don&amp;#39;t use interception for intercepting types that all implement the same generic interface, such as ICommandHandler&amp;lt;T&amp;gt; or IValidator&amp;lt;T&amp;gt;. Try using decorator classes instead, as shown in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Decorators"&gt;Decorators&lt;/a&gt; section on this page.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Property injection&lt;a name="Implicit_Property_Injection"&gt;&lt;/a&gt;&lt;a name="Implicit-Property-Injection"&gt;&lt;/a&gt;&lt;a name="Property-Injection"&gt;&lt;/a&gt;&lt;/h3&gt;Simple Injector does not inject any properties into types that get resolved by the container. In general there are two ways of doing property injection, and both are not supported by default for reasons explained below.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Implicit property injection&lt;/b&gt;&lt;br /&gt;Some containers (such as Castle Windsor) implicitly inject public writable properties by default for any instance you resolve. They do this by mapping those properties to configured types. When no such registration exists, or when the property doesn’t have a public setter, the property will be skipped. Simple Injector does not do this, and for good reason. We think that &lt;i&gt;implicit property injection&lt;/i&gt; is simply too uuhh...  implicit :-). Silently skipping properties that can&amp;#39;t be mapped can lead to a DI configuration that can&amp;#39;t be verified easily and can therefore result in an application that fails at runtime instead of failing when the container is verified.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Explicit property injection&lt;/b&gt;&lt;br /&gt;We strongly feel that explicit property injection is a much better way to go. With explicit property injection the container is forced to inject a property and the process will fail immediately when a property can&amp;#39;t be mapped or injected. Some containers (such as Unity and Ninject) allow explicit property injection by default by allowing properties to be decorated with attributes. Problem with this is that this forces the application to take a dependency on the framework, which is something that should be prevented.&lt;br /&gt;&lt;br /&gt;Because Simple Injector does not encourage its users to take a dependency on the container (except for the startup path of course), Simple Injector does not contain any attributes that allow explicit property injection and it can therefore not explicitly inject properties out-of-the-box.&lt;br /&gt;&lt;br /&gt;Besides this, the use of property injection should be very exceptional and in general constructor injection should always be used. If a constructor gets too many parameters (constructor over-injection anti-pattern), it is an indication of a violation of the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; (SRP). SRP violations often lead to maintainability issues. So instead of patching constructor over-injection with property injection, the root cause should be analyzed and the type should be refactored, probably with &lt;a href="http://blog.ploeh.dk/2010/02/02/RefactoringtoAggregateServices/"&gt;Facade Services&lt;/a&gt;. Another common reason to use properties is because those dependencies are optional. Instead of using optional property dependencies, best practice is to inject empty implementations (a.k.a. &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object pattern&lt;/a&gt;) into the constructor.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Enabling property injection&lt;/b&gt;&lt;br /&gt;Simple Injector contains two ways to enable property injection. First of all the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Configuring_Property_Injection"&gt;RegisterInitializer&amp;#60;T&amp;#62;&lt;/a&gt; method can be used to inject properties (especially configuration values) on a per-type basis. Take for instance the following code snippet:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterInitializer&amp;lt;HandlerBase&amp;gt;(handlerToInitialize =&amp;gt;
{
    handlerToInitialize.ExecuteAsynchronously = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;
});
&lt;/pre&gt;&lt;/div&gt;In the previous example a &lt;i&gt;Action&amp;lt;T&amp;gt;&lt;/i&gt; delegate is registered that will be called every time the container creates a type that inherits from &lt;i&gt;HandlerBase&lt;/i&gt;. In this case, the handler will set a configuration value on that class.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: although this method can also be used injecting services, please note that the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;Diagnostic Services&lt;/a&gt; will be unable to see and analyze this dependency.&lt;/i&gt;&lt;/blockquote&gt;
The second way to inject properties is by implementing a custom &lt;b&gt;IPropertySelectionBehavior&lt;/b&gt;. The &lt;i&gt;property selection behavior&lt;/i&gt; is a general extension point provided by the container, to override the library&amp;#39;s default behavior. The following code for instance enables explicit property injection using attributes, using any customly defined attribute:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Reflection;

&lt;span style="color:Blue;"&gt;using&lt;/span&gt; SimpleInjector.Advanced;

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AttributedPropertyInjectionExtensions
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; AutowirePropertiesWithAttribute&amp;lt;TAttribute&amp;gt;(
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt; ContainerOptions options)
        &lt;span style="color:Blue;"&gt;where&lt;/span&gt; TAttribute : Attribute
    {
        options.PropertySelectionBehavior =
            &lt;span style="color:Blue;"&gt;new&lt;/span&gt; AttributePropertyInjectionBehavior(
                options.PropertySelectionBehavior, 
                &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(TAttribute));
    }

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AttributePropertyInjectionBehavior 
        : IPropertySelectionBehavior
    {
        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IPropertySelectionBehavior behavior;
        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Type attribute;

        &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AttributePropertyInjectionBehavior(
            IPropertySelectionBehavior baseBehavior, 
            Type attributeType)
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.behavior = baseBehavior;
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.attribute = attributeType;
        }

        &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;bool&lt;/span&gt; SelectProperty(Type type, PropertyInfo prop)
        {
            &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.IsPropertyDecorated(prop) ||
                &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.behavior.SelectProperty(type, prop);
        }

        &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;bool&lt;/span&gt; IsPropertyDecorated(PropertyInfo p)
        {
            &lt;span style="color:Blue;"&gt;return&lt;/span&gt; p.GetCustomAttributes(&lt;span style="color:Blue;"&gt;this&lt;/span&gt;.attribute, &lt;span style="color:Blue;"&gt;true&lt;/span&gt;).Any();
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;The previous code snippet can be used as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();
container.Options.AutowirePropertiesWithAttribute&amp;lt;MyInjectAttribute&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;This enables explicit property injection on all properties that are marked with the &lt;b&gt;MyInjectAttribute&lt;/b&gt; and it will fail when the property cannot be injected for whatever reason.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: Properties injected by the container through the &lt;b&gt;IPropertySelectionBehavior&lt;/b&gt; will be analyzed by the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Advanced-scenarios"&gt;Diagnostic Services&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;
&lt;h3&gt;Covariance and Contravariance&lt;a name="Covariance_Contravariance"&gt;&lt;/a&gt;&lt;a name="Covariance-Contravariance"&gt;&lt;/a&gt;&lt;/h3&gt;Since version 4.0 of the .NET framework, the type system allows &lt;a href="http://msdn.microsoft.com/en-us/library/dd799517.aspx"&gt;Covariance and Contravariance in Generics&lt;/a&gt; (especially interfaces and delegates). This allows for instance, to use a &lt;i&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/i&gt; as an &lt;i&gt;IEnumerable&amp;lt;object&amp;gt;&lt;/i&gt; (covariance), or to use an &lt;i&gt;Action&amp;lt;object&amp;gt;&lt;/i&gt; as an &lt;i&gt;Action&amp;lt;string&amp;gt;&lt;/i&gt; (contravariance).&lt;br /&gt;&lt;br /&gt;In some circumstances, the application design can benefit from the use of covariance and contravariance (or variance for short) and it would be beneficial when the IoC container returns services that are &amp;#39;compatible&amp;#39; to the requested service, even although the requested service is not registered. To stick with the previous example, the container could return an &lt;i&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/i&gt; even when an &lt;i&gt;IEnumerable&amp;lt;object&amp;gt;&lt;/i&gt; is requested.&lt;br /&gt;&lt;br /&gt;By default, the Simple Injector does not return variant implementations of given services, but the Simple Injector can be extended to behave this way. The actual way to write this extension depends on the requirements of the application.&lt;br /&gt;&lt;br /&gt;Take a look at the following application design around the &lt;i&gt;IEventHandler&amp;lt;in TEvent&amp;gt;&lt;/i&gt; interface:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IEventHandler&amp;lt;&lt;span style="color:Blue;"&gt;in&lt;/span&gt; TEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(TEvent e);
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedEvent 
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;int&lt;/span&gt; CustomerId { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Address NewAddress { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedAbroadEvent : CustomerMovedEvent
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; Country Country { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
}

&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CustomerMovedEventHandler : IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Handle(CustomerMovedEvent e) { ... }
}
&lt;/pre&gt;&lt;/div&gt;The design contains two event classes &lt;i&gt;CustomerMovedEvent&lt;/i&gt; and &lt;i&gt;CustomerMovedAbroadEvent&lt;/i&gt; (where &lt;i&gt;CustomerMovedAbroadEvent&lt;/i&gt; inherits from &lt;i&gt;CustomerMovedEvent&lt;/i&gt;) one concrete event handler &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; and a generic interface for event handlers.&lt;br /&gt;&lt;br /&gt;We can configure the container in such way that not only a request for &lt;i&gt;IEventHandler&amp;lt;CustomerMovedEvent&amp;gt;&lt;/i&gt; results in a &lt;i&gt;CustomerMovedEventHandler,&lt;/i&gt; but also a request for &lt;i&gt;IEventHandler&amp;lt;CustomerMovedAbroadEvent&amp;gt;&lt;/i&gt; results in that same &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; (because &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; also accepts &lt;i&gt;CustomerMovedAbroadEvents&lt;/i&gt;).&lt;br /&gt;&lt;br /&gt;There are several ways to achieve this, but one of the ways is the following:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;CustomerMovedEventHandler&amp;gt;();

container.RegisterSingleOpenGeneric(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;&amp;gt;), 
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ContravarianceEventHandler&amp;lt;&amp;gt;));
&lt;/pre&gt;&lt;/div&gt;This registration depends on the custom &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; that should be placed close to the registration itself:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ContravarianceEventHandler&amp;lt;TEvent&amp;gt;
    : IEventHandler&amp;lt;TEvent&amp;gt;
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; Func&amp;lt;IEventHandler&amp;lt;TEvent&amp;gt;&amp;gt; handlerFactory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ContravarianceEventHandler(Container container)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handlerType = &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IEventHandler&amp;lt;TEvent&amp;gt;);

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; registration = (
            &lt;span style="color:Blue;"&gt;from&lt;/span&gt; r &lt;span style="color:Blue;"&gt;in&lt;/span&gt; container.GetCurrentRegistrations()
            &lt;span style="color:Blue;"&gt;let&lt;/span&gt; assignableInterfaces = (
                &lt;span style="color:Blue;"&gt;from&lt;/span&gt; iface &lt;span style="color:Blue;"&gt;in&lt;/span&gt; r.ServiceType.GetInterfaces()
                &lt;span style="color:Blue;"&gt;where&lt;/span&gt; handlerType.IsAssignableFrom(iface)
                &lt;span style="color:Blue;"&gt;select&lt;/span&gt; iface)
            &lt;span style="color:Blue;"&gt;where&lt;/span&gt; assignableInterfaces.Any()
            &lt;span style="color:Blue;"&gt;select&lt;/span&gt; r)
            .Single();

        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handlerFactory =
            () =&amp;gt; (IEventHandler&amp;lt;TEvent&amp;gt;)r.GetInstance();
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IEventHandler&amp;lt;TEvent&amp;gt;.Handle(TEvent e)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.handlerFactory().Handle(e);
    }
}
&lt;/pre&gt;&lt;/div&gt;The registration ensures that every time an &lt;i&gt;IEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; is requested, a &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; is returned. The &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; will on creation query the container for a single service type that implements &lt;i&gt;IEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt;. Because the &lt;i&gt;CustomerMovedEventHandler&lt;/i&gt; is not registered by its interface, but by itself, the &lt;i&gt;ContravarianceEventHandler&amp;lt;TEvent&amp;gt;&lt;/i&gt; will find that type.&lt;br /&gt;&lt;br /&gt;This is just one example and one way of adding variance support. For a more elaborate discussion on this subject, please read the following article: &lt;a href="http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=90"&gt;Adding Covariance and Contravariance to the Simple Injector&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;Registering plugins dynamically&lt;a name="Plugins"&gt;&lt;/a&gt;&lt;/h3&gt;Applications with a plugin architecture often allow special plugin assemblies to be dropped in a special folder and to be picked up by the application, without the need of a recompile. Although Simple Injector does not support this out of the box, registering plugins from dynamically loaded assemblies can be implemented in a few lines of code. Here is an example:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;string&lt;/span&gt; pluginDirectory =
    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, &lt;span style="color:#A31515;"&gt;&amp;quot;Plugins&amp;quot;&lt;/span&gt;);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; pluginAssemblies =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; file &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; DirectoryInfo(pluginDirectory).GetFiles()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; file.Extension == &lt;span style="color:#A31515;"&gt;&amp;quot;.dll&amp;quot;&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; Assembly.LoadFile(file.FullName);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; pluginTypes =
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; dll &lt;span style="color:Blue;"&gt;in&lt;/span&gt; pluginAssemblies
    &lt;span style="color:Blue;"&gt;from&lt;/span&gt; type &lt;span style="color:Blue;"&gt;in&lt;/span&gt; dll.GetExportedTypes()
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IPlugin).IsAssignableFrom(type)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; !type.IsAbstract
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; !type.IsGenericTypeDefinition
    &lt;span style="color:Blue;"&gt;select&lt;/span&gt; type;

container.RegisterAll&amp;lt;IPlugin&amp;gt;(pluginTypes);
&lt;/pre&gt;&lt;/div&gt;The given example makes use of an &lt;i&gt;IPlugin&lt;/i&gt; interface that is known to the application, and probably located in a shared assembly. The dynamically loaded plugin .dll files can contain multiple classes that implement &lt;i&gt;IPlugin&lt;/i&gt;, and all publicly exposed concrete types that implements &lt;i&gt;IPlugin&lt;/i&gt; will be registered using the &lt;i&gt;RegisterAll&lt;/i&gt; method and can get resolved using the default auto-wiring behavior of the container, meaning that the plugin must have a single public constructor and all constructor arguments must be resolvable by the container. The plugins can get resolved using &lt;i&gt;container.GetAllInstances&amp;lt;IPlugin&amp;gt;()&lt;/i&gt; or by adding an &lt;i&gt;IEnumerable&amp;lt;IPlugin&amp;gt;&lt;/i&gt; argument to a constructor.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Resolving instances by key&lt;a name="Resolving_Instances_By_Key"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolve_Instances_By_Key"&gt;Resolve Instances By Key&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios"&gt;How to&lt;/a&gt; documentation page, for the information you are looking for.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Resolving array and lists&lt;a name="Resolving_Arrays_And_Lists"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#Resolve_Arrays_And_Lists"&gt;Resolve Arrays and Lists&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;referringTitle=Advanced-scenarios"&gt;How to&lt;/a&gt; page, for the information you are looking for.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Lifetime scoping&lt;a name="Lifetime_scoping"&gt;&lt;/a&gt;&lt;/h3&gt;The information you are looking for has been moved. Please see the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios&amp;ANCHOR#PerLifetimeScope"&gt;Per Lifetime Scope&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Advanced-scenarios"&gt;Object Lifestyle Management&lt;/a&gt; documentation page for more information.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Thu, 04 Apr 2013 09:37:03 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Advanced-scenarios 20130404093703A</guid></item><item><title>Updated Wiki: UnregisteredTypes</title><link>https://simpleinjector.codeplex.com/wikipage?title=UnregisteredTypes&amp;version=5</link><description>&lt;div class="wikidoc"&gt;&lt;h1&gt;Diagnostic Warning - Unregistered Types&lt;/h1&gt;
&lt;h2&gt;Cause&lt;a name="Cause"&gt;&lt;/a&gt;&lt;/h2&gt;An concrete type that was not registered explicitly and was not resolved using unregistered type resolution, but was created by the container using the transient lifestyle.&lt;br /&gt;
&lt;h2&gt;Warning Description&lt;a name="WarningDescription"&gt;&lt;/a&gt;&lt;/h2&gt;The unregistered typed warning shows all concrete types that weren&amp;#39;t registered explicitly, but registered by the container as transient for you, because they were referenced by another component&amp;#39;s constructor or were resolved through a direct call to &lt;i&gt;container.GetIntance&amp;lt;T&amp;gt;()&lt;/i&gt; (inside a   &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/M_SimpleInjector_Container_RegisterInitializer__1.htm"&gt;RegisterInitializer&lt;/a&gt; registered delegate for instance).&lt;br /&gt;&lt;br /&gt;This warning deserves your attention, since it might indicate that you program to implementations, instead of abstractions. Although the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=PotentialLifestyleMismatches&amp;referringTitle=UnregisteredTypes"&gt;Potential Lifestyle Mismatches&lt;/a&gt; and &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ShortCircuitedDependencies&amp;referringTitle=UnregisteredTypes"&gt;Short Circuited Dependencies&lt;/a&gt; warnings are a very strong signal of a configuration problem, this &lt;b&gt;Unregistered Type&lt;/b&gt; warnings is just a point of attention. &lt;br /&gt;
&lt;h2&gt;How to Fix Violations&lt;a name="HowToFixViolations"&gt;&lt;/a&gt;&lt;/h2&gt;Let components depend on an interface that described the contract that this concrete type implements and register that concrete type in the container by that interface.&lt;br /&gt;
&lt;h2&gt;When to Ignore Warnings&lt;a name="WhenToIgnoreWarnings"&gt;&lt;/a&gt;&lt;/h2&gt;If your intention is to resolve those types as transient and don&amp;#39;t depend directly on their concrete types (for instance because you register a collection of transient types), this warning can in general be ignored safely.&lt;br /&gt;
&lt;h2&gt;Example&lt;a name="Example"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.Register&amp;lt;HomeController&amp;gt;();

container.Verify();

&lt;span style="color:Green;"&gt;// Definition of HomeController&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; HomeController : Controller {
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; SqlUserRepository repository;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; HomeController(SqlUserRepository repository) {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.repository = repository;
    }
}
&lt;/pre&gt;&lt;/div&gt;The given example registers a &lt;i&gt;HomeController&lt;/i&gt; class that depends on an unregistered &lt;i&gt;SqlUserRepository&lt;/i&gt; class. Injecting a concrete type can lead to a lot of problems, such as:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;It makes the &lt;i&gt;HomeController&lt;/i&gt; hard to test, since concrete types are often hard to fake (or when using a mocking framework that fixes this, would still result in unit tests that contain a lot of configuration for the mocking framework, instead of pure test logic) making the unit tests hard to read and hard to maintain.&lt;/li&gt;
&lt;li&gt;It makes it harder to reuse the class, since it expects a certain implementation of its dependency.&lt;/li&gt;
&lt;li&gt;It makes it harder to add cross-cutting concerns (such as logging, audit trailing and authorization) to the system, because this must now either be added directly in the &lt;i&gt;SqlUserRepository&lt;/i&gt; class (which will make this class hard to test and hard to maintain) or all constructors of classes that depend on &lt;i&gt;SqlUserRepository&lt;/i&gt; must be changed to allow injecting a type that adds these cross-cutting concerns.&lt;/li&gt;&lt;/ul&gt;
Instead of depending directly on &lt;i&gt;SqlUserRepository&lt;/i&gt;, &lt;i&gt;HomeController&lt;/i&gt; can better depend on an &lt;i&gt;IUserRepository&lt;/i&gt; abstraction:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.Register&amp;lt;IUserRepository, SqlUserRepository&amp;gt;();
container.Register&amp;lt;HomeController&amp;gt;();

container.Verify();

&lt;span style="color:Green;"&gt;// Definition of HomeController&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; HomeController : Controller {
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IUserRepository repository;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; HomeController(IUserRepository repository) {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.repository = repository;
    }
}
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: It would probably be better to define an generic &lt;b&gt;IRepository&amp;lt;T&amp;gt;&lt;/b&gt; abstraction. This makes easy to &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=UnregisteredTypes&amp;ANCHOR#Batch_Registration"&gt;batch registration&lt;/a&gt; implementations and allows cross-cutting concerns to be added using &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=UnregisteredTypes&amp;ANCHOR#Generic_Decorators"&gt;decorators&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;
The following example shows the registration of a collection of types:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.RegisterAll&amp;lt;ILogger&amp;gt;(
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(FileLogger),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DbLogger),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(WindowsEventLogger));
&lt;/pre&gt;&lt;/div&gt;In this case &lt;i&gt;FileLogger&lt;/i&gt;, &lt;i&gt;DbLogger&lt;/i&gt; and &lt;i&gt;WindowsEventLogger&lt;/i&gt; are not registered explicitly, and will be resolved with the transient lifestyle. When the application does not depend directly on one of those types, but only on &lt;i&gt;IEnumerable&amp;lt;ILogger&amp;gt;&lt;/i&gt;, the application will still only depend on abstractions. In that case this warning can be ignored safely.&lt;br /&gt;&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Thu, 04 Apr 2013 09:30:36 GMT</pubDate><guid isPermaLink="false">Updated Wiki: UnregisteredTypes 20130404093036A</guid></item></channel></rss>