/// <summary> /// Writes the C# code of the table /// </summary> /// <returns></returns> public string Write() { var codeTarget = _codeWriterSettings.CodeTarget; _dataAnnotationWriter = new DataAnnotationWriter(IsEntityFramework(), _codeWriterSettings); var className = _table.NetName; if (string.IsNullOrEmpty(className) && _table.DatabaseSchema != null) { PrepareSchemaNames.Prepare(_table.DatabaseSchema, _codeWriterSettings.Namer); className = _table.NetName; } _dataTypeWriter.CodeTarget = codeTarget; _inheritanceTable = _table.FindInheritanceTable(); WriteNamespaces(); _codeWriterSettings.CodeInserter.WriteNamespaces(_table, _cb); if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace)) { _cb.BeginNest("namespace " + _codeWriterSettings.Namespace); } if (codeTarget == CodeTarget.PocoRiaServices) { WriteRiaClass(className); } else { var tableOrView = _table is DatabaseView ? "view" : "table"; var comment = "Class representing " + _table.Name + " " + tableOrView; var classDefinition = "public class " + className; if (_inheritanceTable != null) { classDefinition += " : " + _inheritanceTable.NetName; } _codeWriterSettings.CodeInserter.WriteTableAnnotations(_table, _cb); using (_cb.BeginNest(classDefinition, comment)) { WriteClassMembers(className); } } if (_table.HasCompositeKey && _inheritanceTable == null) { WriteCompositeKeyClass(className); } if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace)) { _cb.EndNest(); } return(_cb.ToString()); }
private string Write(EnumeratedDataType type) { CodeWriterUtils.WriteFileHeader(classBuilder); WriteUsings(); classBuilder.BeginNest($"namespace {codeWriterSettings.Namespace}"); classBuilder.BeginNest($"public enum {type.NetDataType}"); WriteEnumerationValues(type); classBuilder.EndNest(); classBuilder.EndNest(); return(classBuilder.ToString()); }
private string Write() { CodeWriterUtils.WriteFileHeader(classBuilder); WriteUsings(); using (classBuilder.BeginNest($"namespace {codeWriterSettings.Namespace}")) { using (classBuilder.BeginNest($"public static class {className}")) { WriteRegisterRepositories(); WriteRegisterEntities(); } } return(classBuilder.ToString()); }
private string Write() { WriteUsings(); using (classBuilder.BeginNest($"namespace {codeWriterSettings.Namespace}")) { using (classBuilder.BeginNest($"public static class {className}")) { using (classBuilder.BeginNest("public static void RegisterEnumerationTypeMappings()")) { WriteMapEnumerations(); classBuilder.AppendLine("NpgsqlConnection.GlobalTypeMapper.UseNetTopologySuite();"); } } } return(classBuilder.ToString()); }
/// <summary> /// Writes the C# code of the table /// </summary> /// <returns></returns> public string Write() { var codeTarget = _codeWriterSettings.CodeTarget; _dataAnnotationWriter = new DataAnnotationWriter(IsEntityFramework(), _codeWriterSettings); var className = _table.NetName; if (string.IsNullOrEmpty(className) && _table.DatabaseSchema != null) { PrepareSchemaNames.Prepare(_table.DatabaseSchema); className = _table.NetName; } _dataTypeWriter.CodeTarget = codeTarget; WriteNamespaces(); if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace)) { _cb.BeginNest("namespace " + _codeWriterSettings.Namespace); } if (codeTarget == CodeTarget.PocoRiaServices) { WriteRiaClass(className); } else { var tableOrView = _table is DatabaseView ? "view" : "table"; using (_cb.BeginNest("public class " + className, "Class representing " + _table.Name + " " + tableOrView)) { WriteClassMembers(className); } } if (_table.HasCompositeKey) { WriteCompositeKeyClass(className); } if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace)) { _cb.EndNest(); } return(_cb.ToString()); }
public static void WriteEntryLogging(ClassBuilder classBuilder, string methodSignature) { var methodName = RegexMethodName.Match(methodSignature).Groups["methodName"].Value; classBuilder.AppendLine($"this._logger.LogTrace(\"Entering {methodName}\");"); classBuilder.AppendLine("var stopwatch = new Stopwatch();"); classBuilder.AppendLine("stopwatch.Start();"); classBuilder.BeginNest("try"); }
private void AddEquals() { using (_cb.BeginNest("public override bool Equals(object obj)")) { _cb.AppendLine("var x = obj as " + NetName + ";"); _cb.AppendLine("if (x == null) return false;"); foreach (var column in _columns) { var primaryKeyName = _namer.PrimaryKeyName(column); var datatype = column.DataType ?? new DataType("x", "x"); //we'll only use scalar key properties //if (column.IsForeignKey) //{ // _cb.AppendLine("if (" + primaryKeyName + " == null && x." + primaryKeyName + " == null) return ReferenceEquals(this, x);"); //} //else if (datatype.IsNumeric) { _cb.AppendLine("if (" + primaryKeyName + " == 0 && x." + primaryKeyName + " == 0) return ReferenceEquals(this, x);"); } else if (datatype.IsString) { _cb.AppendLine("if (string.IsNullOrEmpty(" + primaryKeyName + ") && string.IsNullOrEmpty(" + primaryKeyName + ")) return object.ReferenceEquals(this, x);"); } } var sb = new StringBuilder(); sb.Append("return "); var i = 0; foreach (var column in _columns) { if (i != 0) { sb.Append(" && "); } i++; var primaryKeyName = _namer.PrimaryKeyName(column); sb.Append("(" + primaryKeyName + " == x." + primaryKeyName + ")"); } sb.AppendLine(";"); _cb.AppendLine(sb.ToString()); } }
public static void WriteExitLogging(ClassBuilder classBuilder, string methodSignature) { var methodName = RegexMethodName.Match(methodSignature).Groups["methodName"].Value; classBuilder.EndNest(); classBuilder.BeginNest("finally"); classBuilder.AppendLine("stopwatch.Stop();"); classBuilder.AppendLine($"this._logger.LogTrace($\"Exiting {methodName}\\t\\t{{stopwatch.ElapsedMilliseconds}}\");"); classBuilder.EndNest(); }
public static void BeginNestNamespace(ClassBuilder classBuilder, CodeWriterSettings codeWriterSettings) { if (!string.IsNullOrEmpty(codeWriterSettings.Namespace)) { classBuilder.BeginNest("namespace " + codeWriterSettings.Namespace); return; } throw new ArgumentNullException("codeWriterSettings.Namespace"); }
public string Write() { if (string.IsNullOrEmpty(table.NetName) && table.DatabaseSchema != null) { PrepareSchemaNames.Prepare(table.DatabaseSchema, codeWriterSettings.Namer); } CodeWriterUtils.WriteFileHeader(classBuilder); WriteUsings(); CodeWriterUtils.BeginNestNamespace(classBuilder, codeWriterSettings); classBuilder.AppendXmlSummary($"Class representing the {table.Name} table."); classBuilder.AppendLine($"[Table(\"\\\"{table.Name}\\\"\")]"); using (classBuilder.BeginNest($"public partial class {table.NetName}")) { WriteAllMembers(); } classBuilder.EndNest(); return(classBuilder.ToString()); }
private string Write() { if (string.IsNullOrEmpty(table.NetName) && table.DatabaseSchema != null) { PrepareSchemaNames.Prepare(table.DatabaseSchema, codeWriterSettings.Namer); } CodeWriterUtils.WriteFileHeader(classBuilder); WriteUsings(); CodeWriterUtils.BeginNestNamespace(classBuilder, codeWriterSettings); var tableOrView = table is DatabaseView ? "view" : "table"; var comment = $"Interface providing repository CRUD operations for the {table.Name} {tableOrView}"; var interfaceDefinition = $"public partial interface {CodeWriterUtils.GetRepositoryInterfaceName(table)}"; classBuilder.AppendXmlSummary(comment); classBuilder.BeginNest(interfaceDefinition); WriteInterfaceMembers(); classBuilder.EndNest(); // interface classBuilder.EndNest(); // namespace return(classBuilder.ToString()); }
public string Write() { //find first table with no dependencies (foreign keys) and a .Net name. var entity = _schema.Tables .FirstOrDefault(t => t.ForeignKeys.Count == 0 && !string.IsNullOrEmpty(t.NetName)); if (entity == null) { return(null); } //we'll also run a sproc if we find one var sproc = _schema.StoredProcedures.FirstOrDefault(p => p.ResultSets.Count > 0); ClassName = entity.NetName + "Test"; WriteNamespaces(sproc != null); using (_cb.BeginNest("namespace " + _codeWriterSettings.Namespace + ".Tests")) { _cb.AppendLine("[TestClass]"); using (_cb.BeginNest("public class " + ClassName)) { WriteStaticConstructor(entity); WriteOpenSession(); WriteGenerateString(); WriteCreateEntity(entity); WriteCrudTest(entity); WriteSproc(sproc); } } return(_cb.ToString()); }