Izayoi.Data.Repository
1.0.0
dotnet add package Izayoi.Data.Repository --version 1.0.0
NuGet\Install-Package Izayoi.Data.Repository -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Izayoi.Data.Repository" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Izayoi.Data.Repository --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Izayoi.Data.Repository, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Izayoi.Data.Repository as a Cake Addin #addin nuget:?package=Izayoi.Data.Repository&version=1.0.0 // Install Izayoi.Data.Repository as a Cake Tool #tool nuget:?package=Izayoi.Data.Repository&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Izayoi.Data.Repository
This is a database operation repository infrastructure with CRUD functionality.
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. It is used for update or delete methods.
Repository Class
using Izayoi.Data;
using Izayoi.Data.Repository;
using Izayoi.Data.Query;
public class UserRepository : DbRepositoryBase<User, int> // <MapClass, KeyDataType>
{
public UserRepository(IDbDataMapper dbDataMapper, QueryOption queryOption)
: base(dbDataMapper, queryOption) { }
public UserRepository(IDbCommandAdapter dbCommandAdapter)
: base(dbCommandAdapter) { }
}
Example Class
using System.Collections.Generic;
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 Example
{
private readonly string dbConnectionString;
private readonly DbCommandAdapter dbCommandAdapter;
private readonly DbDataMapper dbDataMapper;
private readonly QueryOption queryOption;
private readonly UserRepository userRepository;
public Example()
{
queryOption = new QueryOption(RdbKind.SqlServer);
dbDataMapper = new DbDataMapper();
dbCommandAdapter = new DbCommandAdapter(dbDataMapper, queryOption);
userRepository = new UserRepository(dbCommandAdapter);
}
public async Task Method1(CancellationToken cancellationToken)
{
using SqlConnection dbConnection = new(dbConnectionString);
dbConnection.Open();
List<User> users = await userRepository.FetchAllAsync(dbConnection, cancellationToken);
User? user = await userRepository.FetchAsync(dbConnection, id: 1, cancellationToken);
dbConnection.Close();
}
public async Task Method2(CancellationToken cancellationToken)
{
using SqlConnection dbConnection = new(dbConnectionString);
dbConnection.Open();
var user = new User()
{
Id = 0,
Name = "name1",
Age = 20,
Gender = GenderType.Male,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
};
int affectedRowCount;
affectedRowCount = await userRepository.InsertReturnAsync(dbConnection, user, cancellationToken);
user.Age = 21;
user.UpdateAt = DateTime.UtcNow;
affectedRowCount = await userRepository.UpdateAsync(dbConnection, user, cancellationToken);
affectedRowCount = await userRepository.DeleteAsync(dbConnection, user, cancellationToken);
dbConnection.Close();
}
}
Last updated: 22 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Izayoi.Data.DbCommandAdapter (>= 1.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 138 | 8/22/2024 |