Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public IEnumerable <ModelProperty> ReadColumnsFromDbObject()
        {
            try
            {
                if (string.IsNullOrEmpty(ConnectionString))
                {
                    throw new ArgumentException("Connection string is null or empty");
                }

                var properties = new List <ModelProperty>();

                using (var connection = new SqlConnection(ConnectionString))
                    using (var command = new SqlCommand(DbObjectName, connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        var parameterList = _dbSysUtils.GetParametersFromSP(ConnectionString, DbObjectName).ToArray();

                        command.Parameters.Clear();
                        command.Parameters.AddRange(parameterList);

                        _dbSysUtils.SetParametersToNull(command.Parameters);

                        connection.Open();

                        var sqlDataAdapter = new SqlDataAdapter(command);
                        var dataSet        = new DataSet();
                        sqlDataAdapter.FillSchema(dataSet, SchemaType.Source);

                        properties.AddRange(
                            from DataColumn dataColumn in dataSet.Tables[0].Columns
                            select new ModelProperty
                        {
                            PropertyName     = dataColumn.ColumnName,
                            PropertyDataType = _typeUtils.GetFriendlyNameByType(dataColumn.DataType),
                            IsNullable       = dataColumn.AllowDBNull
                        });

                        connection.Close();
                    }
                return(properties);
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);
                Debug.Flush();
                throw;
            }
        }
Ejemplo n.º 3
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);
        }