Пример #1
0
        public void Enumerable_ShouldCreateSameScopeds()
        {
            var container = new ScopedContainer();

            container.RegisterScoped <BaseClass>();
            container.RegisterScoped <BaseClassChild, BaseClass>();
            container.RegisterScoped <BaseClassGrandchild, BaseClass>();


            var last   = container.Resolve <BaseClass>();
            var result = container.ResolveMany <BaseClass>().ToArray();

            Assert.NotSame(result[0], result[1]);
            Assert.NotSame(result[1], result[2]);
            Assert.Same(last, result[2]);
        }
Пример #2
0
        public void EagerEnumerable_ShouldCreateSameScopeds()
        {
            var targets = CreateTargetContainer();

            targets.SetOption <Options.LazyEnumerables, BaseClass>(false);
            var container = new ScopedContainer(targets);

            container.RegisterScoped <BaseClass>();
            container.RegisterScoped <BaseClassChild, BaseClass>();
            container.RegisterScoped <BaseClassGrandchild, BaseClass>();


            var last   = container.Resolve <BaseClass>();
            var result = container.ResolveMany <BaseClass>().ToArray();

            Assert.NotSame(result[0], result[1]);
            Assert.NotSame(result[1], result[2]);
            Assert.Same(last, result[2]);
        }
Пример #3
0
        public void ScopeShouldCreateOnlyOneScopedObject()
        {
            // <example1>
            using (var container = new ScopedContainer())
            {
                container.RegisterScoped <MyService, IMyService>();

                var result1 = container.Resolve <IMyService>();
                var result2 = container.Resolve <IMyService>();

                Assert.Same(result1, result2);
            }
            // </example1>
        }
Пример #4
0
 public void ScopeShouldCreateDifferentScopedObjectsFromItsParent()
 {
     // <example2>
     using (var container = new ScopedContainer())
     {
         container.RegisterScoped <MyService, IMyService>();
         var        result1 = container.Resolve <IMyService>();
         IMyService result2;
         using (var scope = container.CreateScope())
         {
             result2 = scope.Resolve <IMyService>();
             Assert.NotSame(result1, result2);
         }
     }
     // </example2>
 }
        public void ShouldInjectEnumerableWithItemsWithDifferentLifetimes()
        {
            // <example4>
            // since we're using a scoped registration here,
            // we'll use the ScopedContainer, which establishes
            // a root scope.
            var container = new ScopedContainer();

            container.RegisterSingleton <MyService1, IMyService>();
            container.RegisterScoped <MyService2, IMyService>();
            container.RegisterType <MyService3, IMyService>();

            // So - each enumerable will contain, in order:
            // 1) Singleton IMyService
            // 2) Scoped IMyService
            // 3) Transient IMyService

            var fromRoot1 = container.ResolveMany <IMyService>().ToArray();
            var fromRoot2 = container.ResolveMany <IMyService>().ToArray();

            Assert.Same(fromRoot1[0], fromRoot2[0]);
            // both scoped objects should be the same because we've resolved
            // from the root scope
            Assert.Same(fromRoot1[1], fromRoot2[1]);
            Assert.NotSame(fromRoot1[2], fromRoot2[2]);

            using (var childScope = container.CreateScope())
            {
                var fromChildScope1 = childScope.ResolveMany <IMyService>().ToArray();
                // singleton should be the same as before, but
                // the scoped object will be different
                Assert.Same(fromRoot1[0], fromChildScope1[0]);
                Assert.NotSame(fromRoot1[1], fromChildScope1[1]);
                Assert.NotSame(fromRoot1[2], fromChildScope1[2]);

                var fromChildScope2 = childScope.ResolveMany <IMyService>().ToArray();
                // the scoped object will be the same as above
                Assert.Same(fromChildScope1[0], fromChildScope2[0]);
                Assert.Same(fromChildScope1[1], fromChildScope2[1]);
                Assert.NotSame(fromChildScope1[2], fromChildScope2[2]);
            }
            // </example4>
        }