예제 #1
0
        protected virtual void GenerateSequenceMethods()
        {
            if (!Options.GenerateContextSequenceMethods)
            {
                return;
            }

            var contextNamespace = ContextNamespace();
            var contextClassName = ContextClassName();
            var contextClass     = GenerationContext.FindClass(contextClassName, contextNamespace);

            SequenceToGenerate.ForEach(sequence =>
            {
                var dataType   = DataTypeResolver.ResolveType(sequence);
                var outputType = dataType.GetOutputType();

                var methodName = $"GetNextValueFor{sequence.Name}";

                contextClass.Method(m =>
                {
                    m.AccessModifier(AccessModifiers.Public)
                    .ReturnType(outputType)
                    .Name(methodName);

                    GenerateGetNextSequenceLines(m, outputType, sequence);
                });
            });
        }
예제 #2
0
        protected virtual void GenerateModelInterface(ITable table, FileBuilder fileBuilder)
        {
            if (!Options.GenerateModelsInterfaces)
            {
                return;
            }

            var tableNamespace     = TableNamespace(table);
            var tableClassName     = TableClassName(table);
            var modelInterfaceName = ModelInterfaceName(table);
            var tableClassFullName = TableClassFullName(table);
            var tableClass         = GenerationContext.FindClass(tableClassName, tableNamespace);


            if (!Options.OutputToSingleFile)
            {
                var filePath = $"{Options.OutputDir}{Path.DirectorySeparatorChar}{modelInterfaceName}.generated.cs";
                fileBuilder.Path(filePath);
            }

            fileBuilder.Namespace(tableNamespace, true, ns =>
            {
                ns.Interface(modelInterface =>
                {
                    // set basic info.
                    modelInterface.Partial(true).Name(modelInterfaceName);

                    // set properties.
                    table.Columns.ForEach(column =>
                    {
                        modelInterface.Property(columnProperty =>
                        {
                            var type                = DataTypeResolver.ResolveType(column);
                            var typeName            = type.GetOutputType();
                            bool isPropertyNullable = column.IsNullable || Options.GenerateModelPropertyAsNullable;
                            if (type.IsValueType && isPropertyNullable)
                            {
                                typeName = $"{typeName}?";
                            }

                            columnProperty
                            .AccessModifier(AccessModifiers.Omit)
                            .Name(column.Name)
                            .Type(typeName)
                            .Meta(column);
                        });
                    });
                });
            });
        }
예제 #3
0
        public virtual Tuple <string, bool> GetColumnTypeInfo(IColumn column)
        {
            var dynamicAssemblyResolvedType = ResolveDynamicAssemblyType(column);

            if (dynamicAssemblyResolvedType != null)
            {
                return(dynamicAssemblyResolvedType);
            }

            var type     = DataTypeResolver.ResolveType(column);
            var typeName = type.GetOutputType();

            return(new Tuple <string, bool>(typeName, type.IsValueType));
        }
예제 #4
0
        private void GenerateEntity(ITable table, FileBuilder fileBuilder)
        {
            var tableNamespace     = TableNamespace(table);
            var tableClassName     = TableClassName(table);
            var tableInterfaceName = TableInterfaceName(table);

            if (!Options.OutputToSingleFile)
            {
                var filePath = $"{Options.OutputDir}{Path.DirectorySeparatorChar}{tableClassName}.generated.cs";
                fileBuilder.Path(filePath);
            }

            fileBuilder.Namespace(tableNamespace, true, ns =>
            {
                ns.Class(tableClass =>
                {
                    // set basic info.
                    tableClass.Partial(true).Name(tableClassName);

                    if (Options.GenerateInterfaces)
                    {
                        tableClass.Inherits(tableInterfaceName);
                    }

                    // set properties.
                    table.Columns.ForEach(column =>
                    {
                        tableClass.Property(columnProperty =>
                        {
                            var type     = DataTypeResolver.ResolveType(column);
                            var typeName = type.GetOutputType();
                            if (type.IsValueType && column.IsNullable)
                            {
                                typeName = $"{typeName}?";
                            }

                            columnProperty
                            .Name(column.Name)
                            .Type(typeName)
                            .Meta(column);
                        });
                    });
                });
            });
        }
예제 #5
0
        protected virtual void GenerateEntityInterface(ITable table, FileBuilder fileBuilder)
        {
            if (!Options.GenerateInterfaces)
            {
                return;
            }

            var tableNamespace     = TableNamespace(table);
            var tableClassName     = TableClassName(table);
            var tableInterfaceName = TableInterfaceName(table);

            if (!Options.OutputToSingleFile)
            {
                var filePath = $"{Options.OutputDir}{Path.DirectorySeparatorChar}{tableInterfaceName}.generated.cs";
                fileBuilder.Path(filePath);
            }

            fileBuilder.Namespace(tableNamespace, true, ns =>
            {
                ns.Interface(tableInterface =>
                {
                    tableInterface.Partial(true).Name(tableInterfaceName);
                    table.Columns.ForEach(column =>
                    {
                        tableInterface.Property(columnProperty =>
                        {
                            var type     = DataTypeResolver.ResolveType(column);
                            var typeName = type.GetOutputType();
                            if (type.IsValueType && column.IsNullable)
                            {
                                typeName = $"{typeName}?";
                            }

                            columnProperty
                            .Name(column.Name)
                            .AccessModifier(AccessModifiers.Omit)
                            .Type(typeName)
                            .Meta(column);
                        });
                    });
                });
            });
        }
예제 #6
0
        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));
            }
        }
예제 #7
0
        protected virtual void GenerateModel(ITable table, FileBuilder fileBuilder)
        {
            if (!Options.GenerateModels)
            {
                return;
            }

            var tableNamespace     = TableNamespace(table);
            var tableClassName     = TableClassName(table);
            var modelClassName     = ModelClassName(table);
            var modelInterfaceName = ModelInterfaceName(table);
            var tableClassFullName = TableClassFullName(table);
            var tableClass         = GenerationContext.FindClass(tableClassName, tableNamespace);


            if (!Options.OutputToSingleFile)
            {
                var filePath = $"{Options.OutputDir}{Path.DirectorySeparatorChar}{modelClassName}.generated.cs";
                fileBuilder.Path(filePath);
            }

            fileBuilder.Namespace(tableNamespace, true, ns =>
            {
                ns.Class(modelClass =>
                {
                    // set basic info.
                    modelClass.Partial(true).Name(modelClassName);

                    if (Options.GenerateModelsInterfaces)
                    {
                        modelClass.Inherits(modelInterfaceName);
                    }

                    Options?.ModelInheritances.ForEach(mi =>
                    {
                        modelClass.Inherits(ReplaceMetas(mi, table));
                    });


                    MethodBuilder from = null;
                    MethodBuilder to   = null;

                    modelClass.Method(m =>
                    {
                        m
                        .AccessModifier(AccessModifiers.Public)
                        .Virtual(true)
                        .ReturnType("void")
                        .Name("From")
                        .Parameter(p => p.Type(tableClassFullName).Name("entity"));
                        from = m;
                    });

                    modelClass.Method(m =>
                    {
                        m
                        .AccessModifier(AccessModifiers.Public)
                        .Virtual(true)
                        .ReturnType("void")
                        .Name("To")
                        .Parameter(p => p.Type(tableClassFullName).Name("entity"));
                        to = m;
                    });

                    modelClass.Method(m => m
                                      .Virtual(true)
                                      .ReturnType("System.Type")
                                      .Name("GetContextType")
                                      .RawLine($"return typeof({ContextFullClassName()})")
                                      );

                    modelClass.Method(m => m
                                      .ReturnType("System.Type")
                                      .Name("GetEntityType")
                                      .RawLine($"return typeof({tableClassFullName})")
                                      );

                    // set properties.
                    table.Columns.ForEach(column =>
                    {
                        modelClass.Property(columnProperty =>
                        {
                            var type                = DataTypeResolver.ResolveType(column);
                            var typeName            = type.GetOutputType();
                            bool isPropertyNullable = column.IsNullable || Options.GenerateModelPropertyAsNullable;
                            if (type.IsValueType && isPropertyNullable)
                            {
                                typeName = $"{typeName}?";
                            }

                            columnProperty
                            .Virtual(true)
                            .Name(column.Name)
                            .Type(typeName)
                            .Meta(column);

                            from.RawLine($"{column.Name} = entity.{column.Name}");

                            if (isPropertyNullable && !column.IsNullable)
                            {
                                var matchingProp = tableClass.FindByMeta <PropertyBuilder>(column);
                                var ternary      = TernaryBuilder
                                                   .Create()
                                                   .RawCondition(rc => rc.Condition($"{column.Name} != null"))
                                                   .True(RawInlineBuilder.Create($"entity.{column.Name} = ({matchingProp.GetTypeName()}){column.Name}"))
                                                   .False(RawInlineBuilder.Create($"entity.{column.Name} = default({matchingProp.GetTypeName()})"));

                                to.RawLine($"entity.{column.Name} = {ternary.GenerateInline()}");
                            }
                            else
                            {
                                to.RawLine($"entity.{column.Name} = {column.Name}");
                            }
                        });
                    });
                });
            });
        }