Exemplo n.º 1
0
        protected IReuse ConvertLifetimeToReuse(LifetimeType lifetime)
        {
            switch (lifetime)
            {
            case LifetimeType.Transient:
                return(Reuse.Transient);

            case LifetimeType.Singleton:
                return(Reuse.InCurrentNamedScope(Container.NonAmbientRootScopeName));

            case LifetimeType.Scoped:
                return(Reuse.InCurrentScope);

            default:
                throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null);
            }
        }
Exemplo n.º 2
0
        public void Unable_to_resolve_in_case_of_no_scope()
        {
            var container = new Container();

            container.Register <X>();
            container.Register <Y>(Reuse.InCurrentNamedScope("specialScope"));

            using (var scope = container.OpenScope("luckyScope"))
            {
                var ex = Assert.Throws <ContainerException>(() =>
                                                            scope.Resolve <X>());

                Assert.AreEqual(
                    Error.NameOf(Error.NoMatchedScopeFound),
                    Error.NameOf(ex.Error));
            }
        }
Exemplo n.º 3
0
        public void Open_context_independent_named_scope()
        {
            var container = new Container();

            container.Register <Blah>(Reuse.InCurrentNamedScope("hey"));

            using (var scope = container.OpenScope("hey"))
            {
                var blah = scope.Resolve <Blah>();
                Assert.AreSame(blah, scope.Resolve <Blah>());

                using (var scope2 = ((Container)scope).OpenScope())
                    Assert.AreSame(blah, scope2.Resolve <Blah>());
            }

            container.Dispose();
        }
Exemplo n.º 4
0
        private ServiceContainer(ServiceContainer parent, string scopeName)
        {
            if (parent != null)
            {
                container = parent.container.OpenScope(scopeName)
                            .With(rules => rules.WithDefaultReuseInsteadOfTransient(Reuse.InCurrentNamedScope(scopeName)));

                ScopeName = scopeName;
            }
            else
            {
                container = new Container(Rules.Default
                                          .WithDefaultReuseInsteadOfTransient(Reuse.Singleton)
                                          .WithoutThrowOnRegisteringDisposableTransient());
            }

            registrationProviders = new List <IRegistrationConfigurationProvider>();
        }
        public void Should_work_as_is()
        {
            var container = new Container();

            container.Register <IAction, ActionOne>(Reuse.InCurrentNamedScope("1"));
            container.Register <IAction, ActionTwo>(Reuse.InCurrentNamedScope("2"));
            container.Register <Service>();

            using (var scopeOne = container.OpenScope("1"))
            {
                scopeOne.Resolve <Service>();
            }

            using (var scopeTwo = container.OpenScope("2"))
            {
                scopeTwo.Resolve <Service>();
            }
        }
        public void Should_work_with_asResolutionCall()
        {
            var container = new Container();

            container.Register <IAction, ActionOne>(Reuse.InCurrentNamedScope("1"), setup: Setup.With(asResolutionCall: true));
            container.Register <IAction, ActionTwo>(Reuse.InCurrentNamedScope("2"), setup: Setup.With(asResolutionCall: true));
            container.Register <Service>();

            using (var scopeOne = container.OpenScope("1"))
            {
                scopeOne.Resolve <Service>();
            }

            using (var scopeTwo = container.OpenScope("2"))
            {
                scopeTwo.Resolve <Service>();
            }
        }
Exemplo n.º 7
0
        public void Cache_should_not_affect_results_for_lazy_enumerable()
        {
            var container = new Container(rules => rules.WithResolveIEnumerableAsLazyEnumerable());

            container.Register <IAction, ActionOne>(Reuse.InCurrentNamedScope("A"));
            container.Register <IAction, ActionTwo>(Reuse.InCurrentNamedScope("B"));

            using (var scopeA = container.OpenScope("A"))
            {
                var scopedActions = scopeA.Resolve <IEnumerable <IAction> >().ToArray();
                Assert.AreEqual(1, scopedActions.Length);
                Assert.IsInstanceOf <ActionOne>(scopedActions[0]);
            }

            var actions = container.Resolve <IEnumerable <IAction> >().ToArray();

            Assert.AreEqual(0, actions.Length);
        }
Exemplo n.º 8
0
        public void Register(ServiceRegistration registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }

            if (container == null)
            {
                throw new InvalidOperationException("The container was not initialized.");
            }

            try {
                lock (this) {
                    var serviceType        = registration.ServiceType;
                    var service            = registration.Instance;
                    var serviceName        = registration.ServiceKey;
                    var implementationType = registration.ImplementationType;

                    var reuse = Reuse.Transient;
                    if (!String.IsNullOrWhiteSpace(ScopeName))
                    {
                        reuse = Reuse.InCurrentNamedScope(ScopeName);
                    }
                    if (!String.IsNullOrEmpty(registration.Scope))
                    {
                        reuse = Reuse.InCurrentNamedScope(registration.Scope);
                    }

                    if (service == null)
                    {
                        container.Register(serviceType, implementationType, serviceKey: serviceName, reuse: reuse);
                    }
                    else
                    {
                        container.RegisterInstance(serviceType, service, serviceKey: serviceName, reuse: reuse);
                    }
                }
            } catch (ServiceException) {
                throw;
            } catch (Exception ex) {
                throw new ServiceException("Error when registering service.", ex);
            }
        }
        public void Reuse_can_select_scope_with_specific_name()
        {
            var container = new Container(scopeContext: new AsyncExecutionFlowScopeContext());

            container.Register <Blah>(Reuse.InCurrentNamedScope(1));

            using (var s1 = container.OpenScope(1))
            {
                var blah1 = s1.Resolve <Blah>();

                using (var s2 = s1.OpenScope(2))
                {
                    var blah2 = s2.Resolve <Blah>();
                    Assert.AreSame(blah1, blah2);
                }

                Assert.AreSame(blah1, s1.Resolve <Blah>());
            }
        }
Exemplo n.º 10
0
        public void Register(ServiceRegistration registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException("registration");
            }

            if (container == null)
            {
                throw new InvalidOperationException("The container was not initialized.");
            }

            lock (this) {
                var serviceType        = registration.ServiceType;
                var service            = registration.Instance;
                var serviceName        = registration.ServiceKey;
                var implementationType = registration.ImplementationType;

                var reuse = Reuse.Singleton;
                if (!String.IsNullOrEmpty(ScopeName))
                {
                    reuse = Reuse.InCurrentNamedScope(ScopeName);
                }

                if (!String.IsNullOrEmpty(registration.Scope))
                {
                    reuse = Reuse.InCurrentNamedScope(registration.Scope);
                }

                if (service == null)
                {
                    container.Register(serviceType, implementationType, serviceKey: serviceName, reuse: reuse);
                }
                else
                {
                    container.RegisterInstance(serviceType, service, serviceKey: serviceName, reuse: reuse);
                }
            }
        }
        public void Test()
        {
            var container = new Container(Rules.Default
                                          // What is the scopeName? How it relates to scopes below?
                                          //.WithDefaultReuseInsteadOfTransient(Reuse.InCurrentNamedScope(scopeName))
                                          .With(propertiesAndFields: PropertiesAndFields.Auto)
                                          .WithoutThrowOnRegisteringDisposableTransient());

            container.Register(typeof(IFoo), typeof(Foo), Reuse.InCurrentNamedScope("Parent"));
            container.Register(typeof(IBar), typeof(Bar), Reuse.InCurrentNamedScope("Child"));

            var parentScope = container.OpenScope("Parent");
            var childScope  = parentScope.OpenScope("Child");

            var bar = childScope.Resolve <IBar>();

            Assert.IsNotNull(bar);
            Assert.IsNotNull(bar.Foo);

            var parentFoo = parentScope.Resolve <IFoo>();

            Assert.IsNotNull(parentFoo);
        }