This is a database operation support library that includes a fast micro O/R mapper (ORM).
It has 9 functions.
DbDataMapper
: This is a micro O/R mapper.QueryBuilder
: This helps construct the query and parameters.DbCommandAdapter
: The above two functions are combined to support command execution.DbRepositoryBase
: It provides basic CRUD operations on table.DataValidator
: It provides fast model validation.ComparableEnum
: This is a comparable enumlation.ComparableNullable
: This is a comparable nullable value.ComparableStructPack
: This is a comparable structure pack.TimestampedObject
: This is a timestamped object.
A Database with a package that implements classes that inherit from the DbCommand
and DbDataReader
classes.
Database | NuGet | GitHub | Project |
---|---|---|---|
MySQL | MySqlConnector | MySqlConnector | mysqlconnector.net |
PostgreSQL | Npgsql | Npgsql | Npgsql |
SQL Server | Microsoft.Data.Sqlclient | - | - |
SQLite | Microsoft.Data.Sqlite | - | - |
Package Name | NuGet | GitHub |
---|---|---|
Izayoi.Data.Comparable | Izayoi.Data.Comparable | Izayoi.Data |
Izayoi.Data.DbCommandAdapter | Izayoi.Data.DbCommandAdapter | Izayoi.Data |
Izayoi.Data.DbDataMapper | Izayoi.Data.DbDataMapper | Izayoi.Data |
Izayoi.Data.Packs | Izayoi.Data.Packs | Izayoi.Data |
Izayoi.Data.Query | Izayoi.Data.Query | Izayoi.Data |
Izayoi.Data.Repository | Izayoi.Data.Repository | Izayoi.Data |
Izayoi.Data.TimestampedObjects | Izayoi.Data.TimestampedObjects | Izayoi.Data |
Izayoi.Data.Validation | Izayoi.Data.Validation | Izayoi.Data |
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("users")]
public class User
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("name")]
public string Name { get; set; } = string.Empty;
[Column("age")]
public byte Age { get; set; }
[Column("gender")]
public GenderType Gender { get; set; }
[Column("created_at")]
public DateTime CreatedAt { get; set; }
[Column("updated_at")]
public DateTime UpdatedAt { get; set; }
}
- If the
[Table]
attribute is not defined, the class name is used as the table name. - If the
[Column]
attribute is not defined, the property name is used as the column name. - If the
[NotMapped]
attribute is defined, the property is excluded from the mapping. - The
[Key]
attribute is set to the primary key. It is used for update or delete methods.
The most important method is ExecuteQueryAsync<T>
.
And maybe you'll have an opportunity to use methods ReadToObjectAsync<T>
and ReadToObjectsAsync<T>
.
using System.Collections.Generic;
using System.Threading.Tasks;
using Izayoi.Data;
using Microsoft.Data.SqlClient; // for SQL Server
//using Microsoft.Data.Sqlite; // for SQLite
//using MySqlConnector; // for MySQL
//using Npgsql; // for PostgreSQL
public class UserRepository
{
private readonly string dbConnectionString;
private readonly DbDataMapper dbDataMapper = new();
public async Task<List<User>> GetUsers(CancellationToken cancellationToken)
{
using SqlConnection dbConnection = new(dbConnectionString);
using SqlCommand dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = "SELECT * FROM users";
dbConnection.Open();
// Map DB data to objects.
List<User> users = await dbDataMapper.ExecuteQueryAsync<User>(dbCommand, cancellationToken);
dbConnection.Close();
return users;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Izayoi.Data;
using Izayoi.Data.Query;
using Microsoft.Data.SqlClient; // for SQL Server
//using Microsoft.Data.Sqlite; // for SQLite
//using MySqlConnector; // for MySQL
//using Npgsql; // for PostgreSQL
public class UserRepository
{
private readonly string dbConnectionString;
private readonly DbCommandAdapter dbCommandAdapter;
private readonly DbDataMapper dbDataMapper;
private readonly QueryOption queryOption;
public UserRepository()
{
queryOption = new QueryOption(RdbKind.SqlServer);
dbDataMapper = new DbDataMapper();
dbCommandAdapter = new DbCommandAdapter(dbDataMapper, queryOption);
}
public async Task<User> AddUser(string name, byte age, GenderType gender, CancellationToken cancellationToken)
{
using SqlConnection dbConnection = new(dbConnectionString);
using SqlCommand dbCommand = dbConnection.CreateCommand();
dbConnection.Open();
var user = new User()
{
Id = 0,
Name = name,
Age = age,
Gender = gender,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
};
int userId = await dbCommandAdapter.InsertReturnAsync<int, User>(dbCommand, user, excludeKey: true, cancellationToken);
dbConnection.Close();
user.Id = userId;
return user;
}
public async Task<List<User>> GetTeenAgers(CancellationToken cancellationToken)
{
// CreateConnection -> CreateCommand -> Connection.Open
var select = new Select()
.SetFrom("users")
.AddField("*")
.AddWhere("age", OpType.BETWEEN, new int[] { 13, 19 })
.AddOrder("age", OType.ASC);
List<User> users = await dbCommandAdapter.SelectAsync<User>(dbCommand, select, cancellationToken);
return users;
}
}
manifest.json
{
"dependencies": {
"com.izayoi.data.comparable": "https://github.com/izayoijiichan/Izayoi.Data.git?path=Izayoi.Data.Comparable",
"com.izayoi.data.packs": "https://github.com/izayoijiichan/Izayoi.Data.git?path=Izayoi.Data.Packs",
"com.izayoi.data.timestampedobjects": "https://github.com/izayoijiichan/Izayoi.Data.git?path=Izayoi.Data.TimestampedObjects",
"com.izayoi.data.validation": "https://github.com/izayoijiichan/Izayoi.Data.git?path=Izayoi.Data.Validation",
"org.nuget.microsoft.bcl.hashcode": "6.0.0",
"org.nuget.system.componentmodel.annotations": "4.4.0"
}
}
Last updated: 1 February, 2025
Editor: Izayoi Jiichan
Copyright (C) 2024 Izayoi Jiichan. All Rights Reserved.