Skip to content

Instantly share code, notes, and snippets.

@chilversc
Forked from anonymous/gist:25c1bb209b94619b5346
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilversc/f42d89ed1b476aa769f3 to your computer and use it in GitHub Desktop.
Save chilversc/f42d89ed1b476aa769f3 to your computer and use it in GitHub Desktop.
WindsorContainer container;
void Main()
{
CompositionRoot ();
Test ();
}
void Test ()
{
var foo = container.Resolve<IFoo> ();
var myFirestarter = container.Resolve<IFirestarter<int>> ();
var myWaterstarterYouTroubleStarter = container.Resolve<IWaterstarter<Nestling<int>>> ();
}
void CompositionRoot()
{
container = new WindsorContainer ();
container.Register (
Component.For (typeof (IFoo)).ImplementedBy (typeof (ConcreteFoo)),
Component.For (typeof (IFirestarter<>)).ImplementedBy (typeof (ConcreteFirestarter<>)),
// This here is the trick, tell Windsor how to map the generic arguments
Component.For (typeof (IWaterstarter<>)).ImplementedBy (typeof (ConcreteWaterstarter<>), new NestedGenericStrategy ())
);
}
public class NestedGenericStrategy : IGenericImplementationMatchingStrategy
{
public Type[] GetGenericArguments (ComponentModel model, CreationContext context)
{
var nestlingType = context.RequestedType.GetGenericArguments ().Single ();
var outerType = nestlingType.GetGenericArguments ().Single ();
return new [] { outerType };
}
}
// Define other methods and classes here
public interface IFoo { }
public class ConcreteFoo : IFoo { }
// Define other methods and classes here
public interface IFirestarter<T> { }
public class ConcreteFirestarter<T> : IFirestarter<T> { }
public interface IWaterstarter<T> { }
public class Nestling<T> { }
public class ConcreteWaterstarter<T> : IWaterstarter<Nestling<T>> { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment