Using Couchbase Buckets pattern

@CasperSkydt

This is a great question; the Unit Of Work (UOW) pattern is pretty common and basically scopes a resource to Web Request/Response. The Couchbase client does not follow the UOW pattern – it (Cluster and Buckets) should be scoped to the application. You could use the UOW in certain scenarios, but not for high-performance server applications because of the cost of creating and destroying the TCP connections.

The Bucket object itself should probably be a static reference within another abstraction such as a Repository or Data Access Object (DAO) or a simple façade/wrapper. Ideally it would be lazy loaded and injected via DI. You could use an IoC container to manage this if you wanted to.

One thing to note, however, is that the Cluster object should always be scoped at the application level, since it manages the bucket resources when a bucket is opened. Also, the buckets use reference counting to ensure that one thread doesn’t close a bucket while another is using it; once the internal count is 0, the object will be disposed and resources reclaimed.

-Jeff