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(); } }
private SourceCodeBuilder WriteMethod_ReaderBlock(SourceCodeBuilder sb) { sb.AppendLine($"using(var rd = await command.ExecuteReaderAsync())").OpenBlock(); sb.AppendLine( $"var lz = new Lazy<int[]>(() => rd.GetIndexes({String.Join(", ", ResultBindings.Select(x => x.SqlColumnName.DblQuoted()))}), LazyThreadSafetyMode.None);"); if (_isCollectionResult) { sb.AppendLine($"List<{_resultType}> resultSet = new List<{_resultType}>();"); sb.AppendLine($"while(await rd.ReadAsync())").OpenBlock(); sb.AppendLine($"var ix = lz.Value;"); GenerateResult(sb); sb.AppendLine($"resultSet.Add(result);"); sb.CloseBlock(); sb.AppendLine("return resultSet.ToArray();"); } else { sb.AppendLine($"if(await rd.ReadAsync())").OpenBlock(); sb.AppendLine($"var ix = lz.Value;"); GenerateResult(sb); sb.AppendLine("return result;"); sb.CloseBlock(); sb.AppendLine("return null;"); } sb.CloseBlock(); // reader return(sb); }
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(); } }
public Type CreateDynamicAssertType() { if (ReceivedStatement == null) { ReceivedStatement = "Received(1)"; } if (ArgStatement == null) { ArgStatement = $"Arg.Any<{RequestType.Name}>()"; } if (ReturnsStatement == null) { ReturnsStatement = $"new {ResponseType.Name}()"; } SourceCodeBuilder code = new SourceCodeBuilder(); code.AppendLine("using System;"); code.AppendLine("using System.Threading.Tasks;"); code.AppendLine($"using {typeof(IDynamicMock).Namespace};"); code.AppendLine($"using {typeof(NSubstitute.Substitute).Namespace};"); code.AppendLine($"using {typeof(ICommandHandler<,>).Namespace};"); code.AppendLine($"using {typeof(MockRegister).Namespace};"); code.AppendLine($"using {RequestType.Namespace};"); code.AppendLine("namespace DynamicUtils").OpenBlock(); code.AppendLine($"public class DynamicMock : {typeof(IDynamicMock).Name}").OpenBlock(); code.AppendLine("private MockRegister _register;"); code.AppendLine($"public async Task Execute()").OpenBlock(); var handler = $"_register.GetHandler<{RequestType.Name},{ResponseType.Name}>()"; if (IsAssertion) { code.AppendLine($"await {handler}.{ReceivedStatement}.Execute({ArgStatement});"); } else { code.AppendLine($"{handler}.Execute({ArgStatement}).Returns({ReturnsStatement});"); } code.CloseBlock(); code.AppendLine($"public DynamicMock(MockRegister register)").OpenBlock(); code.AppendLine("this._register = register;").CloseBlock(); code.CloseBlock().CloseBlock(); TypeCompiler compiler = new TypeCompiler(); var assertationAssembly = compiler.FastLoad(code.ToString(), RequestType.Assembly, typeof(ValueTask <>).Assembly, Assembly.Load("System.Threading.Tasks.Extensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51")); return(assertationAssembly.GetType("DynamicUtils.DynamicMock")); }
protected override void GenerateExecuteMethod(SourceCodeBuilder sb) { string collection = _isCollectionResult ? "[]" : ""; sb.AppendLine($"public async Task<{_resultType}{collection}> Execute({_queryType} query)") .OpenBlock(); GenerateMethodBody(sb); 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(); }
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 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); }
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); }