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

            using (var scope = container.CreateScope())
            {
                var result = scope.Resolve <IEnumerable <Action <BaseClassChild> > >();

                Assert.Empty(result);
            }
        }
Пример #2
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>
        }
Пример #4
0
        public void OnlyRootScopeShouldDispose()
        {
            // <example12>
            var disposableObj = new DisposableType();

            using (var container = new ScopedContainer())
            {
                container.RegisterObject(disposableObj, scopeBehaviour: ScopeBehaviour.Explicit);

                using (var scope = container.CreateScope())
                {
                    var result = container.Resolve <DisposableType>();
                    Assert.Same(disposableObj, result);
                }
                // Should not be disposed here...
                Assert.False(disposableObj.Disposed);
            }
            // ... but should be disposed here
            Assert.True(disposableObj.Disposed);
            // </example12>
        }
        public void DisposableSingletonShouldBeDisposedByRootScope()
        {
            // <example4>
            // In this example we use the disposable ScopedContainer, which
            // supports all the same functionality as 'Container' except it
            // also has its own scope, and is therefore disposable.
            DisposableType result;

            using (var container = new ScopedContainer())
            {
                container.RegisterSingleton <DisposableType>();
                using (var scope = container.CreateScope())
                {
                    // singletons force tracking in the
                    // rootmost scope of a scope hierarchy
                    result = scope.Resolve <DisposableType>();
                }
                Assert.False(result.Disposed);
            }
            Assert.True(result.Disposed);
            // </example4>
        }