Example #1
0
        IReadOnlyList <ServiceCacheKey> GetCandidateCacheKeys(ServiceRegistrationKey registrationKey)
        {
            var cacheKeys = ServiceCacheKey.CreateFromRegistrationKey(registrationKey);
            var output    = new List <ServiceCacheKey>();

            lock (syncRoot)
            {
                foreach (var cacheKey in cacheKeys)
                {
                    if (instances.ContainsKey(cacheKey))
                    {
                        output.Add(cacheKey);
                    }

                    if (registrationKey.Name == null)
                    {
                        var otherMatchingKeys = instances.Keys
                                                .Where(x => cacheKey.ImplementationType.GetTypeInfo().IsAssignableFrom(x.ImplementationType.GetTypeInfo()));
                        output.AddRange(otherMatchingKeys);
                    }
                }

                return(output.OrderByDescending(x => x, specificityComparer).ToArray());
            }
        }
Example #2
0
 /// <summary>
 /// Gets a value indicating whether or not the cache contains a component which matches the given key.
 /// </summary>
 /// <returns>
 /// <c>true</c> if the cache contains a matching component; <c>false</c> otherwise.</returns>
 /// <param name="key">Key.</param>
 public bool Has(ServiceRegistrationKey key)
 {
     if (key == null)
     {
         throw new ArgumentNullException(nameof(key));
     }
     return(GetCandidateCacheKeys(key).Any());
 }
Example #3
0
        public void Contains_returns_false_when_a_registration_has_not_been_added(Registry sut,
                                                                                  [Registration] IServiceRegistration registration)
        {
            // Act
            var result = sut.Contains(ServiceRegistrationKey.ForRegistration(registration));

            // Assert
            Assert.That(result, Is.False);
        }
Example #4
0
        public void Get_returns_null_for_a_registration_which_has_not_been_added(Registry sut,
                                                                                 [Registration] IServiceRegistration registration)

        {
            // Act
            var result = sut.Get(ServiceRegistrationKey.ForRegistration(registration));

            // Assert
            Assert.That(result, Is.Null);
        }
Example #5
0
        void DoNotPermitReRegisteringAServiceWhichIsAlreadyCached(IServiceRegistration registration)
        {
            var key = ServiceRegistrationKey.ForRegistration(registration);

            if (cache.Has(key))
            {
                var message = String.Format(Resources.ExceptionFormats.CannotReRegisterAfterResolution, registration);
                throw new ServiceReRegisteredAfterResolutionException(message);
            }
        }
Example #6
0
        /// <summary>
        /// Attempts to get a service/component instance from the cache, matching a given registration.
        /// </summary>
        /// <returns>
        /// <c>true</c>, if a component was found and retrieved, <c>false</c> otherwise.</returns>
        /// <param name="registration">The registration for which to get a component.</param>
        /// <param name="instance">Exposes the component instance found (only if this method returns <c>true</c>).</param>
        public bool TryGet(IServiceRegistration registration, out object instance)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }
            var key = ServiceRegistrationKey.ForRegistration(registration);

            return(TryGet(key, out instance));
        }
Example #7
0
        /// <summary>
        /// Gets a value indicating whether or not the cache contains a component which matches the given registration.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the cache contains a matching component; <c>false</c> otherwise.</returns>
        /// <param name="registration">Registration.</param>
        public bool Has(IServiceRegistration registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }
            var key = ServiceRegistrationKey.ForRegistration(registration);

            return(Has(key));
        }
Example #8
0
        /// <summary>
        /// Attempts to get a service/component instance from the cache, matching a given type and name.
        /// </summary>
        /// <returns>
        /// <c>true</c>, if a component was found and retrieved, <c>false</c> otherwise.</returns>
        /// <param name="serviceType">The service/component type.</param>
        /// <param name="name">The registration name.</param>
        /// <param name="instance">Exposes the component instance found (only if this method returns <c>true</c>).</param>
        public bool TryGet(Type serviceType, string name, out object instance)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }

            var key = new ServiceRegistrationKey(serviceType, name);

            return(TryGet(key, out instance));
        }
Example #9
0
        public void Get_returns_a_registration_which_has_been_added(Registry sut,
                                                                    [Registration] IServiceRegistration registration)
        {
            // Arrange
            sut.Add(registration);

            // Act
            var result = sut.Get(ServiceRegistrationKey.ForRegistration(registration));

            // Assert
            Assert.That(result, Is.SameAs(registration));
        }
Example #10
0
        public void Contains_returns_true_when_a_registration_has_been_added(Registry sut,
                                                                             [Registration] IServiceRegistration registration)
        {
            // Arrange
            sut.Add(registration);

            // Act
            var result = sut.Contains(ServiceRegistrationKey.ForRegistration(registration));

            // Assert
            Assert.That(result, Is.True);
        }
Example #11
0
        bool TryGet(ServiceRegistrationKey key, out object instance)
        {
            var cacheKey = GetCandidateCacheKeys(key).FirstOrDefault();

            if (cacheKey == null)
            {
                instance = null;
                return(false);
            }

            return(instances.TryGetValue(cacheKey, out instance));
        }
Example #12
0
        /// <summary>
        /// Adds a component to the cache.
        /// </summary>
        /// <param name="registration">Registration.</param>
        /// <param name="instance">Instance.</param>
        public void Add(IServiceRegistration registration, object instance)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }
            var key = ServiceRegistrationKey.ForRegistration(registration);

            var cacheKeys = ServiceCacheKey.CreateFromRegistrationKeyAndInstance(key, instance);

            foreach (var cacheKey in cacheKeys)
            {
                instances.TryAdd(cacheKey, instance);
            }
        }
Example #13
0
        /// <summary>
        /// Creates a collection of cache keys from a service registration key.
        /// </summary>
        /// <returns>The created keys.</returns>
        /// <param name="key">Key.</param>
        public static IReadOnlyCollection <ServiceCacheKey> CreateFromRegistrationKey(ServiceRegistrationKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            return(new[] { new ServiceCacheKey(key.ServiceType, key.Name) });
        }
Example #14
0
        /// <summary>
        /// Creates a collection of cache keys from a service registration key and component instance.
        /// </summary>
        /// <returns>The created keys.</returns>
        /// <param name="key">Key.</param>
        /// <param name="instance">Instance.</param>
        public static IReadOnlyCollection <ServiceCacheKey> CreateFromRegistrationKeyAndInstance(ServiceRegistrationKey key, object instance)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var fromRegistrationOnly = CreateFromRegistrationKey(key);

            if (ReferenceEquals(instance, null))
            {
                return(fromRegistrationOnly);
            }

            return(fromRegistrationOnly
                   .Union(new [] { new ServiceCacheKey(instance.GetType(), key.Name) })
                   .ToArray());
        }
Example #15
0
        public void HasRegistration_returns_false_if_no_contained_provider_has_the_key(IServiceRegistrationProvider[] providers,
                                                                                       ServiceRegistrationKey key)
        {
            // Arrange
            Mock.Get(providers[0]).Setup(x => x.HasRegistration(key)).Returns(false);
            Mock.Get(providers[1]).Setup(x => x.HasRegistration(key)).Returns(false);
            Mock.Get(providers[2]).Setup(x => x.HasRegistration(key)).Returns(false);
            var sut = new StackOfRegistriesRegistrationProvider(providers);

            // Act
            var result = sut.HasRegistration(key);

            // Assert
            Assert.That(result, Is.False);
        }