CloudyWing.DatabaseFacade 0.2.0

dotnet add package CloudyWing.DatabaseFacade --version 0.2.0
                    
NuGet\Install-Package CloudyWing.DatabaseFacade -Version 0.2.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="CloudyWing.DatabaseFacade" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CloudyWing.DatabaseFacade" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="CloudyWing.DatabaseFacade" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CloudyWing.DatabaseFacade --version 0.2.0
                    
#r "nuget: CloudyWing.DatabaseFacade, 0.2.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.
#:package CloudyWing.DatabaseFacade@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CloudyWing.DatabaseFacade&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=CloudyWing.DatabaseFacade&version=0.2.0
                    
Install as a Cake Tool

DatabaseFacade

License: MIT Build and Deploy Documentation Publish to NuGet

DatabaseFacade 是用來簡化 ADO.NET 操作流程的資料庫工具,本身不具有操作資料庫的能力,需搭配對應的資料庫 Library 使用。

寫這個套件不是要讓大家使用,只是因為某個念想,想把過往寫得Respository DatabaseClients 完善,但後面發現命名不太適合,所以就重新開一個新的。

目前操作 ADO.NET 已經有一個很強大的套件 Dapper,如果不使用 Entity Framework 的話,我是建議使用 Dapper,DatabaseFacade 能在程式碼能給有需要的人做為參考就足夠了。

支援版本

  • net10.0
  • netstandard2.0
  • net45

使用教學

首先先在 NuGet 安裝你要使用的資料庫 Client,例如:「System.Data.SqlClient」、「Microsoft.Data.SqlClient」,然後在應用程式啟用時,設定 FacadeConfiguration,至少要設定對應的 DbFactory 和連線字串。

FacadeConfiguration.SetConfiguration(SqlClientFactory.Instance, "{資料庫連線字串}");

實際使用(以 SQL Server 語法為例)

using CommandExecutor executor = new CommandExecutor();
executor.CommandText = "SELECT * FROM Table WHERE Id IN @Id";
executor.Parameters.Add("Id", new int[] { 1, 2, 3 });
DataTable dt = executor.CreateDataTable();

非同步 API

如果是在 ASP.NET Core、BackgroundService,或其他本來就以 async / await 為主的流程中使用,也可以直接呼叫非同步版本 API:

  • CreateDataReaderAsync
  • CreateDataTableAsync
  • QueryScalarAsync
  • ExecuteAsync

所有非同步方法都保留原本的同步 API,並支援 CancellationToken

using CommandExecutor executor = new CommandExecutor();
executor.CommandText = "SELECT * FROM Table WHERE Id IN @Id";
executor.Parameters.Add("Id", new int[] { 1, 2, 3 });
DataTable dt = await executor.CreateDataTableAsync(cancellationToken);

如果需要逐筆讀取資料,可以直接使用 CreateDataReaderAsync 回傳的 DbDataReader

using CommandExecutor executor = new CommandExecutor();
executor.CommandText = "SELECT Id, Name FROM Table WHERE Id IN @Id";
executor.Parameters.Add("Id", new int[] { 1, 2, 3 });

using DbDataReader reader = await executor.CreateDataReaderAsync(cancellationToken: cancellationToken);
while (await reader.ReadAsync(cancellationToken)) {
    Console.WriteLine($"{reader["Id"]}, {reader["Name"]}");
}

如果搭配 Transaction 使用,作法與同步版本相同,只是把執行資料庫作業的 API 換成 Async 版本即可。

FacadeConfiguration可設定項目

屬性 型別 用途
DefaultDbProviderFactory DbProviderFactory 用來建立 ADO.NET 相關物件
DefaultConnectionString string 資料庫連線字串
DefaultCommandTimeout int Command 的 Timeout 時間,預設為 30 秒
DefaultKeepConnection bool 執行完 SQL 語法後,是否要關閉 SqlConnection
DefaultIsolationLevel IsolationLevel Transaction 的層級鎖,預設為 ReadCommitted
ParameterNamePrefix string 參數型別為 IEnumerable 時,產生的 ParameterName Prefix,預設「CloudyWing」
OnCommandCreating Action<ParameterCollection, string?> 可在建立 DbCommand 前加入自訂程式碼
OnCommandCreated Action<IDbCommand> 可在建立 DbCommand 後加入自訂程式碼

Documentation

完整文檔請參考:https://cloudywing.github.io/DatabaseFacade/

或查看以下主要文章:

License

This project is MIT licensed.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net10.0

    • No dependencies.

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
0.2.0 131 3/27/2026
0.1.0 887 6/19/2023
0.0.0 358 10/7/2022