private CSharpClassMethod CreateResolvers(ContainerRegistrationDirect registration)
        {
            var targetType = registration.Implementation;
            var ctor       = targetType.GetConstructors()
                             .OrderBy(c => c.GetParameters().Count())
                             .First();

            var statements = new List <CSharpStatement>();

            foreach (var parameter in ctor.GetParameters())
            {
                var paramType          = parameter.ParameterType;
                var resolveInvoke      = new CSharpInvoke(paramType.GetResolveNameBind(), new CSharpStatement[0]);
                var variableDependency = new CSharpInitVariable(null, paramType.GetDependencyName(), resolveInvoke);
                statements.Add(variableDependency);
            }

            var @params = ctor.GetParameters()
                          .Select(t => t.ParameterType)
                          .Select(t => t.GetDependencyName())
                          .Select(p => new CSharpSymbol(p));

            var resultVariable = new CSharpInitVariable(null, "result", new CSharpNewObject(targetType, @params));

            statements.Add(resultVariable);
            statements.Add(new CSharpReturn(new CSharpSymbol("result")));

            var resolveName   = targetType.GetResolveName();
            var resolveMethod = new CSharpClassMethod(targetType, resolveName,
                                                      new MethodParameter[0], false, statements);

            return(resolveMethod);
        }
Пример #2
0
        private CSharpClassMethod CreateTransients(ContainerRegistrationMethod registration)
        {
            var resultStatement = new CSharpInitVariable(null, "result", new CSharpInvoke(registration.FactoryMethod, new CSharpStatement[0]));
            var resolveBody     = new CSharpStatement[] {
                resultStatement,
                new CSharpReturn(new CSharpSymbol("result"))
            };
            var resolveName   = registration.Binding.GetResolveNameBind();
            var resolveMethod = new CSharpClassMethod(registration.Binding, resolveName,
                                                      new MethodParameter[0], false, resolveBody);

            return(resolveMethod);
        }
Пример #3
0
        private IEnumerable <CSharpClassMember> CreateMainResolver(ContainerFactory builderInstance)
        {
            var members = new List <CSharpClassMember>();

            var tableType     = typeof(IDictionary <Type, Func <object> >);
            var tableTypeImpl = typeof(Dictionary <Type, Func <object> >);
            var field         = new CSharpField(tableType, "_ResolveTable", false);

            members.Add(field);

            var initStatements = new List <CSharpStatement>();
            var initField      = new CSharpBinaryStatement(new CSharpSymbol("_ResolveTable"),
                                                           new CSharpNewObject(tableTypeImpl, new CSharpStatement[0]), "=", true);

            initStatements.Add(initField);
            foreach (var registration in builderInstance.Registrations)
            {
                var leftInit    = new CSharpSymbol($"_ResolveTable[typeof({registration.Binding.GetRightFullName()})]");
                var rightInit   = new CSharpSymbol($"(Func<object>){registration.Binding.GetResolveNameBind()}");
                var resolverRow = new CSharpBinaryStatement(leftInit, rightInit, "=", true);
                initStatements.Add(resolverRow);
            }

            var ifStatement =
                new CSharpIf(new CSharpBinaryStatement(new CSharpSymbol("_ResolveTable"), CSharpSymbol.NULL, "=="),
                             initStatements);

            var throwStatement = new CSharpThrowStatement(new CSharpNewObject(typeof(StatorUnresolvedException),
                                                                              new[] { new CSharpSymbol("target") }));
            var resultVariable =
                new CSharpInitVariable(null, "result", new CSharpInvoke("resolver", new CSharpStatement[0]));
            var checkStatement =
                new CSharpIf(
                    new CSharpInvoke("_ResolveTable.TryGetValue",
                                     new CSharpStatement[] { new CSharpSymbol("target"), new CSharpSymbol("out var resolver") }),
                    new CSharpStatement[] { resultVariable, new CSharpReturn(new CSharpSymbol("result")) });

            var statements = new List <CSharpStatement>();

            statements.Add(ifStatement);
            statements.Add(checkStatement);
            statements.Add(throwStatement);

            var resolveMethod = new CSharpClassMethod(typeof(object), nameof(ContainerFactory.Resolve),
                                                      new MethodParameter[] { new MethodParameter(typeof(Type), "target") },
                                                      true, statements, new[] { "override" });

            members.Add(resolveMethod);

            return(members);
        }