Couchbase Live Europe 2015: .NET Data Access Strategies for Couchbase and Language Integrated Query

Preview:

Citation preview

.NET Data Access Strategies for Couchbase and Language Integrated QueryUsing Couchbase with .NET

Martin Esmann Developer Advocate

twitter: martinesmannmartin@couchbase.com

2

Who’s to blame?

3

Goal!

Learn how to access data in Couchbase Server with the .NET SDK

With the main focus on N1QL

I’m assuming you know a bit about N1QL already

4

Agenda

• Couchbase – Quick Overview

• Couchbase .NET SDK 2.X

• Basic Data Access Operations

• Views (…)

• N1QL – SQL like query for Couchbase

• Linq2Couchbase

• Best practices – Application Architecture and Abstractions

• What’s coming next (async, await, observable, reactive)

5

Disclaimer

Details presented in this presentation may change based on customer feedback and other factors by the time the final version of the product is released.

Couchbase – Quick Overview

6

7

Couchbase – Quick Overview

A document store and key-value store

Lightning fast: built-in caching

Query with map-reduce and N1QL

Scales massively

Key benefit: it’s simple and fast.

Challenges (before N1QL):

Data query requires more work

8

Views vs N1QL

Views are sill very relevant an in many cases the recommended approach to data query.

Compared to Views, N1QL is even more flexible in terms of search capability, but with this flexibility there is a potential tradeoff for performance (automatic indexes).

In short, Views definitely have there place and so does N1QL it just depends on you specific needs.

Current demo setup

9

10

Developer Environment Setup - Overview

Application ServerCouchbase Cluster

(3 nodes, 8GB, 4 cores)

Demo PC with Visual Studio(Code runs from here)

Windows Azure Cloud

Virtual Network(private)

11

Demo Setup - Overview

Couchbase Node(1 node, 8GB, 4 cores)

Demo PC with Visual Studio(Code runs from here)

Windows Azure Cloud

N1QL DP 4(Separate download)

Virtual Network(private)

Couchbase .NET SDK

12

13

.NET SDK overview

Main purpose of any SDK is to make life easier for developers!

• Cluster

• Bucket

• ClientConfiguration

• ClusterHelper*

14

Cluster Object

• Maintains references to all open buckets

• Should be a singleton (in most applications)

• Maintains the current state of the Couchbase Cluster or Server

• Publishes config updates

• Factory for: ClusterManager

15

Bucket class

• “Super API” for all other APIs

• Memcached (K/V)

• Views

• N1QL

• Subscriber for config changes

• Serialization/Deserialization

• Transcoding

16

ClientConfiguration Class

• Configures bootstrapping, connection pooling and general client behavior

• Maps (“adapts”) to config file

• Allows for programmatic configuration of client

• Contains useful defaults - e.g. http:localhost:8091

• Bucket level overrides Cluster level configuration

17

ClusterHelper

• Makes it “easy” to use the Cluster/Bucket

• Cluster is a singleton

• Buckets are multitons

• It will make your life easier, use it

19

The IResult Interface

• As a rule the Bucket class doesn’t “throw” exceptions

• IResult is the primary interface for return values of all APIs:

• IViewResult

• IOperationResult and IentityResult

• IQueryResult

• The interface defines

• Message

• Exception

• Success

• Other info defined in more specialized interfaces

21

.NET SDK Query options

• Key/value

• Views (we will not cover views in this talk)

• N1QL (pronounced nickel)

• SQL like query language for Couchbase

• “SQL for Documents”

DemoQuerying data with the .NET SDK and N1QL

Linq2CouchbaseThe best from N1QL and LINQ

23

24

LINQ support for N1QL

Linq2Couchbase is a LINQ provider that wraps a subset of the available N1QL queries to LINQ

• An extension of the Couchbase .NET SDK 2.0

• Open Source Apache 2.0 License (Pull requests kindly accepted!)

• Github url: https://github.com/couchbaselabs/Linq2Couchbase

• Currently supports a minimal subset of the N1QL language (this will soon change)

• Errors are un-handled

• Results are mapped to POCOs

DemoN1QL with LINQ

Best practices – Application Architecture and Abstractions

Software design principles, maintainability and testable code strategies.

“craftsmanship”

26

27

Design decisions

• N1QL in the UI layer?

• Queries in the UI Layer?

• Hard vs. Loose coupling to Couchbase SDK?

• Testability?

• Maintainability?

• Separation of concerns…

28

The Repository Pattern

“Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.”

- Martin Fowler

• Creates an abstraction between BL and DAL

• Makes it possible to write persistence store, agnostic apps

• Enabler for TDD

• In general, provides a common language and structure

accessing data

• If you don’t like the Repository, try DTO/DAO pattern or some

other data access pattern…or not!

DemoRepository pattern

29

Couchbase .NET SDK version NEXTKnowledge about tomorrow can greatly help us plan

today!

30

31

The Ubiquitous Repository

“Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.”

- Martin Fowler

• Creates an abstraction between BL and DAL

• Makes it possible to write persistence store, agnostic apps

• Enabler for TDD

• In general, provides a common language and structure

accessing data

• If you don’t like the Repository, try DTO/DAO pattern or some

other data access pattern…or not!

32

.NET SDK - Version Next

• async/await support

• Observable support

- Excepted ETA is April

• LINQ to N1QL support in SDK

- High priority, but no specific release date yet.

Summary

33

35

Key take-aways

• Couchbase supports advanced data query with N1QL

• LINQ support in the workings

• async/await + Observable support just around the corner!

• N1QL will be part of Couchbase Installer for GA (Sherlock).

Contributions are highly appreciated especially around

LINQ support for N1QL!

Forums

forums.couchbase.com

36

Questions?

37

Thanks for listening!Martin Esmann

Developer Advocate @ CouchbaseTwitter: martinesmann

martin@couchbase.com

38

Recommended