public void GenerateCode(SourceCodeBuilder sb) { Validate(); if (!string.IsNullOrWhiteSpace(_nameSpace)) { sb.AppendLine($"namespace {_nameSpace}") .OpenBlock(); } if (_usingNs.Any()) { foreach (var ns in _usingNs) { sb.AppendLine($"using {ns};"); } } sb.AppendLine(); sb.AppendLine($"public class {_name} : {GetHandlerInterfaceName()}"); sb.OpenBlock(); GenerateConsts(sb); GenerateCtor(sb); GenerateExecuteMethod(sb); sb.CloseBlock(); if (!string.IsNullOrWhiteSpace(_nameSpace)) { sb.CloseBlock(); } }
public void GenerateCode(SourceCodeBuilder sb) { if (!string.IsNullOrWhiteSpace(_nameSpace)) { sb.AppendLine($"namespace {_nameSpace}") .OpenBlock(); } if (_usingNs.Any()) { foreach (var ns in _usingNs) { sb.AppendLine($"using {ns};"); } } sb.AppendLine(); sb.AppendLine($"public class {TypeName} : ControllerBase"); sb.OpenBlock(); GenerateLocals(sb).AppendLine(); GenerateCtor(sb).AppendLine(); GenerateActions(sb); sb.CloseBlock(); if (!string.IsNullOrWhiteSpace(_nameSpace)) { sb.CloseBlock(); } }
protected virtual void GenerateCtor(SourceCodeBuilder sb) { sb.AppendLine("private IConfiguration _config;"); sb.AppendLine(); sb.AppendLine($"public {_name}(IConfiguration config)"); sb.OpenBlock(); sb.AppendLine($"this._config = config;"); sb.CloseBlock(); }
private SourceCodeBuilder GenerateCtor(SourceCodeBuilder sb) { var args = string.Join(",", _actions.Select(x => $"{x.HandlerInterfaceInfo} {x.Name.StartLower()}Handler")); sb.AppendLine($"public {TypeName}({args}, IMapper mapper)"); sb.OpenBlock(); sb.AppendLine("this._mapper = mapper;"); foreach (var a in _actions) { sb.AppendLine($"this._{a.Name.StartLower()}Handler = {a.Name.StartLower()}Handler;"); } sb.CloseBlock(); return(sb); }
protected void GenerateMethodBody(SourceCodeBuilder sb) { sb.AppendLine($"using(var connection = new SqlConnection(_config.GetConnectionString(_dbName)))"); sb.OpenBlock(); sb.AppendLine("await connection.OpenAsync();"); sb.AppendLine($"using(var command = connection.CreateCommand())").OpenBlock(); sb.AppendLine($"command.CommandType = CommandType.StoredProcedure;"); sb.AppendLine($"command.CommandText = _procName;").AppendLine(); sb.AppendLine($"var parameters = command.Parameters;").AppendLine(); foreach (var p in this.ParameterBindings) { var pname = p.ParameterName.DblQuoted(); if (p.ParameterType.IsNullable || p.ParameterType.FullName == "System.String") { sb.AppendLine($"if({MethodArg()}.{p.Path} != null) parameters.AddWithValue({pname}, {MethodArg()}.{p.Path});"); sb.AppendLine($"else parameters.AddWithValue({pname}, DBNull.Value);"); } else { sb.AppendLine($"parameters.AddWithValue({pname}, {MethodArg()}.{p.Path});"); } } sb.AppendLine(); sb.AppendLine("try").OpenBlock(); WriteMethod_ReaderBlock(sb).CloseBlock(); sb.AppendLine("catch(SqlException ex)").OpenBlock(); sb.AppendLine("if (ex.Number >= 50000 && ex.Number <= 51000)").OpenBlock(); sb.AppendLine("if (ex.State == 255) throw new XmlRequestException((ErrorCodeReason) (ex.Number - 50000), XDocument.Parse(ex.Message), ex);"); sb.AppendLine("throw new RequestException((ErrorCodeReason) (ex.Number - 50000), ex.Message, ex.State, ex);"); sb.CloseBlock(); // close if sb.AppendLine("throw;"); sb.CloseBlock(); // close catch sb.CloseBlock(); // command sb.CloseBlock(); // connection }
private SourceCodeBuilder GenerateActions(SourceCodeBuilder sb) { foreach (var a in _actions) { a.WriteAttributes(sb); var args = a.RequestArguments.ToString(); if (a.IsResponseCollection) { sb.AppendLine($"public async Task<{a.ResponseType}[]> {a.Name}({args})"); } else { sb.AppendLine($"public async Task<{a.ResponseType}> {a.Name}({args})"); } sb.OpenBlock(); sb.AppendLine($"{a.HandlerRequestType.Name} arg = new {a.HandlerRequestType.Name}();"); foreach (var arg in a.RequestArguments) { sb.AppendLine($"this._mapper.Map({arg.Name}, arg);"); } sb.AppendLine($"var result = await _{a.Name.StartLower()}Handler.Execute(arg);"); if (a.IsResponseCollection) { sb.AppendLine($"return result.Select(x => _mapper.Map<{a.ResponseType}>(x)).ToArray();"); } else { sb.AppendLine($"return _mapper.Map<{a.ResponseType}>(result);"); } sb.CloseBlock().AppendLine(); } return(sb); }