How to return an instance based on its parent using Simple Injector?


We have been using Windsor Castle as our DI Container but we are looking around for alternatives. Now I got to the Simple Injector and so far I have been impressed by its simplicity but one scenario I am stuck at right now is when we are using late bound instantiation for some of our components.

My first question is, is it even possible?

With Windsor it is done like this;

Container.Register(Component.For<ILogger>() .UsingFactoryMethod( (kernel, componentModel, context) => new Logger( context.Handler.ComponentModel.Implementation.Name));

Is there a way to do the same with Simple Injector?


With Simple Injector, you will just have to either:

<ol><li>Register a Func delegate explicitly using Register<Func<A, B, C>>((a, b) => { ... })</li> <li>Create an interface such as ILoggerFactory, creste an implementation and register them.</li> </ol>


After reading your question again and looking up the definition of Castle's UsingFactoryMethod I came to the conclusion that I misinterpreted your question. What you are looking for is creating an instance based on its parent type: <a href="http://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios#Context_Based_Injection" rel="nofollow">Context Based Injection</a>.

You can achieve this by implementing the <a href="http://simpleinjector.codeplex.com/wikipage?title=ContextDependentExtensions&referringTitle=Advanced-scenarios" rel="nofollow">Context Based Injection Extension method</a> that is given as example on the Simple Injector's documentation wiki.

When you add this extension method to your project you can do the equivalent registration as follows:

<pre class="lang-cs prettyprint-override">container.RegisterWithContext<ILogger>(context => { return new Logger(context.ImplementationType.Name); });


  • Protecting webapi with IdentityServer and Autofac - can't get claims
  • Windsor registration for generic commands/command handlers
  • How to return an instance based on its parent using Simple Injector?
  • Inject dependency into DelegatingHandler
  • Entity Framework Core 2.0 migrations update database with seed data throwing error
  • Simple Injector and a Logging Abstraction Using Log4net
  • AssemblyNeutral Attribute in ASp.net vnext
  • Regex for unique user count
  • Attempt at parsing packets: Is there a Java equivalent to Python's “unpack”?
  • Non-overlapped transparent control
  • Trouble finding source of a designer exception in VS2010
  • read / write mp4 id3 tags with PHP
  • Can I control when DataGridView reads and writes from/to its DataSource?
  • What do getresuid() and setresuid() do?
  • Mocking Non-Standard Events in F# Foq
  • Configuring Ninject
  • How do I change the kernel/python version for iPython?
  • triggering user space with kernel
  • Other than Linq to SQL does anything else consume INotifyPropertyChanging?
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Typescript - Unable to get 'import' statement to function
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Updating server-side rendering client-side
  • Javascript Callbacks with Object constructor
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal