Beispiel #1
0
        public void AttributeOnMultiplePropertiesInClass()
        {
            ClassTemplate template = new ClassTemplate((NamespaceTemplate)null, "MyClass");

            template.AddProperty("Property1", Code.Type("string"))
            .WithAttribute("Attribute", Code.String("value"));
            template.AddProperty("Property2", Code.Type("string"))
            .WithAttribute("Attribute", Code.String("value"));
            this.output.Add(template);
            Assert.AreEqual("public partial class MyClass\r\n{\r\n    [Attribute(\"value\")]\r\n    public string Property1 { get; set; }\r\n\r\n    [Attribute(\"value\")]\r\n    public string Property2 { get; set; }\r\n}", this.output.ToString());
        }
Beispiel #2
0
        public void ClassOnePropertyWithAttributeAndOneNormalProperty()
        {
            ClassTemplate template = new ClassTemplate((NamespaceTemplate)null, "test");

            template.AddProperty("Prop1", Code.Type("string")).WithAttribute("attr");
            template.AddProperty("Prop2", Code.Type("string"));
            ClassWriter writer = new ClassWriter(this.options);

            writer.Write(template, this.output);
            Assert.AreEqual("public partial class test\r\n{\r\n    [attr]\r\n    public string Prop1 { get; set; }\r\n\r\n    public string Prop2 { get; set; }\r\n}", this.output.ToString());
        }
Beispiel #3
0
        protected virtual PropertyTemplate AddProperty(ModelTransferObject model, MemberTransferObject member, ClassTemplate classTemplate)
        {
            bool canRead  = true;
            bool canWrite = true;

            if (member is PropertyTransferObject property)
            {
                canRead  = property.CanRead;
                canWrite = property.CanWrite;
            }
            IOptions propertyOptions = this.Options.Get(member);

            if (model.Language != null && propertyOptions.Language != null)
            {
                this.MapType(model.Language, propertyOptions.Language, member.Type);
            }
            PropertyTemplate propertyTemplate = classTemplate.AddProperty(member.Name, member.Type.ToTemplate()).FormatName(propertyOptions);

            propertyTemplate.HasGetter = canRead;
            propertyTemplate.HasSetter = canWrite;
            if (propertyOptions.WithOptionalProperties)
            {
                propertyTemplate.Optional();
            }
            if (member.Type != model)
            {
                this.AddUsing(member.Type, classTemplate, propertyOptions);
            }
            return(propertyTemplate);
        }
Beispiel #4
0
        public void ClassOneProperty()
        {
            ClassTemplate template = new ClassTemplate((NamespaceTemplate)null, "test");

            template.AddProperty("Prop1", Code.Type("string"));
            ClassWriter writer = new ClassWriter();

            writer.Write(template, this.output);
            Assert.AreEqual("public partial class test\r\n{\r\n    public string Prop1 { get; set; }\r\n}", this.output.ToString());
        }
        protected virtual PropertyTemplate AddProperty(ModelTransferObject model, string name, TypeTransferObject type, ClassTemplate classTemplate, IConfiguration configuration, bool canRead = true, bool canWrite = true)
        {
            this.MapType(model.Language, configuration.Language, type);
            PropertyTemplate propertyTemplate = classTemplate.AddProperty(name, type.ToTemplate()).FormatName(configuration);

            propertyTemplate.HasGetter = canRead;
            propertyTemplate.HasSetter = canWrite;
            this.AddUsing(type, classTemplate, configuration);
            return(propertyTemplate);
        }
 public void Write(DemoWriteConfiguration configuration, List <ITransferObject> transferObjects, List <FileTemplate> files)
 {
     foreach (ModelTransferObject model in transferObjects.OfType <ModelTransferObject>())
     {
         Logger.Trace($"Write {model.Name}...");
         ClassTemplate classTemplate = files.AddFile(configuration.RelativePath, configuration.AddHeader)
                                       .AddNamespace(model.Namespace)
                                       .AddClass(model.Name)
                                       .FormatName(configuration);
         foreach (PropertyTransferObject property in model.Properties)
         {
             classTemplate.AddProperty(property.Name, property.Type.ToTemplate());
         }
         if (configuration.Test2)
         {
             classTemplate.WithUsing("System", null, null)
             .AddMethod("Test2", Code.Void())
             .Code.AddLine(Code.Static(Code.Type("Console")).Method("WriteLine", Code.String("Hello World!")).Close());
         }
     }
 }
Beispiel #7
0
        protected virtual ClassTemplate WriteClass(EntityFrameworkWriteConfiguration configuration, List <ITransferObject> transferObjects, List <FileTemplate> files)
        {
            ClassTemplate dataContext = files.AddFile(configuration.RelativePath, configuration.AddHeader)
                                        .AddNamespace(configuration.Namespace)
                                        .AddClass("DataContext", Code.Type("DbContext"));

            if (configuration.IsCore)
            {
                dataContext.WithUsing("Microsoft.EntityFrameworkCore");
            }
            else
            {
                dataContext.WithUsing("System.Data.Entity");
            }

            configuration.Usings.ForEach(x => dataContext.AddUsing(x));

            PropertyTemplate defaultConnectionProperty = dataContext.AddProperty("DefaultConnection", Code.Type("string")).Static().WithDefaultValue(Code.String("name=DataContext"));

            foreach (EntityTransferObject entity in transferObjects.OfType <EntityTransferObject>())
            {
                dataContext.AddProperty(entity.Name, Code.Generic("DbSet", entity.Model.ToTemplate()))
                .FormatName(configuration)
                .Virtual();
            }

            dataContext.AddConstructor()
            .WithThisConstructor(Code.Null());

            ConstructorTemplate constructor      = dataContext.AddConstructor();
            ParameterTemplate   connectionString = constructor.AddParameter(Code.Type("string"), "connectionString");

            if (configuration.IsCore)
            {
                constructor.WithBaseConstructor(Code.Static(Code.Type("SqlServerDbContextOptionsExtensions")).Method("UseSqlServer", Code.New(Code.Type("DbContextOptionsBuilder")), Code.NullCoalescing(Code.Local(connectionString), Code.Local(defaultConnectionProperty))).Property("Options"))
                .Code.AddLine(Code.This().Property("Database").Method("SetCommandTimeout", Code.Number(configuration.DataContext.CommandTimeout)).Close());
            }
            else
            {
                constructor.WithBaseConstructor(Code.NullCoalescing(Code.Local("connectionString"), Code.Local(defaultConnectionProperty)))
                .Code.AddLine(Code.This().Property("Database").Property("CommandTimeout").Assign(Code.Number(configuration.DataContext.CommandTimeout)).Close());
            }

            MethodTemplate    createMethod = dataContext.AddMethod("OnModelCreating", Code.Void()).Protected().Override();
            ParameterTemplate modelBuilder = createMethod.AddParameter(Code.Type(configuration.IsCore ? "ModelBuilder" : "DbModelBuilder"), "modelBuilder");

            if (!configuration.IsCore)
            {
                createMethod.Code.AddLine(Code.Local(modelBuilder).Property("Configurations").Method("AddFromAssembly", Code.This().Method("GetType").Property("Assembly")).Close());
            }

            foreach (EntityTransferObject entity in transferObjects.OfType <EntityTransferObject>())
            {
                createMethod.Code.AddLine(Code.Local(modelBuilder).GenericMethod("Entity", entity.Model.ToTemplate()).BreakLine()
                                          .Method("ToTable", Code.String(entity.Table), Code.String(entity.Schema)).BreakLine()
                                          .Method("HasKey", Code.Lambda("x", Code.Csharp("new { " + string.Join(", ", entity.Keys.Select(key => $"x.{key.Name}")) + " }"))).Close());
            }
            foreach (StoredProcedureTransferObject storedProcedure in transferObjects.OfType <StoredProcedureTransferObject>())
            {
                dataContext.AddMethod(storedProcedure.Name, storedProcedure.ReturnType.ToTemplate())
                .Code.AddLine(Code.This().Property("Database").Method("ExecuteSqlCommand", Code.String($"exec {storedProcedure.Schema}.{storedProcedure.Name}")).Close());
            }
            return(dataContext);
        }