Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Setting up CouchbaseClient to be used with IoC

0.00/5 (No votes)
29 Nov 2013 1  
Injecting servers, buckets and passwords into CouchbaseClient

Introduction

Couchbase is a fairly new NoSql, Key/Value storage platform that is built to extend MemCached by adding persistence, amongst other changes. My company has recently adopted Couchbase, and I was writing a new API service around it, and ran into a problem injecting the server urls, the bucket name, and the bucket password into the CouchbaseClient object.

I didn't want to use the built in Couchbase config sections because our build/deploy system doesn't deal with specialized sections in the web.config section very well, and personal preference. I would rather do as much of the initialization in code as possible.

This tutorial assumes you have knowledge of CouchbaseClient, and Inversion of Control/Dependency Injection strategies.

Using the Code

The code presented here is step by step instructions on preparing and using a class to inject a CouchbaseClient instance.

I also have created a CouchbaseUri class that takes a url of a Couchbase server, and formats it so that CouchbaseClient will work.

public sealed class CouchbaseUri : Uri
{
    private const string BaseUrl = "http://{0}:8091/pools";

    public CouchbaseUri(string server)
        : base(string.Format(BaseUrl, server))
    {
    }
}

First, create an interface that inherits ICouchbaseClient. There is no need to add any methods or properties to it. Doing it this way will allow for the instance of the class created later in the tutorial to be injected as required.

public interface IMyCouchbaseClient : ICouchbaseClient
{
}

Next, create a class that extends CouchbaseClient, and also inherits the interface that was created above. In this class, add a static method CreateConfiguration which takes the couchbaseServers, bucket, and bucketPassword and creates an ICouchbaseClientConfiguration instance. The couchbaseServers string is actually a comma delimited list of base urls for each server in the Couchbase cluster.

public class MyCouchbaseClient : CouchbaseClient, IMyCouchbaseClient 
{
    private static ICouchbaseClientConfiguration CreateConfiguration(string couchbaseServers,
                                                                     string bucket,
                                                                     string bucketPassword)
    {
        var configuration = new CouchbaseClientConfiguration
                                {
                                    Bucket = bucket,
                                    BucketPassword = bucketPassword,
                                };

        couchbaseServers.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries)
                        .Select(url => new CouchbaseUri(url)).ToList()
                        .ForEach(couchbaseUrl => configuration.Urls.Add(couchbaseUrl));

        return configuration;
    }

    public MyCouchbaseClient (string couchbaseServers,
                              string bucket,
                              string bucketPassword)
        : base(CreateConfiguration(couchbaseServers, bucket, bucketPassword))
    {
    }
}

I have used this method with Castle.Windsor, and created extensions that actually find the servers, and bucket information in the .config file, but that is a little too messy to put here. Ninject is a little easier to demonstrate.

public sealed class ServicesModule : NinjectModule
{
    public override void Load()
    {
        Bind<IMyCouchbaseClient>().ToMethod(x =>
            {
                return new MyCouchbaseClient(
                    ConfigurationManager.AppSettings["couchbaseServers"],
                    ConfigurationManager.AppSettings["bucket"]
                    ConfigurationManager.AppSettings["bucketPassword"]);
            }).InSingletonScope();
    }
} 

Couchbase client is now ready to be injected into classes as required.

public sealed class MyRepository
{
    private readonly IMyCouchbaseClient _dataSource;

    public MyRepository(IMyCouchbaseClient dataSource)
    {
        if (dataSource == null) throw new ArgumentNullException("dataSource");

        _dataSource = dataSource;
    }
} 

What this class does is create a way of differentiating between CouchbaseClient instances. By making classes like this, it is now possible to have multiple CouchbaseClients in your application, pointing to different buckets.

History

  • Nov 29, 2013 - Initial submission

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here