static void Main(string[] args) { var a = new { SomeInfo = "ABC" }; var ctx = GenerationContext .Create() .File(file => { file .Using("System") .Namespace(ns => { ns .Name("Acme.Dal") .Class(c => { c.Attribute(attr => attr .Name("ModelAttribute") .Value(RawInlineBuilder.Create("\"Foo.Bar\"")) .Value(RawInlineBuilder.Create("1")) ); c .Name("Person") .Partial(true) .Property(p => p.Name("Id").Type("long").Virtual(true).Attribute(ab => ab.Name("SomeAttribute").Value("1", "\"David\""))) .Property(p => p.Name("FirstName").Type("string").Virtual(true)) .Property(p => p.Name("LastName").Type("string").Virtual(true).Meta(a)); }) .Class(c => { c .Name("PersonModelBase") .Partial(true) .Property(p => p.Name("Id").Type("long?").Virtual(true)) .Property(p => p.Name("FirstName").Type("string").Virtual(true)) .Property(p => p.Name("LastName").Type("string").Virtual(true)) .Method(m => { m .Name("From") .Virtual(true) .Parameter(p => p.Name("entity").Type("Person")) .Add(RawLineBuilder.Create("Id = entity.Id")) .Add(RawLineBuilder.Create("FirstName = entity.FirstName")) .Add(RawLineBuilder.Create("LastName = entity.LastName")); }) .Method(m => { m .Attribute(ma => ma.Name("CoolMethod")) .Attribute(ma => ma.Name("OtherMethod").Value("1", "false")) .Name("To") .Virtual(true) .Parameter(p => p.Name("entity").Type("Person")) .Add(() => { return(IfBuilder .Create() .RawCondition(rc => rc.Condition("Id != null")) .Add(RawLineBuilder.Create("entity.Id = Id.Value"))); }) .Add(RawLineBuilder.Create("entity.FirstName = FirstName")) .Add(RawLineBuilder.Create("entity.LastName = LastName")); }); }) ; }); }); var propertyOfMetaA = ctx.FindClass("Person").FindByMeta <PropertyBuilder>(a); var lines = ctx.Files.First().GenerateLines(); Console.WriteLine(string.Join("\n", lines)); Console.ReadKey(); /* * var ns = NamespaceBuilder * .Create() * .Name("Acme.Models") * .Class(c => c * .Partial(true) * .Name("PersonModelBase") * //.Field(f => f.AccessModifier(AccessModifiers.Private).Name("_firstName").Type("string")) * //.Field(f => f.AccessModifier(AccessModifiers.Private).Name("_lastName").Type("string")) * .Property(p => p.Name("FirstName").Type("string").Virtual(true)) * .Property(p => p.Name("LastName").Type("string").Virtual(true)) * .Property(p => p.Name("DateOfBirth").Type("DateTime?").Virtual(true)) * .Method(fromMethod => * { * fromMethod * .Name("From") * .Virtual(true) * .ReturnType("void") * .Parameter(p => p.Name("entity").Type("Acme.Dal.Person")) * .Add(() => * { * return IfBuilder * .Create() * .RawCondition(rc => rc.Condition("entity.DateOfBirth != null")) * .Add(RawLineBuilder.Create("DateOfBirth = entity.DateOfBirth")); * }) * .Add(() => * { * return ElseIfBuilder * .Create() * .RawCondition(rc => rc.Condition("entity.DateOfBirth == null")) * .Add(RawLineBuilder.Create("DateOFBirth = DateTime.Now")); * }) * .Add(() => * { * return ElseBuilder * .Create() * .Add(RawLineBuilder.Create("entity = null")); * }); * }) * ); * * * var lines = FileBuilder * .Create() * .Using("System") * .Add(ns) * .GenerateLines(); * * //.SaveToFile("C:\\test\\Person.cs", Encoding.UTF8); * * Console.WriteLine(string.Join("\n", lines)); * Console.ReadKey();*/ }
protected override void GenerateContext() { var contextNamespace = ContextNamespace(); var contextClassName = ContextClassName(); Action <FileBuilder> generateContextInline = (FileBuilder fileBuilder) => { if (!Options.OutputToSingleFile) { var filePath = $"{Options.OutputDir}{Path.DirectorySeparatorChar}{Options.ContextName}.generated.cs"; fileBuilder.Path(filePath); } fileBuilder.Using("Microsoft.EntityFrameworkCore"); fileBuilder.Namespace(contextNamespace, true, ns => { ns.Class(contextClassName, true, contextClass => { contextClass.Partial(true).Inherits(Options.ContextBaseClassName); TablesToGenerate.ForEach(table => { var tableClassFullName = TableClassFullName(table); var tableNamePlural = Pluralize(table.Name); contextClass.Property(tableNamePlural, true, dbSetProp => { dbSetProp.Virtual(true).Type($"DbSet<{tableClassFullName}>"); }); }); // empty constructor. contextClass.Constructor(c => c.Class(contextClass)); // constructor with options. contextClass.Constructor(c => c .Class(contextClass) .Parameter(p => p.Type($"DbContextOptions<{contextClassName}>").Name("options")) .BaseParameter("options") ); // override On Configuring contextClass.Method(m => { m .AccessModifier(AccessModifiers.Protected) .Override(true) .ReturnType("void") .Name("OnConfiguring") .Parameter(p => p.Type("DbContextOptionsBuilder").Name("optionsBuilder")); if (Options.AddConnectionStringOnGenerate) { m.Add(() => { return(IfBuilder.Create() .RawCondition(c => c.Condition("!optionsBuilder.IsConfigured")) .Add(RawLineBuilder.Create( "#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.") .NoEndOfLine()) .Add(UseDatabaseEngineConnectionStringLine())); }); } }); // model creating. contextClass.Method(m => { m .AccessModifier(AccessModifiers.Protected) .Override(true) .ReturnType("void") .Name("OnModelCreating") .Parameter(p => p.Type("ModelBuilder").Name("modelBuilder")); TablesToGenerate.ForEach(table => { AddFluentToMethod(m, table); }); SequenceToGenerate.ForEach(sequence => { var dataType = DataTypeResolver.ResolveType(sequence); var outputType = dataType.GetOutputType(); m.RawLine($"modelBuilder.HasSequence<{outputType}>(\"{sequence.Name}\").StartsAt({sequence.StartAt}).IncrementsBy({sequence.IncrementsBy})"); }); }); }); }); }; if (Options.OutputToSingleFile) { GenerationContext.SingleFile(fb => generateContextInline(fb)); } else { GenerationContext.FileIfPathIsSet(fb => generateContextInline(fb)); } }
protected virtual void GenerateModelExtensions(ITable table) { if (!Options.GenerateModelExtensions) { return; } if (!Options.GenerateModelsInterfaces && !Options.GenerateModels) { throw new Exception("Impossible to generate model extensions because neither GenerateModels or GenerateModelsInterfaces is activated."); } // full paths to model or interface var modelFullClassName = ModelClassFullName(table); var modelInterfaceFullName = ModelInterfaceFullName(table); // poco. var pocoFullInterfaceName = TableInterfaceFullName(table); var pocoFullClassName = TableClassFullName(table); // poco type. var pocoClassName = TableClassName(table); var pocoClassNamespace = TableNamespace(table); var pocoClass = GenerationContext.FindClass(pocoClassName, pocoClassNamespace); var modelExtensionNamespaceName = ModelExtensionsNamespace(table); var modelExtensionClassName = ModelExtensionsClassName(table); var fileBuilder = ResolveModelExtensionFileBuilder(table); fileBuilder.Namespace(modelExtensionNamespaceName, true, ns => { ns.Class(c => { // set basic info. c.Partial(true).Static(true).Name(modelExtensionClassName); var finalEntityType = Options.GenerateInterfaces ? pocoFullInterfaceName : pocoFullClassName; var finalModelType = Options.GenerateModelsInterfaces ? modelInterfaceFullName : modelFullClassName; c.Method(m => { m .AccessModifier(AccessModifiers.Public) .IsStatic(true) .Name("ToModel") .ReturnType("void") .Parameter(p => p.Name("source").Type($"this {finalEntityType}")) .Parameter(p => p.Name("model").Type(finalModelType)); table.Columns.ForEach(column => { m.RawLine($"model.{column.Name} = source.{column.Name}"); }); }); c.Method(m => { m .AccessModifier(AccessModifiers.Public) .IsStatic(true) .Name("FromModel") .ReturnType("void") .Parameter(p => p.Name("model").Type($"this {finalModelType}")) .Parameter(p => p.Name("destination").Type(finalEntityType)) .Parameter(p => p.Name("ignorePrimaryKey").Type("bool").DefaultValue("true")) ; table.Columns.ForEach(column => { var rawLine = ""; bool isPropertyNullable = IsModelPropertyNullable(column); // column.IsNullable || ShouldGenerateModelPropertyAsNullable(column); if (isPropertyNullable && !column.IsNullable) { var matchingProp = pocoClass.FindByMeta <PropertyBuilder>(column); var ternary = TernaryBuilder .Create() .RawCondition(rc => rc.Condition($"model.{column.Name} != null")) .True(RawInlineBuilder.Create( $"destination.{column.Name} = ({matchingProp.GetTypeName()})model.{column.Name}")) .False(RawInlineBuilder.Create( $"destination.{column.Name} = default({matchingProp.GetTypeName()})")); rawLine = $"destination.{column.Name} = {ternary.GenerateInline()}"; } else { rawLine = $"destination.{column.Name} = model.{column.Name}"; } if (column.IsPrimaryKey) { m.Add(IfBuilder.Create().RawCondition(rc => rc.Condition($"ignorePrimaryKey != true")).Add(RawLineBuilder.Create(rawLine))); } else { m.RawLine(rawLine); } }); }); }); }); }