예제 #1
0
파일: Di.cs 프로젝트: Delt06/di-framework
        /// <summary>
        /// Create an instance and injects its dependencies via a constructor.
        /// </summary>
        /// <typeparam name="T">The type of the instance.</typeparam>
        /// <returns>Created instance.</returns>
        public static T Create <[MeansImplicitUse] T>() where T : class
        {
            var type = typeof(T);

            if (type.IsAbstract)
            {
                throw new ArgumentException($"{type} is abstract and thus cannot be instantiated.");
            }
            return((T)PocoInjection.CreateInstance(type, TryResolveGlobally));
        }
예제 #2
0
        private static string GetBakeCallText(Type type, int functionIndex)
        {
            var methodName   = $"{nameof(BakedInjection)}.{nameof(BakedInjection.Bake)}";
            var fullTypeName = GetFullyQualifiedName(type);
            var typeName     = $"typeof({fullTypeName})";
            var functionName = PocoInjection.IsPoco(type)
                ? GetInstantiationFunctionName(functionIndex)
                : GetInjectionFunctionName(functionIndex);

            return(new StringBuilder()
                   .Append(TripleIndent)
                   .Append(methodName)
                   .Append("(")
                   .Append(typeName)
                   .Append(", ")
                   .Append(functionName)
                   .AppendLine(");")
                   .ToString());
        }
예제 #3
0
        private static bool TryGetInstantiationFunctionDeclaration(Type type, int functionIndex, out string result)
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.Append(DoubleIndent)
            .Append("private static object ")
            .Append(GetInstantiationFunctionName(functionIndex))
            .Append($"({nameof(PocoResolutionFunction)} resolve)")
            .AppendLine();

            stringBuilder.Append(DoubleIndent)
            .AppendLine("{");

            if (PocoInjection.TryGetInjectableConstructorParameters(type, out var parameters))
            {
                stringBuilder.Append(TripleIndent)
                .Append("return new ")
                .Append(GetFullyQualifiedName(type))
                .Append("(");

                for (var parameterIndex = 0; parameterIndex < parameters.Count; parameterIndex++)
                {
                    if (parameterIndex > 0)
                    {
                        stringBuilder.Append(", ");
                    }

                    var parameter = parameters[parameterIndex];
                    AppendPocoResolveExpression(parameter.ParameterType, stringBuilder);
                }

                stringBuilder.AppendLine(");");
            }

            stringBuilder.Append(DoubleIndent)
            .AppendLine("}");

            result = stringBuilder.ToString();
            return(true);
        }
예제 #4
0
        public void BakeData([NotNull] TextWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var index                = 0;
            var bakeCalls            = new StringBuilder();
            var functionDeclarations = new StringBuilder();

            foreach (var type in _bakedTypes)
            {
                var bakeCallText = GetBakeCallText(type, index);

                string functionDeclaration;
                var    success = PocoInjection.IsPoco(type)
                    ? TryGetInstantiationFunctionDeclaration(type, index, out functionDeclaration)
                    : TryGetInjectionFunctionDeclaration(type, index, out functionDeclaration);
                if (!success)
                {
                    continue;
                }

                bakeCalls.Append(bakeCallText);
                functionDeclarations.Append(functionDeclaration);
                functionDeclarations.AppendLine();
                index++;
            }

            FixLineEndings(bakeCalls);
            FixLineEndings(functionDeclarations);

            var dataText = string.Format(ClassTemplate, Namespace, _className, bakeCalls,
                                         functionDeclarations
                                         );

            writer.Write(dataText);
        }
예제 #5
0
 private static bool IsInjectablePoco(Type type) =>
 PocoInjection.IsPoco(type) &&
 PocoInjection.IsInjectable(type) &&
 type.GetConstructors(BindingFlags.Public | BindingFlags.Instance).Length > 0;
예제 #6
0
 private object CreateInstance(Type type) => PocoInjection.CreateInstance(type, _resolutionFunction);