36
.NET Data Access Strategies for Couchbase and Language Integrated Query Using Couchbase with .NET Martin Esmann Developer Advocate twitter: martinesmann [email protected]

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

Embed Size (px)

Citation preview

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

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

Martin Esmann Developer Advocate

twitter: [email protected]

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

2

Who’s to blame?

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

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

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

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)

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

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.

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

Couchbase – Quick Overview

6

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

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

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

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.

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

Current demo setup

9

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

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)

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

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)

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

Couchbase .NET SDK

12

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

13

.NET SDK overview

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

• Cluster

• Bucket

• ClientConfiguration

• ClusterHelper*

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

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

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

15

Bucket class

• “Super API” for all other APIs

• Memcached (K/V)

• Views

• N1QL

• Subscriber for config changes

• Serialization/Deserialization

• Transcoding

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

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

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

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

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

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

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

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”

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

DemoQuerying data with the .NET SDK and N1QL

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

Linq2CouchbaseThe best from N1QL and LINQ

23

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

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

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

DemoN1QL with LINQ

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

Best practices – Application Architecture and Abstractions

Software design principles, maintainability and testable code strategies.

“craftsmanship”

26

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

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…

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

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!

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

DemoRepository pattern

29

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

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

today!

30

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

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!

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

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.

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

Summary

33

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

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!

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

Forums

forums.couchbase.com

36

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

Questions?

37

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

Thanks for listening!Martin Esmann

Developer Advocate @ CouchbaseTwitter: martinesmann

[email protected]

38