Example #1
0
        private IEnumerable <CSharpClassMember> CreateSingletons(ContainerRegistrationMethod registration)
        {
            var members = new List <CSharpClassMember>();

            var binding   = registration.Binding;
            var fieldName = binding.GetSingletonFieldName();
            var field     = new CSharpField(binding, fieldName, false);

            members.Add(field);

            var condition  = new CSharpBinaryStatement(new CSharpSymbol(fieldName), CSharpSymbol.NULL, "==");
            var statements = new CSharpStatement[] {
                new CSharpBinaryStatement(new CSharpSymbol(fieldName), new CSharpInvoke(registration.FactoryMethod, new CSharpStatement[0]), "=", true),
            };
            var ifStatement = new CSharpIf(condition, statements);
            var resolveBody = new CSharpStatement[] {
                ifStatement,
                new CSharpReturn(new CSharpSymbol(fieldName))
            };
            var resolveName   = binding.GetResolveNameBind();
            var resolveMethod = new CSharpClassMethod(binding, resolveName,
                                                      new MethodParameter[0], false, resolveBody);

            members.Add(resolveMethod);

            return(members);
        }
Example #2
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);
        }