We’re building an ASP.NET Core web application. Our database is Couchbase Server 5.0.
There are a few nuget packages available. We’re evaluating “Linq2Couchbase” and “Couchbase.Extensions.Dependencyinjection”.
In the “Linq2Couchbase” examples, the dependency injection is done with the ClusterHelper and all Linq queries are run in the ClusterHelper bucket context. Example:
var context = new BucketContext(ClusterHelper.GetBucket("travel-sample"));
var query = (from a in context.Query<AirLine>()
where a.Country == "United Kingdom"
In “Couchbase.Extensions.DependencyInjection” (or CEDI) you’re injecting a bucket where you are supposed to use string queries. Example:
var bucket = _bucketProvider.GetBucket();
var result =
"SELECT Extent.* FROM `travel-sample` AS Extent WHERE type = 'airline' ORDER BY name");
Are these modules/extensions supposed to be used together? If so - how? What is the recommended approach?
These modules can definitely be used together. The DependencyInjection module allows you to use the Couchbase .NET SDK with the ASP.NET Dependency Injection. The Linq2Couchbase package allows you to create a BucketContext with a Bucket object in order to execute Linq queries. Depending on your needs, you can use Bucket and write queries with a string or use BucketContext and write queries with Linq.
Thank you for your response.
Still, I don’t really understand why I would use both of them; if I use the CEDI nuget, my bucket will be injected in my controller/services. Seems to me that if I want to use Linq queries, then I need to create a BucketContext, even though I have a reference to the bucket already injected. Which would be a rather unnecessary thing to do when I can query by strings.
Is this assumption correct? Please clarify this further…
If you prefer writing Linq, you’ll need the BucketContext. I’m not going to comment on which is “better” because that’s a debate that people have been fighting since the early days of Linq
However, if you want to do something OTHER than N1QL queries (for instance, key/value access, full text search, etc), then you’ll still need the Bucket object.
There is a discussion about this on the Linq2Couchbase project about whether or not to expose Bucket FROM the BucketContext, instead of having both available separately: https://github.com/couchbaselabs/Linq2Couchbase/pull/132