Currently you can find out that a view doesn’t exists when you try to perform a query after attempting to retrieve the view:
var result = _couchClient.GetView<etc>("DocumentName", "ViewNameThatDoesNotExist");
result.Count(); // This returns a service exception
You find out it doesn’t exist after Count throws an exception, so at the moment I use a try/catch to determine if there is an exception.
Is there a more elegant solution that doesn’t require as many resources as performing a query?
There doesn’t seem to be anything like
result.exists()
or something similar.
Matt – thanks for sharing your extension method! Having seen this question pop up a couple of times now, I’m baking support into the 1.2.1 client (to be released on Feb 05, 2013). The answer that follows isn’t valid in 1.2.0, but is a preview for future reference. I’m also including it as an answer instead of a comment for better code highlighting…
In 1.2.1, there will be two ways to check for a view not existing. The first is to use the view’s new CheckExists() method. This method will query the design document and parse the JSON. Similar to Matt’s approach, but doesn’t use the CouchbaseCluster API.
The second approach is to catch the exception, which is meaningfully typed (no longer an InvalidOperationException). The advantage of this approach is one fewer HTTP query.
The Jira request is at http://www.couchbase.com/issues/browse/NCBC-165 and the code is in review at http://review.couchbase.org/#/c/24068/1.