Couchbase.Extensions.DependencyInjection 2.0.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Couchbase.Extensions.DependencyInjection --version 2.0.0
NuGet\Install-Package Couchbase.Extensions.DependencyInjection -Version 2.0.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="Couchbase.Extensions.DependencyInjection" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Couchbase.Extensions.DependencyInjection --version 2.0.0
#r "nuget: Couchbase.Extensions.DependencyInjection, 2.0.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.
// Install Couchbase.Extensions.DependencyInjection as a Cake Addin
#addin nuget:?package=Couchbase.Extensions.DependencyInjection&version=2.0.0

// Install Couchbase.Extensions.DependencyInjection as a Cake Tool
#tool nuget:?package=Couchbase.Extensions.DependencyInjection&version=2.0.0

Couchbase.Extensions.DependencyInjection

A .Net Core style dependency injection framework for a Couchbase cluster and buckets. It simplifies cluster configuration, lifetime management, and bucket injection.

Getting Started

Assuming you have an installation of Couchbase Server and Visual Studio (examples with VSCODE forthcoming), do the following:

  • Create a .NET Core Web Application using Visual Studio or VsCodeor CIL
  • Install the package from NuGet or build from source and add reference

Adding Couchbase To Services

The easiest option to to provide the Couchbase configuration as an IConfiguration section in your Startup class.

public IConfigurationRoot Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
    // Register Couchbase with configuration section
    services.AddCouchbase(Configuration.GetSection("Couchbase"));

    // Register other services, like .AddMvc()
}

Alternatively, you can apply manual configuration using an action.

public void ConfigureServices(IServiceCollection services)
{
    // Register Couchbase with configuration section
    services.AddCouchbase(clientDefinition =>
    {
        // Set clientDefinition properties here
    });
}

Injecting Couchbase Buckets

To get a couchbase bucket, simply inject IBucketProvider and call GetBucket. Be sure that you don't dispose the IBucket, it's a singleton that will be reused through the application.

public class HomeController : Controller
{
    private readonly IBucketProvider _bucketProvider;

    public HomeController(IBucketProvider bucketProvider)
    {
        _bucketProvider = bucketProvider;
    }

    public IActionResult Index()
    {        
        var bucket = _bucketProvider.GetBucket("bucketname");

        var result =
            await bucket.QueryAsync<Model>(
                "SELECT Extent.* FROM `bucketname` AS Extent");

        if (!result.Success)
        {
            throw new Exception("Couchbase Error", result.Exception);
        }

        return View(result.Rows);
    }
}

Simplifying Injecting Bucket Names

To further simplify dependency injection, you can setup to inject specific buckets. First, create an interface for each bucket that inherits from INamedBucketProvider. This interface must be public and left empty.

public interface IMyBucketProvider : INamedBucketProvider
{
}

You can then configure your bucket interfaces during IServiceCollection setup.

public IConfigurationRoot Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
    // Register Couchbase with configuration section
    services
        .AddCouchbase(Configuration.GetSection("Couchbase"))
        .AddCouchbaseBucket<IMyBucketProvider>("my-bucket");

    // Register other services, like .AddMvc()
}

The interface you created can now be injected into controllers or business logic, and the GetBucket method will return the specified bucket. You are no longer required to know the name of the bucket in the controller, improving separation of concerns in your application.

public class HomeController : Controller
{
    private readonly IMyBucketProvider _bucketProvider;

    public HomeController(IMyBucketProvider bucketProvider)
    {
        _bucketProvider = bucketProvider;
    }

    public IActionResult Index()
    {        
        var bucket = _bucketProvider.GetBucket();

        var result =
            await bucket.QueryAsync<Model>(
                "SELECT Extent.* FROM `bucketname` AS Extent");

        if (!result.Success)
        {
            throw new Exception("Couchbase Error", result.Exception);
        }

        return View(result.Rows);
    }
}

Shutdown

During application shutdown it's best to close the Couchbase connections gracefully. You can do this using the ICouchbaseLifetimeService. For Asp.Net Core, you can call this service from the ApplicationStopped cancellation token of IApplicationLifetime.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
            IApplicationLifetime applicationLifetime)
{
	// Other application startup here

	// When application is stopped gracefully shutdown Couchbase connections
        applicationLifetime.ApplicationStopped.Register(() =>
        {
            app.ApplicationServices.GetRequiredService<ICouchbaseLifetimeService>().Close();
        });
}
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. 
.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 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.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on Couchbase.Extensions.DependencyInjection:

Package Downloads
Couchbase.Extensions.Caching The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

A custom ASP.NET Core Middleware plugin for distributed cache using Couchbase server as the backing store. Supports both Memcached (in-memory) and Couchbase (persistent) buckets.

Couchbase.Extensions.Locks The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

A system for managing distributed mutexs backed by Couchbase. This can prevent multiple simultaneous processes in separate application instances, which is useful for microservices or other horizontally scaled architectures. Supports Couchbase SDK 3.0.

OpenStore.Infrastructure.Data.NoSql.Couchbase

Package Description

Claudis.Libraries.CouchbaseExtensions

Couchbase repository extensions

OpenStore.Data.NoSql.Couchbase

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.5.0 942 3/11/2024
3.4.15 6,068 2/9/2024
3.4.14 9,153 1/17/2024
3.4.14-rc3 333 12/8/2023
3.4.14-rc2 112 12/7/2023
3.4.14-rc1 124 12/7/2023
3.4.13 47,243 11/9/2023
3.4.12 25,984 10/4/2023
3.4.11 22,510 9/12/2023
3.4.10 33,877 8/4/2023
3.4.9 2,135 7/21/2023
3.4.8 16,445 6/13/2023
3.4.7 1,100 6/7/2023
3.4.6 36,654 5/12/2023
3.4.5 38,559 4/20/2023
3.4.4 14,706 3/10/2023
3.4.3 12,087 2/9/2023
3.4.2 11,199 1/13/2023
3.4.1 23,895 12/9/2022
3.4.0 65,069 11/9/2022
3.3.6 29,176 10/6/2022
3.3.5 58,414 9/17/2022
3.3.4 21,932 8/2/2022
3.3.3 7,067 7/11/2022
3.3.2 14,847 6/30/2022
3.3.0 22,858 4/27/2022
3.2.9 5,443 4/7/2022
3.2.8 19,521 3/2/2022
3.2.7 9,513 2/4/2022
3.2.5 203,649 12/14/2021
3.2.0 72,730 7/29/2021
3.1.6 22,104 5/24/2021
3.1.5 955 5/20/2021
3.0.5.931 125,605 9/29/2020
3.0.4.811 144,100 8/11/2020
3.0.1 3,097 5/22/2020
3.0.0 873 5/22/2020
2.0.2 549,865 5/9/2018
2.0.1 9,638 3/1/2018
2.0.0 5,689 11/8/2017
1.0.2 13,247 8/7/2017
1.0.0 114,726 2/15/2017

Supports .NET Core 2.0