Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

1,456 views

Published on

Whether you have been using Couchbase with .NET for a couple of years or you are a .NET developer who is new to Couchbase, you will benefit from this session on strategies for data access with the current 2.1 and upcoming 2.2 .NET SDKs. Martin and Jeff will cover the API and interfaces provided by the SDK, including a couple of different strategies and patterns for mapping and querying data. The session will show in demos and code the mapping to POCOs from the repository pattern, how to use asynchronous operations, and even how LINQ fits in with current and future SDKs.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

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

  1. 1. .NET DATA ACCESS STRATEGIES FOR COUCHBASE AND LANGUAGE INTEGRATED QUERY Jeff Morris, Couchbase
  2. 2. ©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!
  3. 3. ©2015 Couchbase Inc. 3 Agenda  Couchbase .NET SDK 2.X  Basic DataAccess Operations  Views (…)  N1QL – SQL like query for Couchbase  Linq2Couchbase  Best practices – Application Architecture and Abstractions  What’s coming next (async, await, observable, reactive)
  4. 4. ©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.
  5. 5. Couchbase .NET SDK
  6. 6. ©2015 Couchbase Inc. 6 .NET SDK overview Main purpose of any SDK is to make life easier for developers!  Cluster  Bucket  ClientConfiguration  ClusterHelper*
  7. 7. ©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
  8. 8. ©2015 Couchbase Inc. 8 Bucket Class  “Super API” for all other APIs  Memcached (K/V)  Views  N1QL  Subscriber for config changes  Serialization/Deserialization  Transcoding
  9. 9. ©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
  10. 10. ©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
  11. 11. ©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();
  12. 12. ©2015 Couchbase Inc. 12 Diagram
  13. 13. ©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
  14. 14. ©2015 Couchbase Inc. 14 IResult Hiearchy
  15. 15. ©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”
  16. 16. ©2015 Couchbase Inc. 16 Asynchronous Data Access  TAP (Task Asynchronous Pattern) is support is now available for:  Key/Value operations  Views  N1QL Queries
  17. 17. ©2015 Couchbase Inc. 17 Async/await:
  18. 18. Demo Querying data with the .NET SDK and N1QL
  19. 19. Linq2Couchbase The best from N1QL and LINQ
  20. 20. ©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
  21. 21. ©2015 Couchbase Inc. 21 Linq: simple query var query = from r in bucket.Queryable<Route>() select r; SELECT r.* FROM `travel-sample` as r
  22. 22. ©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){ ... }
  23. 23. ©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()
  24. 24. ©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 …
  25. 25. Demo N1QL with LINQ
  26. 26. Best practices – Application Architecture and Abstractions Software design principles, maintainability and testable code strategies. “craftsmanship”
  27. 27. ©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…
  28. 28. ©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 forTDD  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!
  29. 29. ©2015 Couchbase Inc. 29 Repository Kickstarter  IRepository and Repository<T>  IEntity and EntityBase  Mapping from JSON => POJO  https://github.com/couchbaselabs/couchbase-net-data
  30. 30. ©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(); }
  31. 31. Demo Repository Pattern
  32. 32. Couchbase .NET SDK version NEXT Knowledge about tomorrow can greatly help us plan today!
  33. 33. ©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.
  34. 34. Summery
  35. 35. ©2015 Couchbase Inc. 35 Resources  .NET SDK https://github.com/couchbase/couchbase-net-client  Linq2Couchbase https://github.com/couchbaselabs/Linq2Couchbase  N1QL installer http://www.couchbase.com/nosql-databases/downloads  Azure Setup guide http://blog.couchbase.com/hosting-couchbase-server-in- windows-azure-part-1
  36. 36. ©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!
  37. 37. Need help! forums.couchbase.com
  38. 38. Questions?
  39. 39. Thanks for listening! Jeffry Morris Software Engineer@Couchbase Twitter: jeffrysmorris Jeffry.morris@couchbase.com
  40. 40. Thank you.

×