Beispiel #1
0
        private static void WriteEntity(this SourceBuilder sb, Entity entity)
        {
            bool IsNullable(Field field)
            {
                return(!field.IsPartOfParentKey(entity) && field.Nullable);
            }

            var className = entity.GetModelName();

            sb.PublicClass(className, sb =>
            {
                var fullPrimaryKey = entity.GetFullPrimaryKey();
                var allFields      = fullPrimaryKey.Select(fr => fr.ResolvedField).Concat(entity.Fields).Distinct();
                foreach (var field in allFields)
                {
                    var type              = field.Type.ToCSharp();
                    var nullable          = IsNullable(field);
                    var nullabilityMark   = nullable ? "?" : "";
                    var name              = field.Name.ToPascalCase();
                    var identity          = field.Type.ToCSharpIdentity();
                    var defaultAssignment = !nullable && identity.Length > 0 ? $" = {identity};" : "";
                    sb.AL($"public {type}{nullabilityMark} {name} {{ get; set; }}{defaultAssignment}");
                }
            });
        }
 public static void PublicClass(
     this SourceBuilder sb,
     string ns,
     string className,
     Action <SourceBuilder> builder)
 {
     sb.Namespace(ns, sb =>
     {
         sb.PublicClass(className, builder);
     });
 }
Beispiel #3
0
        public void TestPublicClass_NoNamespace()
        {
            // Arrange
            var sb = new SourceBuilder();

            // Act
            sb.PublicClass("Foo", sb =>
            {
                sb.AL("System.Console.WriteLine(\"Hello World!\");");
            });
            // Assert
            Assert.Equal(@"public class Foo
{
    System.Console.WriteLine(""Hello World!"");
}
", sb.ToString());
        }