Skip to content

ByteTerrace/ByteTerrace.Ouroboros.Database

Repository files navigation

Basic Usage

var options = DbClientOptions.New(
    connectionString: "Server=(localdb)\\MSSQLLocalDb;Initial Catalog=master;",
    providerFactory: SqlClientFactory.Instance // Depends on Microsoft.Data.SqlClient package.
);
var client = DbClient.New(options: options);

_ = client
    .ToIDbClient()
    .ExecuteStoredProcedure(
        name: "sp_tables",
        schemaName: "dbo"
    );

ASP.NET Core Integration

appsettings.json (or equivalent)

{
    "ConnectionStrings": {
        "LocalDb": {
            "type": "Microsoft.Data.SqlClient.SqlClientFactory, Microsoft.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5",
            "value": "Server=(localdb)\\MSSQLLocalDb;Initial Catalog=master;"
        }
    },
    "Logging": {
        "LogLevel": {
            "ByteTerrace.Ouroboros.Database": "None" // Set to Trace for debugging output.
        }
    }
}

Program.cs

using ByteTerrace.Ouroboros.Database;

var builder = WebApplication.CreateBuilder(args: args);

builder
    .Services
    .AddDbClients(configuration: builder.Configuration);

ExampleController.cs

using ByteTerrace.Ouroboros.Database;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace MyNamespace
{
    [Route("api/[controller]")]
    [ApiController]
    public class ExampleController : ControllerBase
    {
        public IDbClientFactory<DbClient> DbClientFactory { get; }

        public ExampleController(IDbClientFactory<DbClient> dbClientFactory) {
            DbClientFactory = dbClientFactory;
        }

        [AllowAnonymous]
        [HttpGet]
        [Route("test-db-connection")]
        public async ValueTask<IActionResult> TestDbConnection(CancellationToken cancellationToken) {
            using var client = DbClientFactory.NewClient(name: "MyConnection");

            await client.OpenConnectionAsync(cancellationToken: cancellationToken);

            return Ok();
        }
    }
}

Links

NuGet References

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages