public void Enumerable_ShouldCreateEagerLoaded_GlobalOption() { // similar to above test, but this time, all instances should be created up front // and should not be recreated every time we enumerate. using (var session = InstanceCountingType.NewSession()) { var targets = CreateTargetContainer(); targets.SetOption <Options.LazyEnumerables>(false); targets.RegisterType <InstanceCountingType>(); targets.RegisterType <InstanceCountingType>(); targets.RegisterType <InstanceCountingType>(); var container = CreateContainer(targets); var result = container.Resolve <IEnumerable <InstanceCountingType> >(); Assert.NotNull(result); Assert.Equal(3, session.InstanceCount); var array1 = result.ToArray(); var array2 = result.ToArray(); Assert.Equal(3, session.InstanceCount); } }
public void DelegateTarget_ShouldExecuteEachResolveCall() { var targets = CreateTargetContainer(); ITarget target = Target.ForDelegate(() => new InstanceCountingType()); targets.Register(target); var container = CreateContainer(targets); using (var session = InstanceCountingType.NewSession()) { var result = container.Resolve <InstanceCountingType>(); Assert.Equal(session.InitialInstanceCount + 1, result.ThisInstanceID); var result2 = container.Resolve <InstanceCountingType>(); Assert.Equal(session.InitialInstanceCount + 2, result2.ThisInstanceID); } }
public void SingletonTarget_ShouldOnlyCreateOneInstance_ViaConstructorTarget() { var targets = CreateTargetContainer(); //RegisterSingleton simply creates ConstructorTarget wrapped inside a singleton targets.RegisterSingleton <InstanceCountingType>(); var container = CreateContainer(targets); using (var session = InstanceCountingType.NewSession()) { var result1 = container.Resolve <InstanceCountingType>(); var result2 = container.Resolve <InstanceCountingType>(); Assert.Same(result1, result2); } }
public void SingletonTarget_ShouldCreateOneInstanceForCovariantEnumerableAndSingle() { var targets = CreateTargetContainer(); targets.RegisterSingleton <InstanceCountingType>(); var container = CreateContainer(targets); using (var session = InstanceCountingType.NewSession()) { var enumerable = container.ResolveMany <IInstanceCountingType>().ToArray(); var single = container.Resolve <InstanceCountingType>(); Assert.Equal(1, session.InstanceCount); Assert.Same(enumerable[0], single); } }
public void Alias_ShouldYieldSameSingleton() { //this test can fail if the compiler is not working properly for singletons, too var targets = CreateTargetContainer(); targets.RegisterSingleton <InstanceCountingType>(); targets.RegisterAlias <IInstanceCountingType, InstanceCountingType>(); using (var session = InstanceCountingType.NewSession()) { var container = CreateContainer(targets); var first = container.Resolve <InstanceCountingType>(); var second = container.Resolve <IInstanceCountingType>(); Assert.Equal(session.InitialInstanceCount + 1, first.ThisInstanceID); Assert.Equal(first.ThisInstanceID, second.ThisInstanceID); Assert.Same(first, second); } }
public void SingletonTarget_ShouldInjectSingleton() { var targets = CreateTargetContainer(); targets.RegisterSingleton <InstanceCountingType>(); targets.RegisterType <RequiresInstanceCountingType>(); var container = CreateContainer(targets); using (var session = InstanceCountingType.NewSession()) { var result = container.Resolve <RequiresInstanceCountingType>(); Assert.Equal(session.InitialInstanceCount + 1, result.Instance.ThisInstanceID); var result2 = container.Resolve <RequiresInstanceCountingType>(); Assert.NotSame(result, result2); Assert.Equal(result.Instance.ThisInstanceID, result2.Instance.ThisInstanceID); } }
public void Enumerable_ShouldCreateLazyLoaded() { using (var session = InstanceCountingType.NewSession()) { var targets = CreateTargetContainer(); targets.RegisterType <InstanceCountingType>(); targets.RegisterType <InstanceCountingType>(); targets.RegisterType <InstanceCountingType>(); var container = CreateContainer(targets); var result = container.Resolve <IEnumerable <InstanceCountingType> >(); Assert.NotNull(result); Assert.Equal(0, session.InstanceCount); var array1 = result.ToArray(); var array2 = result.ToArray(); Assert.Equal(6, session.InstanceCount); } }
public void Enumerable_ShouldCreateEagerLoaded_OneTypeOnly() { // Demonstrating that we can control lazy enumerables on a per-type basis using (var session1 = InstanceCountingType.NewSession()) { using (var session2 = InstanceCountingType2.NewSession()) { var targets = CreateTargetContainer(); targets.SetOption <Options.LazyEnumerables, InstanceCountingType2>(false); targets.RegisterType <InstanceCountingType>(); targets.RegisterType <InstanceCountingType>(); targets.RegisterType <InstanceCountingType>(); targets.RegisterType <InstanceCountingType2>(); targets.RegisterType <InstanceCountingType2>(); targets.RegisterType <InstanceCountingType2>(); var container = CreateContainer(targets); var result1 = container.Resolve <IEnumerable <InstanceCountingType> >(); var result2 = container.Resolve <IEnumerable <InstanceCountingType2> >(); Assert.NotNull(result1); Assert.NotNull(result2); Assert.Equal(0, session1.InstanceCount); Assert.Equal(3, session2.InstanceCount); var array1a = result1.ToArray(); var array1b = result1.ToArray(); var array2a = result2.ToArray(); var array2b = result2.ToArray(); Assert.Equal(6, session1.InstanceCount); Assert.Equal(3, session2.InstanceCount); } } }
public void SingletonTarget_ShouldOnlyResolveOneInstance_ViaResolvedTarget() { //little bit wacky - register the instance counting type as before, //but register a ResolvedTarget (wrapped in a singleton) for a base of //the same type. This is basically how aliasing works (just without //the singleton) //when resolving directly, we should always get a new instance //when resolving via the base, we should only ever get one. var targets = CreateTargetContainer(); targets.RegisterType <InstanceCountingType>(); //now register the ResolvedTarget singleton against IInstanceCountingType targets.Register(new ResolvedTarget(typeof(InstanceCountingType)).Singleton(), typeof(IInstanceCountingType)); var container = CreateContainer(targets); using (var session = InstanceCountingType.NewSession()) { //although we're not testing 'normal' transient objects - we need some //assertions on this part of the test to ensure reliable results. var direct1 = container.Resolve <InstanceCountingType>(); var direct2 = container.Resolve <InstanceCountingType>(); Assert.NotSame(direct1, direct2); var currentInstanceCount = session.InitialInstanceCount + 2; Assert.Equal(currentInstanceCount, InstanceCountingType.InstanceCount); //Right - on to the main course. //This should create a new instance, because the singleton is wrapped around //the inner ResolveTarget. var indirect1 = container.Resolve <IInstanceCountingType>(); Assert.NotEqual(currentInstanceCount, indirect1.ThisInstanceID); var indirect2 = container.Resolve <IInstanceCountingType>(); Assert.Same(indirect1, indirect2); } }
public RequiresInstanceCountingType(InstanceCountingType instance) { Instance = instance; }