Example #1
0
        public string CreateInterfaceCode(CodeNames codeNames)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(
                $@"    public interface {GetInterfaceNameAndTemplateParamaters(codeNames)}
    {{
");

            if (codeNames.IsAsync)
            {
                sb.AppendLine(
                    $@"        Task{(codeNames.HasReturnType ? $"<{CodeNames.TResultName}>" : "")} RunAsync({codeNames.GetFunctionDeclaration()} func);");
            }
            else
            {
                sb.AppendLine(
                    $@"        {(codeNames.HasReturnType ? $"{CodeNames.TResultName}" : "void")} Run({codeNames.GetFunctionDeclaration()} func);");
            }

            sb.AppendLine(
                $@"    }}");

            return(sb.ToString());
        }
Example #2
0
        public string CreateClassCode(CodeNames codeNames)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(
                $@"    internal class {GetClassNameAndTemplateParamaters(codeNames)} : {codeNames.BaseClassName}, {GetInterfaceNameAndTemplateParamaters(codeNames)}
    {{
        internal {GetClassName(codeNames)}({codeNames.BaseClassName} settings)
            : base(settings)
        {{
        }}

");
            string retValueDefinition = string.Empty;
            string retValueGrab       = string.Empty;

            if (codeNames.HasReturnType)
            {
                retValueDefinition =
                    @"
            TResult retValue = default!;
";
                retValueGrab = "retValue = ";
            }

            if (codeNames.IsAsync)
            {
                sb.AppendLine(
                    $@"        public async Task{(codeNames.HasReturnType ? $"<{CodeNames.TResultName}>" : "")} {GetRunName(true)}({codeNames.GetFunctionDeclaration()} func)
        {{{retValueDefinition}
            await Policy.ExecuteAsync(async () =>
            {{");
            }
            else
            {
                sb.AppendLine(
                    $@"        public {(codeNames.HasReturnType ? $"{CodeNames.TResultName}" : "void")} {GetRunName(false)}({codeNames.GetFunctionDeclaration()} func)
        {{{retValueDefinition}
            Policy.Execute(() =>
            {{");
            }

            if (codeNames.HasServices)
            {
                sb.AppendLine(
                    $@"                using (var scope = ServiceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
                {{");

                for (int i = 0; i < codeNames.ServiceCount; i++)
                {
                    sb.AppendLine(
                        $@"                    var {codeNames.ServicesNames[i]} = scope.ServiceProvider.GetService<{codeNames.ServicesTypes[i]}>()!;");
                }

                sb.AppendLine();
                sb.Append("    ");
            }

            if (codeNames.IsAsync)
            {
                sb.AppendLine(
                    $@"                {retValueGrab}await func.Invoke({string.Join(", ", codeNames.ServicesNames)}).ConfigureAwait(false);");
            }
            else
            {
                sb.AppendLine(
                    $@"                {retValueGrab} func.Invoke({string.Join(", ", codeNames.ServicesNames)});");
            }

            if (codeNames.HasServices)
            {
                sb.AppendLine(
                    $@"                }}");
            }

            sb.AppendLine(
                $@"            }});");

            if (codeNames.HasReturnType)
            {
                sb.AppendLine(
                    $@"
            return retValue;");
            }

            sb.AppendLine(
                $@"        }}
    }}");

            return(sb.ToString());
        }
Example #3
0
        public string CreateClassCode(CodeNames codeNames)
        {
            PolicyScopeRunnerGenerator policyScopeRunnerGenerator =
                new PolicyScopeRunnerGenerator();

            StringBuilder sb = new StringBuilder();

            sb.Append(
                $@"    internal class {GetClassNameAndTemplateParamaters(codeNames)} : {policyScopeRunnerGenerator.GetInterfaceNameAndTemplateParamaters(codeNames)}
    {{
        internal {GetClassName(codeNames)}(PolicyScopeMockConfiguration policyScopeMockConfiguration)
        {{
            PolicyScopeMockConfiguration = policyScopeMockConfiguration;
        }}

        internal PolicyScopeMockConfiguration PolicyScopeMockConfiguration {{ get; }}

");
            string retValueGrab = string.Empty;

            if (codeNames.HasReturnType)
            {
                retValueGrab = CodeNames.TResultName + " retValue = ";
            }

            if (codeNames.IsAsync)
            {
                sb.AppendLine(
                    $@"        public async Task{(codeNames.HasReturnType ? $"<{CodeNames.TResultName}>" : "")} {PolicyScopeRunnerGenerator.GetRunName(true)}({codeNames.GetFunctionDeclaration()} func)
        {{");
            }
            else
            {
                sb.AppendLine(
                    $@"        public {(codeNames.HasReturnType ? $"{CodeNames.TResultName}" : "void")} {PolicyScopeRunnerGenerator.GetRunName(false)}({codeNames.GetFunctionDeclaration()} func)
        {{");
            }

            if (codeNames.HasServices)
            {
                for (int i = 0; i < codeNames.ServiceCount; i++)
                {
                    sb.AppendLine(
                        $@"            var {codeNames.ServicesNames[i]} = ({codeNames.ServicesTypes[i]}) PolicyScopeMockConfiguration.ServiceInstances[{i}];");
                }

                sb.AppendLine();
            }

            if (codeNames.IsAsync)
            {
                sb.AppendLine(
                    $@"            {retValueGrab}await func.Invoke({string.Join(", ", codeNames.ServicesNames)}).ConfigureAwait(false);");
            }
            else
            {
                sb.AppendLine(
                    $@"            {retValueGrab} func.Invoke({string.Join(", ", codeNames.ServicesNames)});");
            }

            sb.AppendLine(
                $@"
            PolicyScopeMockConfiguration.ExecutionCount++;");

            if (codeNames.HasReturnType)
            {
                sb.AppendLine(
                    $@"
            return retValue;");
            }

            sb.AppendLine(
                $@"        }}
    }}");

            return(sb.ToString());
        }