Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Last active December 14, 2015 22:59
Show Gist options
  • Save jmarnold/5162504 to your computer and use it in GitHub Desktop.
Save jmarnold/5162504 to your computer and use it in GitHub Desktop.
public class LinkInjector
{
private readonly IContainer _container;
public LinkInjector(IContainer container)
{
_container = container;
}
public void Inject(Type type, object model)
{
var proxyType = typeof(LinkInjectionProxy<>).MakeGenericType(type);
var proxy = (ILinkInjectionProxy)Activator.CreateInstance(proxyType);
proxy.Inject(_container, type, model);
}
public interface ILinkInjectionProxy
{
void Inject(IContainer container, Type type, object model);
}
public class LinkInjectionProxy<T> : ILinkInjectionProxy
{
public void Inject(IContainer container, Type type, object model)
{
var injectors = container.GetAllInstances<ILinkInjection<T>>();
injectors.Each(x => x.Inject(model));
}
}
}
[TestFixture]
public class LinkInjectorTester : InteractionContext<LinkInjector>
{
private ILinkInjector<LinkInjectorTarget> l1;
private ILinkInjector<LinkInjectorTarget> l2;
private LinkInjectorTarget theTarget;
protected override void beforeEach()
{
l1 = MockRepository.GenerateStub<ILinkInjector<LinkInjectorTarget>>();
l2 = MockRepository.GenerateStub<ILinkInjector<LinkInjectorTarget>>();
theTarget = new LinkInjectorTarget();
Services.Inject(l1);
Services.Inject(l2);
ClassUnderTest.Inject(typeof(LinkInjectorTarget), theTarget);
}
[Test]
public void invokes_the_injectors()
{
l1.AssertWasCalled(x => x.Inject(theTarget));
l2.AssertWasCalled(x => x.Inject(theTarget));
}
public class LinkInjectorTarget { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment