Updating Couchbase client from old version

Yeah, the 2.0 SDK includes a lot of major changes compared to 1.0. I very recently completed the upgrade for one of my projects.

Here are the key points that can you you started.

  1. The format of the .config file has changed, so you’ll have to rearrange it. The definitions for server URLs are now separate from the definition for buckets. More information: http://developer.couchbase.com/documentation/server/4.0/sdks/dotnet-2.1/configuring-the-client.html

  2. Configuration is not automatically loaded, you’ll need to initialize with configuration during application startup. Do this by calling ClusterHelper.Initialize. The simplest use case is normally ClusterHelper.Initialize(“couchbase”), where “couchbase” agrees with your configuration section name.

  3. CouchbaseClient is no longer a class, you’ll want to use the Couchbase.Core.IBucket interfaces instead. Just call ClusterHelper.GetBucket(“bucketName”) to get an IBucket interface for the bucket you want. Be sure that you don’t call Dispose on this when you’re done, it’s being shared between all uses. Also, you don’t need to do instance management anymore, ClusterHelper is making sure that there is only one instance per application for you.

  4. To get a document, use IBucket.Get(T) or IBucket.GetDocument(T). Note that these do not directly return the document, but instead return it wrapped inside a result object. You can then check the Status to see if the request was successful, and for things like missing keys.

  5. The equivalent of Store with StoreMode.Set is IBucket.Upsert(T). StoreMode.Replace is IBucket.Replace(T), and StoreMode.Add is IBucket.Insert(T).

  6. Note that these methods are really meant for JSON documents. If you need backwards compatibility with other document types you might have to mess with creating a custom ITypeSerializer. The default still uses Newtonsoft.Json. However, I noticed what seemed like some differences with I was reading basic literals like strings that we’re not JSON documents. They were in cache only, so I just flushed the cache, so I’m not 100% certain what the differences might have been.

  7. Also note that IBucket has Async versions of all the read/write methods. You can use this to help improve application performance if you’re using the async/await model for your application.

I hope this helps you get started. Let me know if you have any more specific questions.

Brant

2 Likes