Beispiel #1
0
        private ServiceEntry <TService, TFunc> SetServiceEntry <TService, TFunc>(ServiceKey key, ServiceEntry <TService, TFunc> entry)
        {
            lock (services)
            {
                services[key] = entry;
                Interlocked.Exchange(ref servicesReadOnlyCopy, null);
            }

            return(entry);
        }
Beispiel #2
0
        protected virtual ServiceEntry <TService, TFunc> GetEntry <TService, TFunc>(string serviceName, bool throwIfMissing)
        {
            try
            {
                TService resolved;
                if (CheckAdapterFirst &&
                    Adapter != null &&
                    typeof(TService) != typeof(IRequest) &&
                    !Equals(default(TService), (resolved = Adapter.TryResolve <TService>())))
                {
                    return(new ServiceEntry <TService, TFunc>(
                               (TFunc)(object)(Func <Container, TService>)(c => resolved))
                    {
                        Owner = DefaultOwner,
                        Container = this,
                    });
                }
            }
            catch (Exception ex)
            {
                throw CreateAdapterException <TService>(ex);
            }

            var          key       = new ServiceKey(typeof(TFunc), serviceName);
            ServiceEntry entry     = null;
            Container    container = this;

            // Go up the hierarchy always for registrations.
            while (!container.TryGetServiceEntry(key, out entry) && container.parent != null)
            {
                container = container.parent;
            }

            if (entry == null)
            {
                var genericDef = typeof(TService).FirstGenericTypeDefinition();
                if (genericDef != null && genericDef.Name.StartsWith("Func`")) //Lazy Dependencies
                {
                    var argTypes     = typeof(TService).GetGenericArguments();
                    var lazyResolver = GetLazyResolver(argTypes);

                    return(new ServiceEntry <TService, TFunc>(
                               (TFunc)(object)(Func <Container, TService>)(c => (TService)lazyResolver))
                    {
                        Owner = DefaultOwner,
                        Container = this,
                    });
                }
            }

            if (entry != null)
            {
                if (entry.Reuse == ReuseScope.Container && entry.Container != this)
                {
                    entry = SetServiceEntry(key, ((ServiceEntry <TService, TFunc>)entry).CloneFor(this));
                }
            }
            else
            {
                try
                {
                    //i.e. if called Resolve<> for Constructor injection
                    if (throwIfMissing)
                    {
                        if (Adapter != null)
                        {
                            return(new ServiceEntry <TService, TFunc>(
                                       (TFunc)(object)(Func <Container, TService>)(c => Adapter.Resolve <TService>()))
                            {
                                Owner = DefaultOwner,
                                Container = this,
                            });
                        }
                        // ThrowMissing<TService>(serviceName);
                    }
                    else
                    {
                        if (Adapter != null &&
                            (typeof(TService) != typeof(IRequest)))
                        {
                            //Container.Exists<> needs to return null if no dependency exists
                            var instance = Adapter.TryResolve <TService>();
                            if (instance == null)
                            {
                                return(null);
                            }

                            return(new ServiceEntry <TService, TFunc>(
                                       (TFunc)(object)(Func <Container, TService>)(c => instance))
                            {
                                Owner = DefaultOwner,
                                Container = this,
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw CreateAdapterException <TService>(ex);
                }
            }

            return((ServiceEntry <TService, TFunc>)entry);
        }
Beispiel #3
0
        private ServiceEntry <TService, TFunc> GetEntry <TService, TFunc>(string serviceName, bool throwIfMissing)
        {
            try
            {
                TService resolved;
                if (CheckAdapterFirst &&
                    Adapter != null &&
                    typeof(TService) != typeof(IRequest) &&
                    !Equals(default(TService), (resolved = Adapter.TryResolve <TService>())))
                {
                    return(new ServiceEntry <TService, TFunc>(
                               (TFunc)(object)(Func <Container, TService>)(c => resolved))
                    {
                        Owner = DefaultOwner,
                        Container = this,
                    });
                }
            }
            catch (Exception ex)
            {
                throw CreateAdapterException <TService>(ex);
            }

            var          key       = new ServiceKey(typeof(TFunc), serviceName);
            ServiceEntry entry     = null;
            Container    container = this;

            // Go up the hierarchy always for registrations.
            while (!container.TryGetServiceEntry(key, out entry) && container.parent != null)
            {
                container = container.parent;
            }

            if (entry != null)
            {
                if (entry.Reuse == ReuseScope.Container && entry.Container != this)
                {
                    entry = SetServiceEntry(key, ((ServiceEntry <TService, TFunc>)entry).CloneFor(this));
                }
            }
            else
            {
                try
                {
                    //i.e. if called Resolve<> for Constructor injection
                    if (throwIfMissing)
                    {
                        if (Adapter != null)
                        {
                            return(new ServiceEntry <TService, TFunc>(
                                       (TFunc)(object)(Func <Container, TService>)(c => Adapter.Resolve <TService>()))
                            {
                                Owner = DefaultOwner,
                                Container = this,
                            });
                        }
                        ThrowMissing <TService>(serviceName);
                    }
                    else
                    {
                        if (Adapter != null &&
                            (typeof(TService) != typeof(IRequest)))
                        {
                            return(new ServiceEntry <TService, TFunc>(
                                       (TFunc)(object)(Func <Container, TService>)(c => Adapter.TryResolve <TService>()))
                            {
                                Owner = DefaultOwner,
                                Container = this,
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw CreateAdapterException <TService>(ex);
                }
            }

            return((ServiceEntry <TService, TFunc>)entry);
        }