Izayoi.Data.DbDataMapper
1.0.0
dotnet add package Izayoi.Data.DbDataMapper --version 1.0.0
NuGet\Install-Package Izayoi.Data.DbDataMapper -Version 1.0.0
<PackageReference Include="Izayoi.Data.DbDataMapper" Version="1.0.0" />
paket add Izayoi.Data.DbDataMapper --version 1.0.0
#r "nuget: Izayoi.Data.DbDataMapper, 1.0.0"
// Install Izayoi.Data.DbDataMapper as a Cake Addin #addin nuget:?package=Izayoi.Data.DbDataMapper&version=1.0.0 // Install Izayoi.Data.DbDataMapper as a Cake Tool #tool nuget:?package=Izayoi.Data.DbDataMapper&version=1.0.0
Izayoi.Data.DbDataMapper
This is a fast micro O/R mapper (ORM) that stores data retrieved from a DB into any object.
Available Databases
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 | - | - |
Wiki
Examples
Database
-- SQL Server Example
CREATE TABLE [dbo].[users] (
[id] INT IDENTITY (1, 1) NOT NULL,
[name] NVARCHAR (50) NOT NULL,
[age] TINYINT NOT NULL,
[gender] TINYINT NOT NULL,
[created_at] DATETIME2 (7) NOT NULL,
[updated_at] DATETIME2 (7) NOT NULL,
PRIMARY KEY CLUSTERED ([id] ASC)
);
Map class
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
//[Table("users")]
[Table("users", Schema = "dbo")]
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; }
[NotMapped]
public int IgnoreProperty { 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, but is not used by theDbDataMapper
alone. (It is used when combined with theDbCommandAdapter
class.)
DbDataMapper
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 Example()
{
private readonly dbConnectionString;
private readonly DbDataMapper dbDataMapper = new();
public async Task Method(CancellationToken cancellationToken)
{
using SqlConnection dbConnection = new(dbConnectionString);
using SqlCommand dbCommand = dbConnection.CreateCommand();
dbConnection.Open();
List<User> users;
// Select method A
{
dbCommand.CommandText = "SELECT * FROM dbo.users";
using SqlDataReader dbDataReader = await dbCommand.ExecuteReaderAsync(cancellationToken);
users = await dbDataMapper.ReadToObjectsAsync<User>(dbDataReader, cancellationToken);
}
// Select method B
{
dbCommand.CommandText = "SELECT * FROM dbo.users";
users = await dbDataMapper.ExecuteQueryAsync<User>(dbCommand, cancellationToken);
}
// Select method C
{
users = await dbDataMapper.SelectAllAsync<User>(dbCommand, cancellationToken);
}
dbConnection.Close();
}
}
Remarks
Reuse a DbDataMapper
object whenever possible.
For a better experience
This library really shines when used in conjunction with the Izayoi.Data.DbCommandAdapter
.
Last updated: 16 August, 2024
Editor: Izayoi Jiichan
Copyright (C) 2024 Izayoi Jiichan. All Rights Reserved.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Izayoi.Data.DbDataMapper:
Package | Downloads |
---|---|
Izayoi.Data.DbCommandAdapter
This is a database operation support library that includes a fast micro O/R mapper (ORM). |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 160 | 8/16/2024 |