40
.NET DATA ACCESS STRATEGIES FOR COUCHBASE AND LANGUAGE INTEGRATED QUERY Jeff Morris, Couchbase

NET Data Access Strategies for Couchbase and Language Integrated Query: Couchbase Connect 2015

Embed Size (px)

Citation preview

.NET DATA ACCESS STRATEGIES FOR COUCHBASE AND LANGUAGE INTEGRATED QUERYJeff Morris, Couchbase

©2015 Couchbase Inc. 2

Goal

Learn how to access data in Couchbase Server with the .NET SDK Main focus on N1QL (SQL for Documents)

There are lot’s of other sessions that go into more detail about N1QL!

©2015 Couchbase Inc. 3

Agenda

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)

©2015 Couchbase Inc. 4

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 .NET SDK

©2015 Couchbase Inc. 6

.NET SDK overview

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

Cluster

Bucket

ClientConfiguration

ClusterHelper*

©2015 Couchbase Inc. 7

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

©2015 Couchbase Inc. 8

Bucket Class

“Super API” for all other APIs Memcached (K/V) Views N1QL

Subscriber for config changes Serialization/Deserialization Transcoding

©2015 Couchbase Inc. 9

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

©2015 Couchbase Inc. 10

ClusterHelper

Makes it “easy” to use the Cluster/Bucket Cluster is a singleton Buckets are multitons It will make your life easier, use it

©2015 Couchbase Inc. 11

ClusterHelper: Example

var config = new ClientConfiguration(); ClusterHelper.Initialize(config);

var bucket = ClusterHelper.GetBucket("default");

//use bucket ClusterHelper.RemoveBucket("default"); ClusterHelper.Close();

©2015 Couchbase Inc. 12

Diagram

©2015 Couchbase Inc. 13

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

©2015 Couchbase Inc. 14

IResult Hiearchy

©2015 Couchbase Inc. 15

.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”

©2015 Couchbase Inc. 16

Asynchronous Data Access

TAP (Task Asynchronous Pattern) is support is now available for: Key/Value operations Views N1QL Queries

©2015 Couchbase Inc. 17

Async/await:

DemoQuerying data with the .NET SDK and N1QL

Linq2CouchbaseThe best from N1QL and LINQ

©2015 Couchbase Inc. 20

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

©2015 Couchbase Inc. 21

Linq: simple query

var query = from r in bucket.Queryable<Route>() select r;

SELECT r.* FROM `travel-sample` as r

©2015 Couchbase Inc. 22

Linq: Where and Limit

SELECT DISTINCT r.* FROM `travel-sample` as r WHERE (r.type = 'route') LIMIT 10

var query = (from r in bucket.Queryable<Route>()where r.Type == "route"

select r).Take(10)

.Distinct();

foreach (var route in query){ ... }

©2015 Couchbase Inc. 23

Linq Provider: Current Support

N1qL Keyword Linq Equiv Keyword

SELECT Select

FROM from

WHERE where

ORDER BY orderby

LIMIT Take()

OFFSET Skip()

DISTINCT Distinct()

EXPLAIN* Explain()

©2015 Couchbase Inc. 24

Linq Provider: Future Support

N1qL Keyword Linq Equiv Keyword

JOIN join

NEST Nest()*

UNEST Unnest()*

HAVING having

GROUP BY groupby

SATISFIES Satisfies()*

EVERY Every()*

WITHIN Within

Others …

DemoN1QL with LINQ

Best practices – Application Architecture and

AbstractionsSoftware design principles, maintainability and

testable code strategies.

“craftsmanship”

©2015 Couchbase Inc. 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…

©2015 Couchbase Inc. 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!

©2015 Couchbase Inc. 29

Repository Kickstarter

IRepository and Repository<T> IEntity and EntityBase Mapping from JSON => POJO https://github.com/couchbaselabs/couchbase-

net-data

©2015 Couchbase Inc. 30

Using the SDK in ASP.NET (in Global.asax)

protected void Application_Start() {

AreaRegistration.RegisterAllAreas();FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);RouteConfig.RegisterRoutes(RouteTable.Routes);BundleConfig.RegisterBundles(BundleTable.Bundles);

ClusterHelper.Initialize();}

protected void Application_End(){

ClusterHelper.Close();}

DemoRepository Pattern

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

plan today!

©2015 Couchbase Inc. 33

NET SDK - Version Next

Reactive Extensions for .NET? LINQ to N1QL support in SDK - High priority, but no specific release date

yet.

Summery

©2015 Couchbase Inc. 36

Key take-aways

Couchbase supports advanced data query with N1QL

LINQ support in the workings async/await now supported!

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

Contributions are highly appreciated especially around LINQ support for N1QL!

Need help!forums.couchbase.com

Questions?

Thanks for listening!Jeffry Morris

Software Engineer@ CouchbaseTwitter: jeffrysmorris

[email protected]

Thank you.