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 url
s, 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 url
s 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