public static IDependencyFactoryInfo As(this IDependencyFactoryInfo typeInfo, Type concreteRequestType)
        {
            if (!IsFactoryGeneric(typeInfo))
            {
                return(new DependencyFactoryInfo(concreteRequestType, typeInfo.MethodName, typeInfo));
            }

            var resovleType = typeInfo.FactoryType;

            return(As(typeInfo, concreteRequestType.GenericTypeArguments, resovleType, rt => new DependencyFactoryInfo(rt, typeInfo.MethodName, typeInfo)));
        }
        /// <summary>
        /// Convert a generic factory info into a concrete type info.
        /// </summary>
        /// <param name="typeInfo">The current dependency info</param>
        /// <param name="genericTypeArguments">The generic type arguments.</param>
        /// <returns>A new instance of the dependency info with the concrete information set.</returns>
        public static IDependencyFactoryInfo As(this IDependencyFactoryInfo typeInfo, params Type[] genericTypeArguments)
        {
            if (!IsFactoryGeneric(typeInfo))
            {
                return(typeInfo);
            }

            var resovleType = typeInfo.FactoryType;

            return(As(typeInfo, genericTypeArguments, resovleType, rt => new DependencyFactoryInfo(rt, typeInfo.MethodName, typeInfo)));
        }
 private object ExecuteFactory(IDependencyFactoryInfo info, IList <Parameter> parameters)
 {
     if (parameters == null || parameters.Count == 0)
     {
         return(_dependencyFactory.Resolve(info.CreateContext(Ioc.Container)));
     }
     if (TryGetNamedParameters(parameters, out (string, object)[] namedParameters))
     {
         return(_dependencyFactory.Resolve(info.CreateContext(Ioc.Container).SetArguments(namedParameters)));
     }
     if (TryGetPositionalParameters(parameters, out object[] positionalParameters))
        private object ExecuteFactory(IDependencyFactoryInfo info, IContext ctx)
        {
            var executionInfo = info;

            if (info.IsFactoryGeneric())
            {
                executionInfo = info.As(ctx.GenericArguments);
            }

            var args = ctx.Parameters.Count == 0 ? null : ctx.Parameters.Select(s => ValueTuple.Create(s.Name, s.GetValue(ctx, ctx.Request.Target))).ToArray();

            return(_dependencyFactory.Resolve(executionInfo.CreateContext(Ioc.Container).SetArguments(args)));
        }
Exemple #5
0
        protected override void OnRegisterFactorySingleton(IDependencyFactoryInfo info, IDependencyFactory factory)
        {
            if (info.IsFactoryGeneric())
            {
            }
            else
            {
                foreach (var resolveType in info.ResolvedTypes)
                {
                    _services.AddSingleton(resolveType, p => factory.Resolve(info.CreateContext(Ioc.Container)));
                }
            }

            base.OnRegisterFactorySingleton(info, factory);
        }
        private bool TryRegisterFactory(IDependencyFactoryInfo info)
        {
            if (info == null)
            {
                return(false);
            }

            switch (info.Scope)
            {
            case DependyBuilderScope.Transient:
                OnRegisterFactory(info, _dependencyFactory);
                break;

            case DependyBuilderScope.Singleton:
                OnRegisterFactorySingleton(info, _dependencyFactory);
                break;

            default:
                throw new NotSupportedException($"{info.Scope} scope not currently supported for {info}.");
            }

            return(true);
        }
 /// <summary>
 /// Get an indicator to see if the factory is a generic type.
 /// </summary>
 /// <param name="typeInfo"></param>
 /// <returns></returns>
 public static bool IsFactoryGeneric(this IDependencyFactoryInfo typeInfo)
 {
     return(typeInfo.FactoryType.IsGenericType);
 }
 /// <summary>
 /// Register a singleton <see cref="IDependencyTypeInfo"/> instance with the configuration.
 /// Can be overridden.
 /// </summary>
 /// <param name="info">The dependency information describing the resolving requirements.</param>
 /// <param name="factory">The dependency factory that will resolve the factory.</param>
 protected virtual void OnRegisterFactorySingleton(IDependencyFactoryInfo info, IDependencyFactory factory)
 {
     _configuration.Add(info.ResolvedTypes, new DependencySingletionResolution <IDependencyFactoryInfo, DependencyFactoryResolution>(new DependencyFactoryResolution(info, factory)));
 }