fdonnet / Dapper-Layers-Generator

Automate layers generation for dapper (DAL/ POCO/ maybe API), based on database definitions. Read your DB and generate a lot of stuff to access your data.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dapper-Layers-Generator

For the "always DB first" lovers❤️

Generate all the ez and boring DAL accesses you need at the begining with dapper and you will be able to focus on the complex parts of your app! At the end, you will keep the control and can extend the code.


Read DB definitions from:

  • MySql / MariaDB
  • MsSql (will be next)
  • PostGreSQL
  • Oracle

Generated C# code based on your db definitions:

  • Poco / Entities for all tables
  • DbContext / DbContext factory (simple)
  • DAL base implementation (multi-dbs)
  • DAL MySql / Maria DB specifics
  • DAL MsSql DB specifics (will be next)
  • DAL PostGreSQL specifics
  • DAL Oracle specifics

Already implemented DAL methods in the generated code:

all the generated methods will have custom names based on your columns def, if your pk is "id" the generated method becomes GetByIdAsync(), if your unique index is on columns "firstname" and "lastname" the generated method name becomes GetByFirstnameAndLastNameAsync() etc)

  • GetAllAsync()
  • GetByPkAsync()
  • GetByListOfPKAsync()
  • GetByListOfPKBulkAsync() with MySqlBulkCopy and temp table
  • GetByUniqueIndexAsync()
  • AddAsync()
  • AddMultiAsync()
  • AddBulkAsync(), with MySqlBulkCopy
  • UpdateAsync()
  • UpdateMultiAsync()
  • UpdateBulkAsync(), with MySqlBulkCopy and temp table
  • DeleteAsync()
  • DeletebyPkListAsync()
  • DeleteBulkAsync(), with MySqlBulkCopy and temp table

Config and first simple start

Set the mandatory configs in appsettings or environnement or usersecrets

{
  "ConnectionStrings": {
    "Default": "Server=localhost;Database=information_schema;Uid=root;Pwd=root;"
  },

  "DB": {
    "Schemas": "testschema",
    "Provider": "MySql"
  }
}
  • ConnectionStrings:Default => a MySql/Mariadb connection string with read right on information_schema
  • DB:Schemas => the schemas/db name you want to read the definitions from to be able to generate your layers code
  • DB:Provider => only MySql supported now.

Run the app

MainMenu

===> Go on "Main settings"

MainSettings

Set at least the following configs

  • (3) Author name: your name
  • (4) Target project namespace: the main namespace for your generated code
  • (8) Target project path: physical path (where you want to generate) => don't forget the last backslash "\"

Check all the path specified exist on your file system. The generator will not create the folder structure.

Press (q) to go back to the main menu.

On the main menu, choose "Save"

Save

Complete path/filename.json where you want to save your config.

Generate

Generate

After your first code generation, return, load your config file and test all the available settings in (General settings / adavanced settings). You can go deeper as column config. I let you discover.


Generated code usage

In your net6 app/asp.net, register the dbcontext :

IServiceCollection _services = new ServiceCollection()
    .AddSingleton(_config)
    .AddTransient<IDbContext, DbContextMySql>();

and call your new generated dal in your controller/service or app :

var clients = await dal.ClientRepo.GetAllAsync();

with transaction :

using var trans = await dal.OpenTransactionAsync();
int newClientId = await dal.ClientRepo.AddAsync(new Client() { Firstname = "John", Lastname = "Smith", City = "Paris" });
int newFailureId = await dal.FailureRepo.AddAsync(new Failure() { Description="Fail to pass the door", ClientId = newClientId });
dal.CommitTransaction();

If you want to extent the generated code, create a new file and declare a partial class with the same name as the class you want to extent !

Open an issue if you have specific questions or if you detect an issue !

And love for

https://github.com/DapperLib/Dapper

https://github.com/spectreconsole/spectre.console

https://github.com/Humanizr/Humanizer

About

Automate layers generation for dapper (DAL/ POCO/ maybe API), based on database definitions. Read your DB and generate a lot of stuff to access your data.

License:MIT License


Languages

Language:C# 100.0%