/CodegenCS

C# Library for Code Generation

Primary LanguageC#MIT LicenseMIT

CodegenCS

C# Library for Code Generation

... or Yet Another Code Generator. Maybe a little better than T4 templates.

This repository contains the CodegenCS core library, and the dotnet command-line tool dotnet-codegencs which contains some utilities (like extracting MSSQL/PostgreSQL schemas) and some out-of-the-box templates (like POCO generator).

CodegenCS (Core Library)

CodegenCS is a class library for code generation using pure C#.
Basically it provides a custom TextWriter tweaked to solve common issues in code generation:

  • Preserves indent (keeps track of current Indent level).
    When you write new lines it will automatically indent the line according to current level.
  • Helpers to concisely write indented blocks (C-style, Java-style or Python-style) using a Fluent API (IDisposable context will automatically close blocks)
  • Helpers to write multi-line blocks without having to worry about different indentations for control logic and output code.
  • Helpers to keep track of multiple files which can be saved at once in the output folder.
  • IF / ELSE / ENDIF symbols that can be embedded within the text strings and allow concise syntax for Control Blocks

Sample usage:

var w = new CodegenTextWriter();

Action<CodegenTextWriter> generateMyClass = w => w.Write($@"
    void MyClass()
    {{
        void Method1()
        {{
            // ...
        }}
        void Method2()
        {{
            // ...
        }}
    }}");


w.WriteLine($@"
    using System;
    using System.Collections.Generic;
    namespace MyNamespace
    {{
        {generateMyClass}
    }}");

w.SaveToFile("File1.cs"); 

Want to learn more? Check out the full documentation and the unit tests.

dotnet-codegencs (.NET global tool)

This is a .NET 5 global tool which is used as entry-point to launch some embedded utilities and or to run the out-of-the-box templates.

How to Install: dotnet tool install -g dotnet-codegencs

Usage - see all options: codegencs -?

This is a command-line tool which extracts the schema of a MSSQL or PostgreSQL database and save it in a JSON file.

Sample usage:

codegencs extract-dbschema postgresql "Host=localhost; Database=Adventureworks; Username=postgres; Password=MyPassword" AdventureWorks.json

codegencs extract-dbschema mssql "Server=MYSERVER; Database=AdventureWorks; User Id=myUsername;Password=MyPassword" AdventureWorks.json

codegencs extract-dbschema mssql "Server=(local)\SQLEXPRESS; Database=AdventureWorks; Integrated Security=True" AdventureWorks.json

This is a template that generates POCO classes from a JSON schema extracted with extract-dbschema.

Sample usage:

codegencs simplepocogenerator AdventureWorks.json --Namespace=MyProject.POCOs

codegencs simplepocogenerator AdventureWorks.json --Namespace=MyProject.POCOs --TargetFolder=OutputFolder --SingleFile=POCOs.generated.cs --CrudExtensions --CrudClassMethods

To see all available options use codegencs simplepocogenerator -? or check out Simple POCO documentation

It's also easy to customize the template output - check out how to do it

Template: Entity Framework Core

This is a template (still in beta) that generates EntityFrameworkCore Entities and DbContext from a JSON schema extracted with extract-dbschema.

Sample usage:

codegencs efcoregenerator AdventureWorks.json --TargetFolder=OutputFolder --Namespace=MyProject.POCOs --DbContextName=AdventureWorksDbContext

Contributing

This is a brand new project, and your contribution can help a lot.

Would you like to collaborate or share your own template?

Please submit a pull-request or if you prefer you can contact me to discuss your idea.

Some ideas for new features or templates:

  • Port DbSchema.Extractor to other database vendors
  • Generate Dapper/Petapoco classes from database schema files - check Simple POCO Generator
  • Generate EF Core Entities/DBContext
  • Generate REST Web API endpoints from OpenAPI YAML
  • Generate Nancy endpoints for retrieving/updating business entities
  • Generate REST or SOAP web service wrappers (client)
  • Generate ASP.NET MVC (Razor Views CSHTML and Controllers) to display and edit business entities
  • Data Access Objects from database schema files
  • Object caching
  • Application-level database journaling

History

License

MIT License