IDictionary <ServiceRegistrationKey, IServiceRegistration> GetNonConflictingRegistrations(IEnumerable <ServiceRegistrationKey> alreadyFound,
                                                                                                  IServiceRegistrationProvider provider,
                                                                                                  Type serviceTypeFilter)
        {
            if (alreadyFound == null)
            {
                throw new ArgumentNullException(nameof(alreadyFound));
            }
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            var candidates = (serviceTypeFilter != null)? provider.GetAll(serviceTypeFilter) : provider.GetAll();

            if (candidates == null)
            {
                candidates = new IServiceRegistration[0];
            }

            return((from registration in candidates
                    let key = ServiceRegistrationKey.ForRegistration(registration)
                              where !alreadyFound.Contains(key)
                              select new { Registration = registration, Key = key })
                   .ToDictionary(k => k.Key, v => v.Registration));
        }
Esempio n. 2
0
 bool IServiceRegistrationProvider.HasRegistration(ServiceRegistrationKey key)
 {
     if (key == null)
     {
         throw new ArgumentNullException(nameof(key));
     }
     return(Contains(key));
 }
 /// <summary>
 /// Gets a value which indicates whether or not the current provider has a matching registrations.
 /// </summary>
 /// <returns>
 /// <c>true</c>, if a matching registration is available, <c>false</c> otherwise.</returns>
 /// <param name="key">A registration key.</param>
 public bool HasRegistration(ServiceRegistrationKey key)
 {
     if (key == null)
     {
         throw new ArgumentNullException(nameof(key));
     }
     return(providers.Any(x => x.HasRegistration(key)));
 }
Esempio n. 4
0
        /// <summary>
        /// Gets a value that indicates whether or not the current registration matches the specified registration key or not.
        /// </summary>
        /// <returns>
        /// <c>true</c>, if the current instance matches the specified key, <c>false</c> otherwise.</returns>
        /// <param name="key">The registration key against which to test.</param>
        public virtual bool MatchesKey(ServiceRegistrationKey key)
        {
            if (key == null)
            {
                return(false);
            }

            return(ServiceType == key.ServiceType && Name == key.Name);
        }
Esempio n. 5
0
        /// <summary>
        /// Gets a registration matching the specified key.
        /// </summary>
        /// <param name="key">Key.</param>
        public IServiceRegistration Get(ServiceRegistrationKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var candidates = GetCandidateRegistrations(key).OrderByDescending(x => x.Priority);

            return(candidates.FirstOrDefault());
        }
Esempio n. 6
0
        /// <summary>
        /// Gets a value which indicates whether or not a matching registration is contained within the current instance.
        /// </summary>
        /// <param name="key">A service registration key.</param>
        public bool Contains(ServiceRegistrationKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var candidates = GetCandidateRegistrations(key);

            return(candidates.Any());
        }
Esempio n. 7
0
        bool IServiceRegistrationProvider.CanFulfilRequest(ResolutionRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var key = ServiceRegistrationKey.FromRequest(request);

            return(Contains(key));
        }
Esempio n. 8
0
        IServiceRegistration IServiceRegistrationProvider.Get(ResolutionRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var key = ServiceRegistrationKey.FromRequest(request);

            return(Get(key));
        }
Esempio n. 9
0
        /// <summary>
        /// Gets a value that indicates whether or not the current registration matches the specified registration key or not.
        /// </summary>
        /// <returns>
        /// <c>true</c>, if the current instance matches the specified key, <c>false</c> otherwise.</returns>
        /// <param name="key">The registration key against which to test.</param>
        public override bool MatchesKey(ServiceRegistrationKey key)
        {
            if (base.MatchesKey(key))
            {
                return(true);
            }

            if (key == null)
            {
                return(false);
            }

            return(key.ServiceType.GetTypeInfo().IsAssignableFrom(ImplementationType.GetTypeInfo()) && Name == key.Name);
        }
        /// <summary>
        /// Gets a value that indicates whether or not the current registration matches the specified registration key or not.
        /// </summary>
        /// <returns>
        /// <c>true</c>, if the current instance matches the specified key, <c>false</c> otherwise.</returns>
        /// <param name="key">The registration key against which to test.</param>
        public override bool MatchesKey(ServiceRegistrationKey key)
        {
            if (key == null)
            {
                return(false);
            }

            if (!key.ServiceType.GetTypeInfo().IsGenericType)
            {
                return(false);
            }

            var openGenericKeyType = key.ServiceType.GetGenericTypeDefinition();

            return(openGenericKeyType == ServiceType);
        }
Esempio n. 11
0
        /// <summary>
        /// Add the specified component registration.
        /// </summary>
        /// <param name="registration">Registration.</param>
        public void Add(IServiceRegistration registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }

            registration.AssertIsValid();

            var key = ServiceRegistrationKey.ForRegistration(registration);

            lock (syncRoot)
            {
                IServiceRegistration removed;
                registrations.TryRemove(key, out removed);
                registrations.TryAdd(key, registration);
            }
        }
Esempio n. 12
0
        IReadOnlyList <IServiceRegistration> GetCandidateRegistrations(ServiceRegistrationKey key)
        {
            var output = new List <IServiceRegistration>();

            lock (syncRoot)
            {
                IServiceRegistration exactMatch;
                if (registrations.TryGetValue(key, out exactMatch))
                {
                    output.Add(exactMatch);
                }

                var otherMatches = (from registration in registrations.Values
                                    where
                                    registration.MatchesKey(key) &&
                                    (exactMatch == null || !ReferenceEquals(registration, exactMatch))
                                    select registration)
                                   .ToArray();
                output.AddRange(otherMatches);
            }

            return(output.ToArray());
        }