Exemple #1
0
        protected FactoryBase(IServiceProvider serviceProvider, ServiceMap serviceMap, Type[] argTypes)
        {
            this.serviceProvider = serviceProvider;
            this.argTypes        = argTypes;
            var serviceDescriptor = serviceMap.TryGetValue(typeof(TService), out var value)
                ? value.Single()
                : throw new InvalidOperationException($"Unable to resolve service for type '{typeof(TService)}' while attempting to activate '{this.GetType().GetInterfaces().Single()}'.");

            this.implementationType = serviceDescriptor.ImplementationType;
            if (serviceDescriptor.Lifetime != ServiceLifetime.Transient)
            {
                throw new InvalidOperationException(
                          $"In order to resolve a parameterised factory for service type '{typeof(TService)}', the implementation type '{this.implementationType}' must be registered with Transient lifestyle.");
            }

            var matchingConstructors = this.implementationType.GetConstructors().Where(constructorInfo => IsMatch(constructorInfo, argTypes)).ToList();

            if (matchingConstructors.Count != 1)
            {
                var argTypesText = string.Join(", ", argTypes.Select(argType => $"'{argType}'"));
                throw new InvalidOperationException(
                          $"In order to resolve a parameterised factory for service type '{typeof(TService)}', the implementation type '{this.implementationType}' must contain {(matchingConstructors.Count == 0 ? "a" : "just one")} constructor whose last {(argTypes.Length > 1 ? argTypes.Length + " parameters are assignable from the factory argument types" : "parameter is assignable from the factory argument type")} {argTypesText}.");
            }

            this.constructor = matchingConstructors[0];
        }
Exemple #2
0
 public Factory(IServiceProvider serviceProvider, ServiceMap serviceMap)
     : base(serviceProvider, serviceMap, new[] { typeof(T1), typeof(T2), typeof(T3) })
 {
 }