Exemple #1
0
 /// <summary>
 /// Adds a pool instance to using list
 /// This instance will be released while disposing
 /// </summary>
 public void UsePoolItem(IServicePool pool, PoolServiceDescriptor descriptor)
 {
     lock (_poolInstances)
     {
         if (_poolInstances.ContainsKey(pool))
         {
             if (pool.Type == ImplementationType.Transient)
             {
                 _poolInstances[pool].Add(descriptor);
             }
         }
         else
         {
             _poolInstances.Add(pool, new List <PoolServiceDescriptor> {
                 descriptor
             });
         }
     }
 }
        private object GetFromPool(BuiltServiceDescriptor descriptor, IContainerScope scope)
        {
            IServicePool          pool  = (IServicePool)descriptor.Instance;
            PoolServiceDescriptor pdesc = pool.GetAndLock(scope);

            if (pdesc == null)
            {
                throw new NullReferenceException($"{descriptor.ServiceType.ToTypeString()} Service is not registered in the container");
            }

            if (pool.Type == ImplementationType.Scoped && scope == null)
            {
                throw new ScopeException($"{descriptor.ServiceType.ToTypeString()} is registered as Scoped but scope parameter is null for IServiceContainer.Get method");
            }

            if (scope != null)
            {
                scope.UsePoolItem(pool, pdesc);
            }

            return(pdesc.GetInstance());
        }
Exemple #3
0
        private async Task <object> Get(ServiceDescriptor descriptor, IContainerScope scope = null)
        {
            if (descriptor.IsPool)
            {
                IServicePool          pool  = (IServicePool)descriptor.Instance;
                PoolServiceDescriptor pdesc = await pool.GetAndLock(scope);

                if (pdesc == null)
                {
                    throw new NullReferenceException("Could not get service from container");
                }

                if (pool.Type == ImplementationType.Scoped && scope == null)
                {
                    throw new InvalidOperationException("Type is registered as Scoped but scope parameter is null for IServiceContainer.Get method");
                }

                if (scope != null)
                {
                    scope.UsePoolItem(pool, pdesc);
                }

                return(pdesc.GetInstance());
            }

            switch (descriptor.Implementation)
            {
            //create new instance
            case ImplementationType.Transient:
                object transient = await CreateInstance(descriptor.ImplementationType, scope);

                if (descriptor.AfterCreatedMethod != null)
                {
                    descriptor.AfterCreatedMethod.DynamicInvoke(transient);
                }

                if (descriptor.ProxyType != null)
                {
                    IServiceProxy p = (IServiceProxy) await CreateInstance(descriptor.ProxyType, scope);

                    return(p.Proxy(transient));
                }

                return(transient);

            case ImplementationType.Scoped:

                if (scope == null)
                {
                    throw new InvalidOperationException("Type is registered as Scoped but scope parameter is null for IServiceContainer.Get method");
                }

                return(await scope.Get(descriptor, this));

            case ImplementationType.Singleton:
                //if instance already created return
                if (descriptor.Instance != null)
                {
                    return(descriptor.Instance);
                }

                //create instance for first time and set Instance property of descriptor to prevent re-create for next times
                object instance = await CreateInstance(descriptor.ImplementationType, scope);

                if (descriptor.AfterCreatedMethod != null)
                {
                    descriptor.AfterCreatedMethod.DynamicInvoke(instance);
                }

                if (descriptor.ProxyType != null)
                {
                    IServiceProxy p = (IServiceProxy) await CreateInstance(descriptor.ProxyType, scope);

                    object proxyObject = p.Proxy(instance);
                    descriptor.Instance = proxyObject;
                }
                else
                {
                    descriptor.Instance = instance;
                }

                return(descriptor.Instance);

            default:
                return(null);
            }
        }