Last active
December 14, 2015 22:59
-
-
Save jmarnold/5162504 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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