iCat.DB.Client.Factory 2.0.2

dotnet add package iCat.DB.Client.Factory --version 2.0.2
NuGet\Install-Package iCat.DB.Client.Factory -Version 2.0.2
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="iCat.DB.Client.Factory" Version="2.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add iCat.DB.Client.Factory --version 2.0.2
#r "nuget: iCat.DB.Client.Factory, 2.0.2"
#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 iCat.DB.Client.Factory as a Cake Addin
#addin nuget:?package=iCat.DB.Client.Factory&version=2.0.2

// Install iCat.DB.Client.Factory as a Cake Tool
#tool nuget:?package=iCat.DB.Client.Factory&version=2.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.

  1. General fixed connection registration, registered when the application started, can't be modified.
  2. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on iCat.DB.Client.Factory:

Package Downloads
iCat.DB.Client.Extension.Web

Web project extension for iCat.DB.Client

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.2 343 1/20/2024
2.0.1 391 1/3/2024
2.0.0 331 1/2/2024
1.0.1 426 12/31/2023

Version 2.0.1