Exemplo n.º 1
0
        protected override IContainerExtension CreateContainerExtension()
        {
            var unity     = new UnityContainer().AddExtension(new Diagnostic());
            var container = new UnityContainerExtension(unity);
            var mainScope = new ScopedContainer(container, unity);

            container.RegisterInstance <IScopedContainer>(mainScope);
            return(container);
        }
Exemplo n.º 2
0
        public void Enumerable_ShouldWorkFromScopedContainer()
        {
            var container = new ScopedContainer();

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

                Assert.Empty(result);
            }
        }
Exemplo n.º 3
0
        protected override IContainerExtension CreateContainerExtension()
        {
            IUnityContainer unity = new UnityContainer();

            #if DEBUG
            unity = unity.AddExtension(new Diagnostic());
            #endif
            var container = new UnityContainerExtension(unity);
            var mainScope = new ScopedContainer(container, unity);
            container.RegisterInstance <IScopedContainer>(mainScope);
            ViewBind.ContainerProvider = container;
            return(container);
        }
Exemplo n.º 4
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>
        }
        protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection)
        {
            //Just proving my comments on https://github.com/aspnet/DependencyInjection/issues/589
            var config = TargetContainer
                         .DefaultConfig
                         .Clone()
                         .ConfigureOption <Options.LazyEnumerables>(false);

            var container = new ScopedContainer(
                new TargetContainer(config));

            //var container = new ScopedContainer();
            container.Populate(serviceCollection);
            return(container);
        }
Exemplo n.º 6
0
        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>
        }
Exemplo n.º 7
0
        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>
        }
Exemplo n.º 8
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]);
        }
Exemplo n.º 9
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>
 }
        /// <summary>
        /// Creates a new default <see cref="ScopedContainer"/> and registers the services in <paramref name="services"/>
        /// as targets.
        /// </summary>
        /// <param name="services">The services to be registered.</param>
        /// <returns>An <see cref="Container"/> instance</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="services"/> is null.</exception>
        /// <remarks>The <see cref="Rezolver.Options.LazyEnumerables"/> option (is set to <c>false</c> by this method,
        /// because v2.0 of .Net Core 2 seems to expect all enumerables to be eagerly loaded - getting clarification
        /// on this from the team at https://github.com/aspnet/DependencyInjection/issues/589</remarks>
        public static Container CreateRezolverContainer(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            var targetContainer = new TargetContainer(
                TargetContainer.DefaultConfig
                .Clone()
                .ConfigureOption <Rezolver.Options.LazyEnumerables>(false)
                );

            var c = new ScopedContainer(targetContainer);

            c.Populate(services);
            return(c);
        }
Exemplo n.º 11
0
        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>
        }
Exemplo n.º 12
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]);
        }
Exemplo n.º 13
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>
        }