Example #1
0
        internal static TImplementation GetInstance <TImplementation>(
            ScopedRegistration <TImplementation> registration, Scope scope)
            where TImplementation : class
        {
            if (scope == null)
            {
                return(GetScopelessInstance(registration));
            }

            return(scope.GetInstanceInternal(registration));
        }
Example #2
0
        private static object GetScopelessInstance(ScopedRegistration registration)
        {
            if (registration.Container.IsVerifying)
            {
                return(registration.Container.VerificationScope !.GetInstanceInternal(registration));
            }

            throw new ActivationException(
                      StringResources.TheServiceIsRequestedOutsideTheContextOfAScopedLifestyle(
                          registration.ImplementationType,
                          registration.Lifestyle));
        }
Example #3
0
        private static TImplementation GetScopelessInstance <TImplementation>(
            ScopedRegistration <TImplementation> registration)
            where TImplementation : class
        {
            if (registration.Container.IsVerifying)
            {
                return(registration.Container.VerificationScope.GetInstanceInternal(registration));
            }

            throw new ActivationException(
                      StringResources.TheServiceIsRequestedOutsideTheContextOfAScopedLifestyle(
                          typeof(TImplementation),
                          registration.Lifestyle));
        }
Example #4
0
        private TImplementation CreateAndCacheInstance <TImplementation>(
            ScopedRegistration <TImplementation> registration)
            where TImplementation : class
        {
            TImplementation instance = registration.InstanceCreator.Invoke();

            this.cachedInstances[registration] = instance;

            if (instance is IDisposable disposable)
            {
                this.RegisterForDisposalInternal(disposable);
            }

            return(instance);
        }
Example #5
0
        internal static TService GetInstance <TService, TImplementation>(
            ScopedRegistration <TService, TImplementation> registration, Scope scope)
            where TImplementation : class, TService
            where TService : class
        {
            if (scope == null)
            {
                return(GetScopelessInstance(registration));
            }

            lock (scope.syncRoot)
            {
                return(scope.GetInstance(registration));
            }
        }
Example #6
0
        private TImplementation CreateAndCacheInstance <TImplementation>(
            ScopedRegistration <TImplementation> registration, Dictionary <Registration, object> cache)
            where TImplementation : class
        {
            // registration.BuildExpression has been called, and InstanceCreate thus been initialized.
            Func <TImplementation> instanceCreator = registration.InstanceCreator !;

            TImplementation instance = instanceCreator.Invoke();

            cache[registration] = instance;

            if (instance is IDisposable disposable && !registration.SuppressDisposal)
            {
                this.RegisterForDisposalInternal(disposable);
            }

            return(instance);
        }
Example #7
0
        private object GetInstanceInternal(ScopedRegistration registration)
        {
            lock (this.syncRoot)
            {
                this.RequiresInstanceNotDisposed();

                bool cacheIsEmpty = this.cachedInstances is null;

                if (this.cachedInstances is null)
                {
                    this.cachedInstances =
                        new Dictionary <Registration, object>(ReferenceEqualityComparer <Registration> .Instance);
                }

                return(!cacheIsEmpty && this.cachedInstances.TryGetValue(registration, out object?instance)
                    ? instance
                    : this.CreateAndCacheInstance(registration, this.cachedInstances));
            }
        }
Example #8
0
        private TService CreateAndCacheInstance <TService, TImplementation>(
            ScopedRegistration <TService, TImplementation> registration)
            where TService : class
            where TImplementation : class, TService
        {
            TService service = registration.InstanceCreator.Invoke();

            this.cachedInstances[registration] = service;

            if (registration.RegisterForDisposal)
            {
                var disposable = service as IDisposable;

                if (disposable != null)
                {
                    this.RegisterForDisposalInternal(disposable);
                }
            }

            return(service);
        }
Example #9
0
        private TService GetInstance <TService, TImplementation>(
            ScopedRegistration <TService, TImplementation> registration)
            where TService : class
            where TImplementation : class, TService
        {
            this.RequiresInstanceNotDisposed();

            bool cacheIsEmpty = this.cachedInstances == null;

            if (this.cachedInstances == null)
            {
                this.cachedInstances =
                    new Dictionary <Registration, object>(ReferenceEqualityComparer <Registration> .Instance);
            }

            object instance;

            return(!cacheIsEmpty && this.cachedInstances.TryGetValue(registration, out instance)
                ? (TService)instance
                : this.CreateAndCacheInstance(registration));
        }
Example #10
0
        private TImplementation GetInstanceInternal <TImplementation>(
            ScopedRegistration <TImplementation> registration)
            where TImplementation : class
        {
            lock (this.syncRoot)
            {
                this.RequiresInstanceNotDisposed();

                bool cacheIsEmpty = this.cachedInstances == null;

                if (this.cachedInstances == null)
                {
                    this.cachedInstances =
                        new Dictionary <Registration, object>(ReferenceEqualityComparer <Registration> .Instance);
                }

                return(!cacheIsEmpty && this.cachedInstances.TryGetValue(registration, out object?instance)
                    ? (TImplementation)instance
                    : this.CreateAndCacheInstance(registration, this.cachedInstances));
            }
        }
Example #11
0
        private object CreateAndCacheInstance(
            ScopedRegistration registration, Dictionary <Registration, object> cache)
        {
            // registration.BuildExpression has been called, and InstanceCreate thus been initialized.
            Func <object> instanceCreator = registration.InstanceCreator !;

            object instance = instanceCreator.Invoke();

            cache[registration] = instance;

            if (!registration.SuppressDisposal)
            {
#if NETSTANDARD1_0 || NETSTANDARD1_3 || NET45
                if (instance is IDisposable)
#else
                if (instance is IDisposable || instance is IAsyncDisposable)
#endif
                {
                    this.RegisterForDisposalInternal(instance);
                }
            }

            return(instance);
        }