Ejemplo n.º 1
0
        private ServiceEntry <TService, TFunc> GetEntry <TService, TFunc>(string serviceName, bool throwIfMissing)
        {
            var          key       = new ServiceKey(typeof(TFunc), serviceName);
            ServiceEntry entry     = null;
            Container    container = this;

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

            if (entry != null)
            {
                if (entry.Reuse == ReuseScope.Container && entry.Container != this)
                {
                    entry         = ((ServiceEntry <TService, TFunc>)entry).CloneFor(this);
                    services[key] = entry;
                }
            }
            else if (throwIfMissing)
            {
                ThrowMissing <TService>(serviceName);
            }

            return((ServiceEntry <TService, TFunc>)entry);
        }
Ejemplo n.º 2
0
		public static bool Equals(ServiceKey obj1, ServiceKey obj2)
		{
			if (Object.Equals(null, obj1) ||
				Object.Equals(null, obj2))
				return false;

			return obj1.FactoryType == obj2.FactoryType && 
				obj1.Name == obj2.Name;
		}
Ejemplo n.º 3
0
        public static bool Equals(ServiceKey obj1, ServiceKey obj2)
        {
            if (obj2 == null || obj1.GetType() != obj2.GetType())
            {
                return false;
            }

            return obj1.ServiceType == obj2.ServiceType && obj1.FactoryType == obj2.FactoryType;
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        public static bool Equals(ServiceKey obj1, ServiceKey obj2)
        {
            if (Object.Equals(null, obj1) ||
                Object.Equals(null, obj2))
            {
                return(false);
            }

            return(obj1.FactoryType == obj2.FactoryType &&
                   obj1.Name == obj2.Name);
        }
Ejemplo n.º 6
0
        private ServiceEntry <TService, TFunc> GetEntry <TService, TFunc>(string serviceName, bool throwIfMissing)
        {
            var          key       = new ServiceKey(typeof(TFunc), serviceName);
            ServiceEntry entry     = null;
            Container    container = this;

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

            if (entry != null)
            {
                if (entry.Reuse == ReuseScope.Container && entry.Container != this)
                {
                    entry         = ((ServiceEntry <TService, TFunc>)entry).CloneFor(this);
                    services[key] = entry;
                }
            }
            else
            {
                //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 = Owner.Container,
                            Container = this,
                        });
                    }
                    ThrowMissing <TService>(serviceName);
                }
                else
                {
                    if (Adapter != null &&
                        (typeof(TService) != typeof(IRequestContext)))
                    {
                        return(new ServiceEntry <TService, TFunc>(
                                   (TFunc)(object)(Func <Container, TService>)(c => Adapter.TryResolve <TService>()))
                        {
                            Owner = Owner.Container,
                            Container = this,
                        });
                    }
                }
            }

            return((ServiceEntry <TService, TFunc>)entry);
        }
Ejemplo n.º 7
0
        private bool TryGetServiceEntry(ServiceKey key, out ServiceEntry entry)
        {
            var snapshot = servicesReadOnlyCopy;

            if (snapshot == null)
            {
                lock (services)
                {
                    snapshot = new Dictionary <ServiceKey, ServiceEntry>(services);
                    Interlocked.Exchange(ref servicesReadOnlyCopy, snapshot);
                }
            }

            return(snapshot.TryGetValue(key, out entry));
        }
Ejemplo n.º 8
0
        private ServiceEntry <TService, TFunc> RegisterImpl <TService, TFunc>(string name, TFunc factory)
        {
            //if (typeof(TService) == typeof(Container))
            //    throw new ArgumentException(ServiceStack.ResourceDesigner.Resources.Registration_CantRegisterContainer);

            var entry = new ServiceEntry <TService, TFunc>(factory)
            {
                Container = this,
                Reuse     = DefaultReuse,
                Owner     = DefaultOwner
            };
            var key = new ServiceKey(typeof(TFunc), name);

            SetServiceEntry(key, entry);

            return(entry);
        }
Ejemplo n.º 9
0
        private ServiceEntry <TService, TFunc> RegisterImpl <TService, TFunc>(string name, TFunc factory)
        {
            if (typeof(TService) == typeof(Container))
            {
                throw new ArgumentException(ServiceStack.Properties.Resources.Registration_CantRegisterContainer);
            }

            var entry = new ServiceEntry <TService, TFunc>(factory)
            {
                Container = this,
                Reuse     = DefaultReuse,
                Owner     = DefaultOwner
            };
            var key = new ServiceKey(typeof(TFunc), name);

            services[key] = entry;

            return(entry);
        }
Ejemplo n.º 10
0
        private ServiceEntry <TService, TFunc> RegisterImpl <TService, TFunc>(string name, TFunc factory)
        {
            if (typeof(TService) == typeof(Container))
            {
                throw new ArgumentException("Container service is built-in and read-only.");
            }

            var entry = new ServiceEntry <TService, TFunc>(factory)
            {
                Container = this,
                Reuse     = DefaultReuse,
                Owner     = DefaultOwner
            };
            var key = new ServiceKey(typeof(TFunc), name);

            SetServiceEntry(key, entry);

            return(entry);
        }
Ejemplo n.º 11
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).GetTypeGenericArguments();
                    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);
        }
Ejemplo n.º 12
0
 public override bool Equals(object obj)
 {
     return(ServiceKey.Equals(this, obj as ServiceKey));
 }
Ejemplo n.º 13
0
 public bool Equals(ServiceKey other)
 {
     return(ServiceKey.Equals(this, other));
 }
Ejemplo n.º 14
0
        private ServiceEntry <TService, TFunc> GetEntry <TService, TFunc>(string serviceName, bool throwIfMissing)
        {
            try
            {
                TService resolved;
                if (CheckAdapterFirst &&
                    Adapter != null &&
                    typeof(TService) != typeof(IRequestContext) &&
                    !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(IRequestContext)))
                        {
                            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);
        }
Ejemplo n.º 15
0
		public bool Equals(ServiceKey other)
		{
			return ServiceKey.Equals(this, other);
		}