iCat.DB.Client
2.2.2
dotnet add package iCat.DB.Client --version 2.2.2
NuGet\Install-Package iCat.DB.Client -Version 2.2.2
<PackageReference Include="iCat.DB.Client" Version="2.2.2" />
paket add iCat.DB.Client --version 2.2.2
#r "nuget: iCat.DB.Client, 2.2.2"
// Install iCat.DB.Client as a Cake Addin #addin nuget:?package=iCat.DB.Client&version=2.2.2 // Install iCat.DB.Client as a Cake Tool #tool nuget:?package=iCat.DB.Client&version=2.2.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
Sample ( Single database )
Program.cs
using iCat.DB.Client.Extension.Web;
using iCat.DB.Client.Implements;
using iCat.DB.Client.Models;
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((s) => new DBClient(new SqlConnection("Your connection string")));
services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.MapControllers();
app.Run();
}
}
Controller
using iCat.DB.Client.Interfaces;
[ApiController]
[Route("[controller]")]
public class DemoController : ControllerBase
{
private readonly IUnitOfWork _unitOfWork;
private readonly IConnection _connection;
public DemoController(IUnitOfWork unitOfWork, IConnection connection)
{
_unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
_connection = connection ?? throw new ArgumentNullException(nameof(connection));
}
[HttpGet]
public IActionResult Get()
{
using (_unitOfWork)
{
try
{
_unitOfWork.Open();
_unitOfWork.BeginTransaction();
foreach (var dr in _connection.ExecuteReader("SELECT * FROM YourTable", new DbParameter[] { }))
{
var filed = dr["fieldName"];
};
_unitOfWork.Commit();
}
catch (Exception)
{
_unitOfWork.Rollback();
}
finally
{
_unitOfWork.Close();
}
}
return Ok();
}
}
Sample ( General Fixed Connections Registration )
Program.cs
using iCat.DB.Client.Extension.Web;
using iCat.DB.Client.Implements;
using iCat.DB.Client.Models;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var services = builder.Services;
services
.AddDBClients(
(s) => new DBClient(new DBClientInfo("MainDB", new SqlConnection("Your connection string"))),
(s) => new DBClient(new DBClientInfo("Company", new SqlConnection("Your connection string")))
);
services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.MapControllers();
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 unitOfWork = _unitOfWorks.First(p => p.Category == "MainDB"))
{
try
{
unitOfWork.Open();
unitOfWork.BeginTransaction();
var connection = _connections.First(p => p.Category == "MainDB");
foreach (var dr in connection.ExecuteReader("SELECT * FROM YourTable", new DbParameter[] { }))
{
var filed = dr["fieldName"];
};
unitOfWork.Commit();
}
catch (Exception)
{
unitOfWork.Rollback();
}
finally
{
unitOfWork.Close();
}
}
return Ok();
}
}
Sample ( Through factory registration )
Program.cs
using iCat.DB.Client.Extension.Web;
using iCat.DB.Client.Implements;
using iCat.DB.Client.Models;
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(
() => new DBClient(new DBClientInfo("MainDB", new SqlConnection("Your connection string"))),
() => new DBClient(new DBClientInfo("Company", new SqlConnection("Your connection string")))
);
services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.MapControllers();
app.Run();
}
}
Controller
using iCat.DB.Client.Factory.Interfaces;
[ApiController]
[Route("[controller]")]
public class DemoController : ControllerBase
{
private readonly IDBClientFactory _factory;
public DemoController(IDBClientFactory factory)
{
_factory = factory ?? throw new ArgumentNullException(nameof(factory));
}
[HttpGet]
public IActionResult Get()
{
using (var unitOfWork = _factory.GetUnitOfWork("MainDB"))
{
try
{
unitOfWork.Open();
unitOfWork.BeginTransaction();
var connection = _factory.GetConnection("MainDB");
foreach (var dr in connection.ExecuteReader("SELECT * FROM YourTable", new DbParameter[] { }))
{
var filed = dr["fieldName"];
};
unitOfWork.Commit();
}
catch (Exception)
{
unitOfWork.Rollback();
}
finally
{
unitOfWork.Close();
}
}
return Ok();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- System.Data.Common (>= 4.3.0)
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- System.Data.Common (>= 4.3.0)
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- System.Data.Common (>= 4.3.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- System.Data.Common (>= 4.3.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on iCat.DB.Client:
Package | Downloads |
---|---|
iCat.DB.Client.Factory
Factory for iCat.DB.Client |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version 2.2.2