iCat.DB.Client.Extension.Web
1.0.2
Extension methods are included in iCat.DB.Client, iCat.DB.Client.Factory.
See the version list below for details.
dotnet add package iCat.DB.Client.Extension.Web --version 1.0.2
NuGet\Install-Package iCat.DB.Client.Extension.Web -Version 1.0.2
<PackageReference Include="iCat.DB.Client.Extension.Web" Version="1.0.2" />
paket add iCat.DB.Client.Extension.Web --version 1.0.2
#r "nuget: iCat.DB.Client.Extension.Web, 1.0.2"
// Install iCat.DB.Client.Extension.Web as a Cake Addin
#addin nuget:?package=iCat.DB.Client.Extension.Web&version=1.0.2
// Install iCat.DB.Client.Extension.Web as a Cake Tool
#tool nuget:?package=iCat.DB.Client.Extension.Web&version=1.0.2
iCat.DB.CLient
iCat.DB.Client is a UnitOfWork design pattern library for db connection. It can manages and serves dynamic provide db clients at runtime.
Description
The library provides two way for registering IUnitOfWork and IConnection. IUnitOfWork and IConnection export the DBConnection property, which can also used in dapper.net.
- General fixed connection registration, registered when the application started, can't be modified.
- Through factory registration, programers can implement "IDBClientProvider" to provide IUnitOfWork/IConnection,
As a reminder, the IUnitOfWork/IConnection obtained from "General Fixed Connection Registration" and "Factory" are different instances.
Installation
dotnet add package iCat.DB.Client
dotnet add package iCat.DB.Client.Extension.Web
dotnet add package iCat.DB.Client.Factory
dotnet add package iCat.DB.Client.MSSQL
dotnet add package iCat.DB.Client.MySQL
Sample ( General Fixed Connection Registration )
Program.cs
using iCat.DB.Client.Extension.Web;
using iCat.DB.Client.Interfaces;
using iCat.DB.Client.Models;
using MSSQL = iCat.DB.Client.MSSQL;
using MySQL = iCat.DB.Client.MSSQL;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var services = builder.Services;
services
.AddDBClient(
new MySQL.DBClient(new DBClientInfo("MainDB", "mysql connection string")),
new MSSQL.DBClient(new DBClientInfo("CompanyA", "mssql connection string A")),
new MSSQL.DBClient(new DBClientInfo("CompanyB", "mssql connection string B"))
);
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseAuthorization();
app.MapControllers();
foreach (var iConnection in app.Services.GetServices<IConnection>())
{
iConnection.ExecutedEvent += (category, command, script) =>
{
// category: MainDB, CompanyA, CompanyB..etc
// command: enum (Opened, Closed, TransactionBegined, Commited, Rollbacked, Executed)
// script: sql statements run by the application
};
}
app.Run();
}
}
Controller
using iCat.DB.Client.Interfaces;
[ApiController]
[Route("[controller]")]
public class DemoController : ControllerBase
{
private readonly IEnumerable<IUnitOfWork> _unitOfWorks;
private readonly IEnumerable<IConnection> _connections;
public DemoController(IEnumerable<IUnitOfWork> unitOfWorks, IEnumerable<IConnection> connections)
{
_unitOfWorks = unitOfWorks ?? throw new ArgumentNullException(nameof(unitOfWorks));
_connections = connections ?? throw new ArgumentNullException(nameof(connections));
}
[HttpGet]
public IActionResult Get()
{
using (var unitOfork = _unitOfWorks.First(p => p.Category == "MainDB"))
{
try
{
unitOfork.Open();
unitOfork.BeginTransaction();
var connection = _connections.First(p => p.Category == "MainDB");
foreach (var dr in connection.ExecuteReader("SELECT * FROM YourTable", new DbParameter[] { })){
// write in your logic
}
unitOfork.Commit();
}
catch (Exception)
{
unitOfork.Rollback();
}
finally
{
unitOfork.Close();
}
}
return Ok();
}
}
Sample ( Through factory registration )
Program.cs
using iCat.DB.Client.Extension.Web;
using iCat.DB.Client.Interfaces;
using iCat.DB.Client.Models;
using iCat.DB.Client.Factory.Interfaces;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var services = builder.Services;
services
.AddDBClientFactory( // DefaultDBClientProvider ( you can custom IDBClientProvider here )
() => new MySQL.DBClient(new DBClientInfo("MainDB", "mysql connection string")),
() => new MSSQL.DBClient(new DBClientInfo("CompanyA", "mssql connection string A")),
() => new MSSQL.DBClient(new DBClientInfo("CompanyB", "mssql connection string B"))
);
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseAuthorization();
app.MapControllers();
foreach (var factory in app.Services.GetServices<IDBClientFactory>())
{
foreach (var connection in factory.GetConnections())
{
connection.ExecutedEvent += (category, command, script) =>
{
// category: MainDB, CompanyA, CompanyB..etc
// command: enum (Opened, Closed, TransactionBegined, Commited, Rollbacked, Executed)
// script: sql statements run by the application
};
}
}
app.Run();
}
}
Controller
using iCat.DB.Client.Factory.Interfaces;
[ApiController]
[Route("[controller]")]
public class DemoController : ControllerBase
{
private readonly IDBClientFactory _clientFactory;
public DemoController(IDBClientFactory clientFactory)
{
_clientFactory = clientFactory ?? throw new ArgumentNullException(nameof(clientFactory));
}
[HttpGet]
public IActionResult Get()
{
using (var unitOfork = _clientFactory.GetUnitOfWork("MainDB"))
{
try
{
unitOfork.Open();
unitOfork.BeginTransaction();
var connection = _clientFactory.GetConnection("MainDB");
foreach (var dr in connection.ExecuteReader("SELECT * FROM YourTable", new DbParameter[] { }))
{
var filed = dr["fieldName"];
};
unitOfork.Commit();
}
catch (Exception)
{
unitOfork.Rollback();
}
finally
{
unitOfork.Close();
}
}
return Ok();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 is compatible. 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 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. |
-
net6.0
- iCat.DB.Client.Factory (>= 1.0.1)
-
net7.0
- iCat.DB.Client.Factory (>= 1.0.1)
-
net8.0
- iCat.DB.Client.Factory (>= 1.0.1)
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.3.5-alpha | 73 | 5/20/2024 | |
1.1.4 | 153 | 1/3/2024 | |
1.0.3 | 101 | 1/2/2024 | |
1.0.2 | 135 | 12/31/2023 |
Version 1.0.2