A class that converts a delegate into an IFactory instance.
Inheritance: IFactory
        /// <summary>
        /// Gets the generic factory for a concrete service type.
        /// </summary>
        /// <param name="serviceInfo">The <see cref="IServiceInfo"/> object that describes the service to be created.</param>
        /// <param name="factory">The factory instance that will be used to create the service.</param>
        /// <param name="serviceName">The name of the service.</param>
        /// <param name="actualServiceType">The service type.</param>
        /// <returns>A factory instance that can create the generic type.</returns>
        private IFactory GetGenericFactory(IServiceInfo serviceInfo, IFactory factory, string serviceName,
                                           Type actualServiceType)
        {
            var info = new ServiceInfo(serviceName, actualServiceType, serviceInfo.ArgumentTypes);

            if (base.ContainsFactory(info))
            {
                IFactory actualFactory = base.GetFactory(info);
                Func<IFactoryRequest, object> factoryMethod = request => actualFactory;

                factory = new FunctorFactory<IFactory>(factoryMethod);
            }

            return factory;
        }
Example #2
0
        /// <summary>
        /// Obtains the <see cref="IFactory"/> instance that can instantiate the
        /// service described by the <paramref name="serviceInfo"/> object instance.
        /// </summary>
        /// <param name="serviceInfo">The <see cref="IServiceInfo"/> object that describes the service to be created.</param>
        /// <returns>A <see cref="IFactory"/> instance if the service can be instantiated; otherwise, it will return <c>false</c>.</returns>
        public override IFactory GetFactory(IServiceInfo serviceInfo)
        {
            // Attempt to create the service type using
            // the strongly-typed arguments
            var factory = base.GetFactory(serviceInfo);
            var serviceType = serviceInfo.ServiceType;
            var serviceName = serviceInfo.ServiceName;

            // Use the default factory for this service type if no other factory exists
            var defaultServiceInfo = new ServiceInfo(serviceInfo.ServiceName, serviceInfo.ServiceType);
            if (factory == null && base.ContainsFactory(defaultServiceInfo))
                factory = base.GetFactory(defaultServiceInfo);

            // Attempt to create the service type using
            // the generic factories, if possible
            if (factory != null || !serviceType.IsGenericType)
                return factory;

            var definitionType = serviceType.GetGenericTypeDefinition();
            var genericServiceInfo = new ServiceInfo(serviceName, definitionType, serviceInfo.ArgumentTypes);

            if (base.ContainsFactory(genericServiceInfo))
                return base.GetFactory(genericServiceInfo);

            if (definitionType != typeof(IFactory<>))
                return factory;

            var typeArguments = serviceType.GetGenericArguments();
            var actualServiceType = typeArguments[0];
            var info = new ServiceInfo(serviceName, actualServiceType, serviceInfo.ArgumentTypes);

            if (base.ContainsFactory(info))
            {
                var actualFactory = base.GetFactory(info);
                Func<IFactoryRequest, object> factoryMethod = request => actualFactory;

                factory = new FunctorFactory<IFactory>(factoryMethod);
            }

            return factory;
        }