Skip to content

A sample app using C# Incremental Source Generators

License

Notifications You must be signed in to change notification settings

dlandi/AutoGeneratedSystem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 
 
 
 
 

Repository files navigation

AutoGeneratedSystem

A sample app using C# Incremental Source Generators C# Source Generatos allow you to inspect and enrich your code while compiling. In this specific sample, we are using Incremental Generators to generate the code for Services, API Endpoints, and API Clients. You will see some partial classes decorated with attributes such as

  • [ServiceOfEntity("{entityName}")]
  • [ControllerOfEntity("{entityName}")]
  • [ClientServiceOfEntity("{entityName}")]

These attribute are being used in the Incremental Generators code to identify which classes the developer wants to automatically create the code for.

For the auto generation of the Blazor pages, currently, the process is a little bit different. You need to create a file "AutoGeneratePages.txt" in the location "/Pages/{entityName}"

The file needs to have all of each CRUD action you want to autogenerated A CRUD action would be any of these

  • Create
  • List
  • Edit
  • Delete

Note: Currently, the Edit functionality is not implemented

After you have created the file, change the file's Build Action to "C# analyzer additional file", and compile your application. The generator will create the .razor and .razor.cs files for each of the CRUD actions specified in the text file. The "BlazorPagesIncrementalGenerator" currently creates the files by writing them directly on disk ("/Pages/{entity}/{CRUD Action}.razor"), in order to avoid manually implementing all of the .razor autogeneration logic performed by Visual Studio, therefore, you may need to compile 1-2 times before debugging your application in order for the autogenerated pages to work correctly.

Check this video to know more: https://fairplaytube.pticostarica.com/Public/Videos/Details/ed67bf65cf

Support

Please support the development of our projects by donating on BuyMeACoffee https://www.buymeacoffee.com/efonsecab

Why I created this project?

I wanted to check the extend of C# Source Generators, by verifying if they would be feasible to create a whole system. I create lots of prototypes and in order to code fast I have established patterns, however, writing code manually takes time, by using this AutoGeneratedSystem with Source Code Generators, the required development time drastically reduces.

Use Cases

The main use case "AutoGeneratedSystem" can be used is for quickly developing administrative CRUD apps. Many times when companies develop software, they avoid creating administrative portals, and instead do everything through the database, which is extremely dangerous and error prone, especially in production scenarios. "AutoGeneratedSystem" helps you avoid depending on modifying the data directly, and instead gives you a simple CRUD UI, which helps you reduce risk of modifying data incorrectly.

Auto-Generated Pages

List

Note: Includes the Delete Action for each item in the list

image

Create

image

Note: Includes the loading of Junction Tables/Foreign Key items data

image

Development Guide

https://github.com/efonsecab/AutoGeneratedSystem/wiki/Development-Guide

Currently supporting code generation of

ServicesIncrementalGenerator

  • Create{entityName}Async
  • GetAll{entityName}
  • Delete{entityName}Async
  • Update{entityName}Async
  • Get{entityName}ByIdAsync

ControllersIncrementalGenerator

  • Create{entityName}
  • GetAll{entityName}
  • Delete{entityName}
  • Update{entityName}
  • Get{entityName}ById

ClientServicesIncrementalGenerator

  • Create{entityName}Async
  • GetAll{entityName}Async

BlazorPagesIncrementalGenerator

  • Create
  • List
  • Delete (as a link in the List page)

MauiBlazorPagesIncrementalGenerator

  • Create
  • List
  • Delete (as a link in the List page)

System Requirements

  • Version 17.2.0 (VS 2022) Preview 3.0
  • MAUI Workloads installed
  • Ngrok or any tunneling app (for being able to access the api hosted on the host machine, from the app published in the Android Emulator)

Recommended Visual Studio Extensions

  • EF Core Power Tools

Limitations

  • The "BlazorPagesIncrementalGenerator" class, which create the Blazor pages, will only create data-input fields for the following data types
    • bool?, bool
    • int?, int
    • long?, long
    • decimal?, decimal
    • double?, double
    • System.String?, System.String, string?, string
    • System.DateTimeOffset?, System.DateTimeOffset, System.DateTime?, System.DateTime

Known Issues

  • If you have any project with EmitCompilerGeneratedFiles set to True, the compilation may fail if you have the project located in a long path, to solve it remove the element or locate the project on the drive's root e.g. "C:\"
  • The auto-generated pages for the MAUI app are showing a build error for DateTimeOffset? properties, indicating the field cannot be bound.

About

A sample app using C# Incremental Source Generators

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published