示例#1
0
        public StringBuilder GenerateCode(IEnumerable <ModelProperty> properties, string connectionString, string dbObjectName)
        {
            var modelProperties = properties.ToList();

            var tableName = _dbSysUtils.GetTablesFromSP(connectionString, dbObjectName)
                            .ToList()
                            .FirstOrDefault()
                            .TransformTo(CaseType.SentenceCase);

            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine($"public class Table{tableName.TransformTo(CaseType.SentenceCase)}");
            stringBuilder.AppendLine("{");

            foreach (var modelProperty in modelProperties)
            {
                stringBuilder.AppendLine("  public "
                                         + modelProperty.PropertyDataType
                                         + _modelPropertyController.GetNullableMarkIfApplicable(modelProperty) + " "
                                         + modelProperty.PropertyName.TransformTo(CaseType.SentenceCase)
                                         + " { get; set; }"
                                         );
            }

            stringBuilder.AppendLine("}");

            return(stringBuilder);
        }
示例#2
0
        public StringBuilder GenerateCode(IEnumerable <ModelProperty> properties, string connectionString, string dbObjectName)
        {
            if (!string.IsNullOrWhiteSpace(connectionString))
            {
                _connectionString = connectionString;
            }
            var modelProperties = properties.ToList();
            var tableName       = _dbSysUtils.GetTablesFromSP(_connectionString, dbObjectName).ToList().FirstOrDefault();
            var firstProperty   = modelProperties.FirstOrDefault();
            var parametersExist = _dbSysUtils.GetParametersFromSP(_connectionString, dbObjectName).Any();

            if (tableName == null || firstProperty == null)
            {
                throw new ArgumentException($"It's not possible to retrieve the first table name from this SP: {dbObjectName}");
            }
            tableName = tableName.TransformTo(CaseType.SentenceCase).Replace("_", "");
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("[TestFixture, Category(\"Integration\")]");
            stringBuilder.AppendLine($"public class {tableName}RepoTests");
            stringBuilder.AppendLine("{");
            stringBuilder.AppendLine("  private TransactionScope _scope;");
            stringBuilder.AppendLine($"  private I{tableName}Repo _target;");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("  [OneTimeSetUp]");
            stringBuilder.AppendLine("  public void Setup()");
            stringBuilder.AppendLine("  {");
            stringBuilder.AppendLine("      _scope = new TransactionScope();");
            stringBuilder.AppendLine($"      _target = new {tableName}Repo();");
            stringBuilder.AppendLine("  }");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("  [Test]");
            stringBuilder.AppendLine($"  public void Test_{tableName}Repo_Methods()");
            stringBuilder.AppendLine("  {");
            stringBuilder.AppendLine("      //Arrange");
            stringBuilder.AppendLine($"      const int test{firstProperty.PropertyName.TransformTo(CaseType.SentenceCase)} = int.MaxValue;");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("      //Act");
            stringBuilder.AppendLine($"      _target.Save(new Table{tableName}");
            stringBuilder.AppendLine("      {");
            stringBuilder.AppendLine($"          {firstProperty.PropertyName.TransformTo(CaseType.SentenceCase)} = test{firstProperty.PropertyName.TransformTo(CaseType.SentenceCase)}");
            stringBuilder.AppendLine("      });");
            stringBuilder.AppendLine(parametersExist
                ? $"      var {tableName.ToLower()}s = _target.GetBy{firstProperty.PropertyName.TransformTo(CaseType.SentenceCase)}(test{firstProperty.PropertyName.TransformTo(CaseType.SentenceCase)});"
                : $"      var {tableName.ToLower()}s = _target.GetAll();");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("      //Assert");
            stringBuilder.AppendLine($"      Assert.IsNotNull({tableName.ToLower()}s);");
            stringBuilder.AppendLine($"      Assert.AreEqual({tableName.ToLower()}s.FirstOrDefault()?.{firstProperty.PropertyName.TransformTo(CaseType.SentenceCase)}, test{firstProperty.PropertyName.TransformTo(CaseType.SentenceCase)});");
            stringBuilder.AppendLine("  }");
            stringBuilder.AppendLine("}");

            return(stringBuilder);
        }
示例#3
0
        public ActionResult GenerateModel(string connectionString, string dbObjectName, CodeServiceType codeServiceType)
        {
            if (string.IsNullOrEmpty(connectionString) ||
                string.IsNullOrEmpty(dbObjectName) ||
                codeServiceType == 0)
            {
                return(View("Index", new GeneratedModelCodeViewModel()));
            }

            _spReadingService.ConnectionString = connectionString;
            _spReadingService.DbObjectName     = dbObjectName;
            var modelProperties = _spReadingService.ReadColumnsFromDbObject().ToList();

            switch (codeServiceType)
            {
            case CodeServiceType.TableModel:
                _generatorService = new TableModelGenerator();
                break;

            case CodeServiceType.Repo:
                _generatorService = new RepoGenerator();
                break;

            case CodeServiceType.RepoTest:
                _generatorService = new RepoTestGenerator();
                break;

            default:
                throw new ArgumentException("That generator does not exists");
            }

            var generatedModelCodeViewModel = new GeneratedModelCodeViewModel
            {
                ModelName       = _dbSysUtils.GetTablesFromSP(connectionString, dbObjectName).ToList().FirstOrDefault(),
                ModelCode       = _generatorService.GenerateCode(modelProperties, connectionString, dbObjectName),
                CodeServiceType = codeServiceType
            };

            return(View("Index", generatedModelCodeViewModel));
        }
示例#4
0
        public StringBuilder GenerateCode(IEnumerable <ModelProperty> properties, string connectionString, string dbObjectName)
        {
            if (!string.IsNullOrWhiteSpace(connectionString))
            {
                _connectionString = connectionString;
            }
            var modelProperties = properties.ToList();
            var tableName       = _dbSysUtils.GetTablesFromSP(_connectionString, dbObjectName).ToList().FirstOrDefault();
            var firstProperty   = modelProperties.FirstOrDefault();
            var lastProperty    = modelProperties.Last();

            var parametersExist = _dbSysUtils.GetParametersFromSP(_connectionString, dbObjectName).Any();

            if (tableName == null || firstProperty == null)
            {
                throw new ArgumentException($"It's not possible to retrieve the first table name from this SP: {dbObjectName}");
            }
            var spName = tableName;

            tableName = tableName.TransformTo(CaseType.SentenceCase).Replace("_", "");

            var stringBuilder = new StringBuilder();

            if (!parametersExist)
            {
                stringBuilder.AppendLine($"public interface I{tableName}Repo");
                stringBuilder.AppendLine("{");
            }
            stringBuilder.AppendLine(parametersExist
                    ? $"  public Table{tableName} GetBy{firstProperty.PropertyName.TransformTo(CaseType.SentenceCase)}({firstProperty.PropertyDataType.ToLower()} {firstProperty.PropertyName.ToLower()});"
                    : $"  IEnumerable<Table{tableName}> GetAll();");
            stringBuilder.AppendLine($"  void Save(Table{tableName} table{tableName});");
            if (!parametersExist)
            {
                stringBuilder.AppendLine("}");
            }
            stringBuilder.AppendLine();
            if (!parametersExist)
            {
                stringBuilder.AppendLine($"public class {tableName}Repo : DataAccessBaseRepo, I{tableName}Repo");
                stringBuilder.AppendLine("{");
            }
            stringBuilder.AppendLine(parametersExist
                ? $"  public Table{tableName} GetBy{firstProperty.PropertyName.TransformTo(CaseType.SentenceCase)}({firstProperty.PropertyDataType.ToLower()} {firstProperty.PropertyName.ToLower()})"
                : $"  public IEnumerable<Table{tableName}> GetAll()");
            stringBuilder.AppendLine("  {");
            stringBuilder.AppendLine(parametersExist
                ? $"      Table{tableName} table{tableName};"
                : $"      var table{tableName} = new List<Table{tableName}>();");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine($"      using (var db = new {DbDataContext}(ConnectionString))");
            stringBuilder.AppendLine("      {");
            stringBuilder.AppendLine(parametersExist
                ? $"          var dataList = db.{dbObjectName.ToUpper()}({firstProperty.PropertyName.ToLower()}).FirstOrDefault();"
                : $"          var dataList = db.{dbObjectName.ToUpper()}().ToList();");
            stringBuilder.AppendLine(parametersExist
                ? $"          table{tableName} = new Table{tableName}"
                : $"          table{tableName}.AddRange(dataList.Select(data{tableName} => new Table{tableName}");
            stringBuilder.AppendLine("          {");

            foreach (var property in modelProperties)
            {
                stringBuilder.Append("              " + property.PropertyName.TransformTo(CaseType.SentenceCase) + $" = data{tableName}." +
                                     property.PropertyName.TransformTo(CaseType.SentenceCase));

                if (property.PropertyName.Contains("TIMESTAMP"))
                {
                    stringBuilder.Append(".UtcDateTime");
                }

                if (!property.Equals(lastProperty))
                {
                    stringBuilder.AppendLine(",");
                }
            }

            stringBuilder.AppendLine();
            stringBuilder.AppendLine(parametersExist ? "          };" : "          }));");
            stringBuilder.AppendLine("      }");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine($"      return table{tableName};");
            stringBuilder.AppendLine("  }");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine($"  public void Save(Table{tableName} table{tableName})");
            stringBuilder.AppendLine("  {");
            stringBuilder.AppendLine($"      using (var db = new {DbDataContext}(ConnectionString))");
            stringBuilder.AppendLine("      {");
            stringBuilder.Append($"          db.SAVE_{spName}(");

            foreach (var property in modelProperties)
            {
                stringBuilder.Append($"table{tableName}.{property.PropertyName.TransformTo(CaseType.SentenceCase)}");
                if (!property.Equals(lastProperty))
                {
                    stringBuilder.Append(", ");
                }
            }
            stringBuilder.AppendLine(");");
            stringBuilder.AppendLine("      }");
            stringBuilder.AppendLine("  }");

            if (!parametersExist)
            {
                stringBuilder.AppendLine("}");
            }

            return(stringBuilder);
        }