Couchbase.Extensions.DependencyInjection 2.0.1

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.1                
NuGet\Install-Package Couchbase.Extensions.DependencyInjection -Version 2.0.1                
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.1" />                
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.1                
#r "nuget: Couchbase.Extensions.DependencyInjection, 2.0.1"                
#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.1

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

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 (10)

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

Package Downloads
Couchbase.Extensions.Caching

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

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.6.4 1,690 11/8/2024
3.6.3 9,504 9/19/2024
3.6.2 46,323 7/22/2024
3.6.1 3,783 7/3/2024
3.6.0 2,591 6/14/2024
3.5.5 116 10/11/2024
3.5.3 128 7/22/2024
3.5.2 18,257 5/3/2024
3.5.1 34,026 4/16/2024
3.5.1-rc5 126 4/12/2024
3.5.0 4,606 3/11/2024
3.4.15 21,970 2/9/2024
3.4.14 33,565 1/17/2024
3.4.14-rc3 518 12/8/2023
3.4.14-rc2 139 12/7/2023
3.4.14-rc1 153 12/7/2023
3.4.13 223,273 11/9/2023
3.4.12 40,405 10/4/2023
3.4.11 38,044 9/12/2023
3.4.10 42,126 8/4/2023
3.4.9 2,399 7/21/2023
3.4.8 18,797 6/13/2023
3.4.7 1,138 6/7/2023
3.4.6 48,979 5/12/2023
3.4.5 72,432 4/20/2023
3.4.4 19,230 3/10/2023
3.4.3 17,500 2/9/2023
3.4.2 15,225 1/13/2023
3.4.1 25,455 12/9/2022
3.4.0 99,911 11/9/2022
3.3.6 64,620 10/6/2022
3.3.5 77,102 9/17/2022
3.3.4 26,211 8/2/2022
3.3.3 8,524 7/11/2022
3.3.2 18,539 6/30/2022
3.3.0 26,748 4/27/2022
3.2.9 5,718 4/7/2022
3.2.8 23,258 3/2/2022
3.2.7 12,513 2/4/2022
3.2.5 223,956 12/14/2021
3.2.0 74,409 7/29/2021
3.1.6 22,766 5/24/2021
3.1.5 996 5/20/2021
3.0.5.931 131,681 9/29/2020
3.0.4.811 209,245 8/11/2020
3.0.1 3,149 5/22/2020
3.0.0 908 5/22/2020
2.0.2 595,508 5/9/2018
2.0.1 9,849 3/1/2018
2.0.0 5,861 11/8/2017
1.0.2 13,340 8/7/2017
1.0.0 121,173 2/15/2017