Exemple #1
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>
        }
        public void ShouldDispose()
        {
            // <example11>
            var disposableObj = new DisposableType();

            using (var container = new ScopedContainer())
            {
                container.RegisterObject(disposableObj, scopeBehaviour: ScopeBehaviour.Explicit);
                var result = container.Resolve <DisposableType>();
                Assert.Same(disposableObj, result);
            }
            // Should be disposed
            Assert.True(disposableObj.Disposed);
            // </example11>
        }
        public void ShouldNotDisposeByDefault()
        {
            // <example10>
            var disposableObj = new DisposableType();

            using (var container = new ScopedContainer())
            {
                container.RegisterObject(disposableObj);
                var result = container.Resolve <DisposableType>();
                Assert.Same(disposableObj, result);
            }
            // Should NOT be disposed
            Assert.False(disposableObj.Disposed);
            // </example10>
        }
        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]);
        }
Exemple #5
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 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]);
        }
        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>
        }