Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
0
        private ServiceEntry <TService, TFunc> GetEntry <TService, TFunc>(string serviceName, bool throwIfMissing)
        {
            try
            {
                TService resolved;
                if (CheckAdapterFirst &&
                    Adapter != null &&
                    !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;
            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)
                        {
                            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.º 7
0
 public bool Equals(ServiceKey other)
 {
     return Equals(this, other);
 }
Ejemplo n.º 8
0
 public bool Equals(ServiceKey other)
 {
     return(Equals(this, other));
 }
Ejemplo n.º 9
0
 public override bool Equals(object obj)
 {
     return(ServiceKey.Equals(this, obj as ServiceKey));
 }