Exemple #1
0
        public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
        {
            if (CommentText.IsNotEmpty())
            {
                writer.WriteComment(CommentText);
            }

            var invokeMethod = InvocationCode(method);

            if (shouldWriteInUsingBlock(method))
            {
                writer.UsingBlock($"{returnActionCode(method)}{invokeMethod}", w => Next?.GenerateCode(method, writer));
            }
            else
            {
                writer.Write($"{returnActionCode(method)}{invokeMethod};");

                // This is just to make the generated code a little
                // easier to read
                if (CommentText.IsNotEmpty())
                {
                    writer.BlankLine();
                }

                Next?.GenerateCode(method, writer);
            }
        }
Exemple #2
0
        public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
        {
            var arguments = _arguments.Select(x => x.Usage).Join(", ");

            if (ReturnCreated)
            {
                writer.Write($"return {_func.Usage}({arguments});");
                Next?.GenerateCode(method, writer);
                return;
            }

            var declaration = $"var {Variable.Usage} = {_func.Usage}({arguments})";

            switch (Disposal)
            {
            case DisposeTracking.None:
                writer.Write(declaration + ";");
                Next?.GenerateCode(method, writer);
                break;

            case DisposeTracking.WithUsing:
                writer.UsingBlock(declaration, w => Next?.GenerateCode(method, w));

                break;

            case DisposeTracking.RegisterWithScope:
                writer.Write(declaration + ";");
                writer.Write($"{_scope.Usage}.{nameof(Scope.TryAddDisposable)}({Variable.Usage});");
                Next?.GenerateCode(method, writer);
                break;
            }
        }
Exemple #3
0
        public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
        {
            var arguments = _arguments.Select(x => x.Usage).Join(", ");
            var implementationTypeName = ImplementationType.FullName.Replace("+", ".");

            var declaration = $"var {Variable.Usage} = new {implementationTypeName}({arguments})";

            if (IsDisposable)
            {
                if (Next is ConstructorFrame && Next.As <ConstructorFrame>().IsDisposable)
                {
                    writer.Write($"using ({declaration})");
                    Next?.GenerateCode(method, writer);
                }
                else
                {
                    writer.UsingBlock(declaration, w => Next?.GenerateCode(method, w));
                }
            }
            else
            {
                writer.Write(declaration + ";");
                Next?.GenerateCode(method, writer);
            }
        }
Exemple #4
0
        public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
        {
            var invokeMethod = invocationCode();

            var returnValue = "";

            if (IsAsync)
            {
                returnValue = method.AsyncMode == AsyncMode.ReturnFromLastNode ? "return " : "await ";
            }

            var isDisposable = false;

            if (shouldAssignVariableToReturnValue(method))
            {
                returnValue  = ReturnVariable.VariableType.IsValueTuple() ? $"{ReturnVariable.Usage} = {returnValue}" : $"var {ReturnVariable.Usage} = {returnValue}";
                isDisposable = ReturnVariable.VariableType.CanBeCastTo <IDisposable>();
            }

            if (isDisposable && DisposalMode == DisposalMode.UsingBlock)
            {
                writer.UsingBlock($"{returnValue}{invokeMethod}", w => Next?.GenerateCode(method, writer));
            }
            else
            {
                writer.Write($"{returnValue}{invokeMethod};");

                Next?.GenerateCode(method, writer);
            }
        }
Exemple #5
0
 public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
 {
     writer.UsingBlock($"var {_scope.Usage} = {_factory.Usage}.{nameof(IServiceScopeFactory.CreateScope)}()", w =>
     {
         w.Write($"var {Provider.Usage} = {_scope.Usage}.{nameof(IServiceScope.ServiceProvider)};");
         Next?.GenerateCode(method, w);
     });
 }
Exemple #6
0
        public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
        {
            if (CommentText.IsNotEmpty())
            {
                writer.WriteComment(CommentText);
            }

            var invokeMethod = invocationCode();

            var returnValue = "";

            if (IsAsync)
            {
                #if NET461 || NET48
                if (method.AsyncMode == AsyncMode.AsyncTask)
                {
                    invokeMethod = invokeMethod + ".ConfigureAwait(false)";
                }
#endif
                returnValue = method.AsyncMode == AsyncMode.ReturnFromLastNode ? "return " : "await ";
            }

            var isDisposable = false;
            if (shouldAssignVariableToReturnValue(method))
            {
#if !NET4x
                returnValue = ReturnVariable.VariableType.IsValueTuple() ? $"{ReturnVariable.Usage} = {returnValue}" : $"{ReturnVariable.AssignmentUsage} = {returnValue}";
                #else
                returnValue = $"{ReturnVariable.AssignmentUsage} = {returnValue}";
#endif


                isDisposable = ReturnVariable.VariableType.CanBeCastTo <IDisposable>();
            }

            if (isDisposable && DisposalMode == DisposalMode.UsingBlock)
            {
                writer.UsingBlock($"{returnValue}{invokeMethod}", w => Next?.GenerateCode(method, writer));
            }
            else
            {
                writer.Write($"{returnValue}{invokeMethod};");

                // This is just to make the generated code a little
                // easier to read
                if (CommentText.IsNotEmpty())
                {
                    writer.BlankLine();
                }

                Next?.GenerateCode(method, writer);
            }
        }
Exemple #7
0
        public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
        {
            var creation = $"var {_output.Usage} = new {_output.VariableType.FullName.Replace("+", ".")}()";

            if (_output.VariableType.CanBeCastTo <IDisposable>())
            {
                writer.UsingBlock(creation, w => Next?.GenerateCode(method, w));
            }
            else
            {
                writer.WriteLine(creation + ";");
                Next?.GenerateCode(method, writer);
            }
        }
        public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
        {
            var typeFullName = Service.VariableType.FullName.Replace("+", ".");
            var declaration  = $"var {Service.Usage} = ({typeFullName}){_provider.Usage}.{nameof(IServiceProvider.GetService)}(typeof({typeFullName}))";

            if (Service.VariableType.CanBeCastTo <IDisposable>())
            {
                writer.UsingBlock(declaration, w => Next?.GenerateCode(method, w));
            }
            else
            {
                writer.Write(declaration + ";");
                Next?.GenerateCode(method, writer);
            }
        }
Exemple #9
0
        public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
        {
            var arguments = _arguments.Select(x => x.Usage).Join(", ");
            var implementationTypeName = _implementationType.FullNameInCode();

            if (ReturnCreated)
            {
                writer.Write($"return new {implementationTypeName}({arguments});");
                Next?.GenerateCode(method, writer);
                return;
            }

            var declaration = $"var {Variable.Usage} = new {implementationTypeName}({arguments})";

            switch (Disposal)
            {
            case DisposeTracking.None:
                writer.Write(declaration + ";");
                Next?.GenerateCode(method, writer);
                break;

            case DisposeTracking.WithUsing:
                if (Next is ConstructorFrame && Next.As <ConstructorFrame>().Disposal == DisposeTracking.WithUsing)
                {
                    writer.Write($"using ({declaration})");
                    Next?.GenerateCode(method, writer);
                }
                else
                {
                    writer.UsingBlock(declaration, w => Next?.GenerateCode(method, w));
                }

                break;

            case DisposeTracking.RegisterWithScope:
                writer.Write(declaration + ";");
                writer.Write($"{_scope.Usage}.{nameof(Scope.TryAddDisposable)}({Variable.Usage});");
                Next?.GenerateCode(method, writer);
                break;
            }
        }
Exemple #10
0
        public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
        {
            switch (Mode)
            {
            case ConstructorCallMode.Variable:
                writer.Write(Declaration() + ";");
                ActivatorFrames.Write(method, writer);

                Next?.GenerateCode(method, writer);
                break;

            case ConstructorCallMode.ReturnValue:
                if (ActivatorFrames.Any())
                {
                    writer.Write(Declaration() + ";");
                    ActivatorFrames.Write(method, writer);

                    writer.Write($"return {Variable.Usage};");
                    Next?.GenerateCode(method, writer);
                }
                else
                {
                    writer.Write($"return {Invocation()};");
                    Next?.GenerateCode(method, writer);
                }


                break;

            case ConstructorCallMode.UsingNestedVariable:
                writer.UsingBlock(Declaration(), w =>
                {
                    ActivatorFrames.Write(method, writer);
                    Next?.GenerateCode(method, w);
                });
                break;
            }
        }
 public override void GenerateCode(IGenerationModel generationModel, ISourceWriter writer)
 {
     writer.UsingBlock("var nested = _root.GetNestedContainer()", w => Next?.GenerateCode(generationModel, writer));
 }