public void InjectedWhateverWithWhateverInsideIsProperlyDisposed() { var injectionist = new Injection.Injectionist(); var eventTracker = new EventTracker(); injectionist.Register(c => { var fakeBus = new FakeBus(c.Get <Disposable1>(), c.Get <EventTracker>()); fakeBus.FakeBusDisposed += () => { foreach (var disposable in c.TrackedInstances.OfType <IDisposable>().Reverse()) { disposable.Dispose(); } }; foreach (var disposable in c.TrackedInstances.OfType <IInitializable>()) { disposable.Initialize(); } return(fakeBus); }); injectionist.Register(c => new Disposable1(c.Get <Disposable2>(), c.Get <EventTracker>())); injectionist.Register(c => new Disposable2(c.Get <EventTracker>())); injectionist.Register(c => eventTracker); using (var bus = injectionist.Get <FakeBus>().Instance) { Console.WriteLine("Using the bus...."); Console.WriteLine("Disposing it"); } Console.WriteLine(@"Here's what happened: {0}", string.Join(Environment.NewLine, eventTracker.Events.Select(e => "- " + e))); Assert.That(eventTracker.Events, Is.EqualTo(new[] { "EventTracker initialized", "Disposable2 initialized", "Disposable1 initialized", "Disposable1 disposed", "Disposable2 disposed", "EventTracker disposed", "FakeBus disposed", })); }
public void DoesNotReturnSameInstanceTwice() { var injectionist = new Injection.Injectionist(); injectionist.Register(c => new Something()); injectionist.Decorate(c => c.Get <Something>()); var result = injectionist.Get <Something>(); Assert.That(result.TrackedInstances.OfType <Something>().Count(), Is.EqualTo(1)); }
public void CanGetResolvedInstancesOfSomeParticularType() { var injectionist = new Injection.Injectionist(); injectionist.Decorate <IService>(c => new Decorator("2", c.Get <IService>())); injectionist.Decorate <IService>(c => new Decorator("3", c.Get <IService>())); injectionist.Decorate <IService>(c => new Decorator("4", c.Get <IService>())); injectionist.Register <IService>(c => new Primary("1")); var result = injectionist.Get <IService>(); var instanceNames = result.TrackedInstances .OfType <INamed>() .Select(n => n.Name) .ToList(); Console.WriteLine($@"Instance names: {string.Join(Environment.NewLine, instanceNames.Select(name => $" {name}"))}"); Assert.That(instanceNames, Is.EqualTo(new[] { "1", "2", "3", "4" })); }