示例#1
0
        public void ShouldDistinguishBetweenTwoServicesOfTheSameTypeButDifferentParameters()
        {
            var firstFactory  = new Mock <IFactory>();
            var secondFactory = new Mock <IFactory>();

            var serviceType = typeof(int);

            IEnumerable <Type> firstParameters  = new Type[] { typeof(int), typeof(int) };
            IEnumerable <Type> secondParameters = new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) };

            _storage.AddFactory("", serviceType, firstParameters, firstFactory.Object);
            _storage.AddFactory("", serviceType, secondParameters, secondFactory.Object);

            Assert.IsTrue(_storage.ContainsFactory("", serviceType, firstParameters));
            Assert.IsTrue(_storage.ContainsFactory("", serviceType, secondParameters));

            // Make sure that the factory returns the correct container
            var firstResult = _storage.GetFactory("", serviceType, firstParameters);

            Assert.AreSame(firstFactory.Object, firstResult);

            var secondResult = _storage.GetFactory("", serviceType, secondParameters);

            Assert.AreSame(secondFactory.Object, secondResult);
        }
        /// <summary>
        /// Determines whether or not a factory exists in storage.
        /// </summary>
        /// <param name="storage">The <see cref="IFactoryStorage"/> object that holds the target factory.</param>
        /// <param name="serviceName">The name that will be associated with the target factory.</param>
        /// <param name="serviceType">The service type that the factory will be able to create.</param>
        /// <param name="additionalParameterTypes">The list of additional parameters that this factory type will support.</param>
        /// <returns>Returns <c>true</c> if the factory exists; otherwise, it will return <c>false</c>.</returns>
        public static bool ContainsFactory(this IFactoryStorage storage, string serviceName, Type serviceType,
                                           IEnumerable <Type> additionalParameterTypes)
        {
            var info = new ServiceInfo(serviceName, serviceType, additionalParameterTypes);

            return(storage.ContainsFactory(info));
        }