Skip to content

Repository files navigation

Activout Database Client

Create a database client only by defining the C# interface you want. Uses Dapper for object mapping.

Create a Database Access Object (DAO) defining the C# interface you want and writing the SQL query. Shamelessly inspired by Jdbi Declarative API.

Rationale

The Activout Database Client provides a type-safe approach to make SQL requests to the database.

For the actual object mapping, Dapper is used but another implementation can be configured.

Example

publicinterfaceIUserDaoAsync{[SqlUpdate("CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR)")]TaskCreateTable();[SqlUpdate("INSERT INTO user(id, name) VALUES (@id, @name)")]TaskInsertNamed([Bind("id")]intid,[Bind("name")]stringname);[SqlUpdate("INSERT INTO user(id, name) VALUES (@id, @name)")]TaskInsertObject([BindProperties]Useruser);[SqlUpdate("INSERT INTO user(id, name) VALUES (@user_id, @user_name)")]TaskInsertObjectFull([BindProperties]Useruser);[SqlQuery("SELECT * FROM user ORDER BY name")]Task<IEnumerable<User>>ListUsers();[SqlQuery("SELECT * FROM user WHERE id = @id")]Task<User>GetUserById(intid);}_userDao=newDatabaseClientBuilder().With(newDapperGateway(sqliteConnection)).Build<IUserDaoAsync>();await_userDao.CreateTable();await_userDao.InsertNamed(42,null);varuser=await_userDao.GetUserById(42);

Full example code in DatabaseClientAsyncTest.cs.

Example with dependency injection

// Connection string example for MySQL:// "Server=example.com;Database=example;User=example;Password=example;AutoEnlist=True;Pooling=True;ConnectionReset=True;CharSet=utf8mb4;"varconnectionString=builder.Configuration.GetConnectionString("DefaultConnection");builder.Services.AddScoped<IDbConnection>(_ =>newMySqlConnection(connectionString));builder.Services.AddScoped<DatabaseClientBuilder>();builder.Services.AddScoped<IDatabaseClient>(sp =>sp.GetRequiredService<DatabaseClientBuilder>().With(newDapperGateway(sp.GetRequiredService<IDbConnection>())).Build<IDatabaseClient>());builder.Services.AddTransient<SomeService>();// ...publicclassSomeService(IDatabaseClientdatabaseClient){// ...}

Collaborate

This project is still under development - participation welcome!

Related projects

  • Activout.RestClient - Create a REST(ish) API client only by defining the C# interface you want.

About Activout

Activout AB is a software company in Ronneby, Sweden.

About

Create a database client only by defining methods on an interface and annotate them with SQL statements. Uses Dapper for object mapping.

Resources

Stars

6 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages