Couchbase Mobile Webinar: Building Apps for Couchbase Mobile .NET

Download Couchbase Mobile Webinar: Building Apps for Couchbase Mobile .NET

Post on 20-Aug-2015

404 views

Category:

Software

0 download

TRANSCRIPT

  1. 1. Building .NET Apps using Couchbase Lite!Zack Gramana
  2. 2. About MeZack GramanaSenior Software EngineerMobile@zgramana
  3. 3. And, about meWayne CarterArchitectMobile@waynecarter
  4. 4. 4CouchbaseLiteOn-device,lightweight,na1veembeddedJSONdatabaseSyncGatewaySynchronizeon-deviceCouchbaseLitewithCouchbaseServerinthecloudCouchbaseServerHighperformance,scalable,always-onJSONdatabaseinthecloudCouchbase Mobile
  5. 5. 5Couchbase LiteCouchbaseLiteFull FeaturedJSONNativeLightweightSecureBeta
  6. 6. 6Sync GatewaySyncGatewayReplicationAuthenticationData PartitioningData Access Control
  7. 7. Authentication, Partitioning, Access ControlPluggable AuthenticationJavaScript sync function runs on all mutations.7Data Partitioning channel(): Routes the document to the named channelData Access Control Read! access(): Grants access to a channel to a specified user, list of users, or a role role(): Grants a user a role Write! throw(): Prevents a document mutation from persisting requireUser/Role/Access(): Validates the user, their role assignments, or their accessprivileges.
  8. 8. 8Couchbase ServerCouchbaseServerJSONHighly ScalableHigh PerformanceAlways On
  9. 9. Couchbase Lite In A Nutshell
  10. 10. Highlights Document Database Map Reduce Queries Sync Small: Assembly is < 500KB
  11. 11. Document Database Overview Semi-structured Key-value pairs Schemaless JSON-backed Very low-friction
  12. 12. Map Reduce Queries Overview Functional queries, instead of set-theoretic Write query logic in pure C#, not SQL Can be one-off, or auto-updating as data changes You probably already know how to use it: Map is like LINQs Select method Reduce is like LINQs Aggregate method
  13. 13. Sync Overview Master-master replication Uses HTTP + REST Offline mode for free Manual or continuous modes Automatic retry on failure Responds to network changes
  14. 14. Programming Model You: make your changes to docs Queries: will reflect those changes Replicators: sync changes for you
  15. 15. Getting Started with Documents
  16. 16. Opening a Databasevar db = Manager.SharedInstance.GetDatabase("foo");"Debug.Assert(db != null);"var db = Manager.SharedInstance.GetExistingDatabase("foo");"Debug.Assert(db == null);"
  17. 17. Getting a Documentvar doc = db.GetDocument("foo-doc");"Debug.Assert(doc != null);""var doc = db.GetExistingDocument("foo-doc");"Debug.Assert(doc == null);
  18. 18. Creating a Documentvar doc = db.CreateDocument();"Debug.Assert(doc.Id != null);"
  19. 19. Deleting a Documentdoc.Delete();"Debug.Assert(doc.Deleted);"
  20. 20. Updating a New Documentvar rev = doc.CreateRevision();"var props = new Dictionary"{"{ "foo", "bar"},"{ "fizz", "buzz"}"};"rev.SetUserProperties(props);"rev.Save();
  21. 21. Updating an Existing Documentvar props = doc.Properties;"var newProps = new Dictionary(props)"{"{ "foo", "bar"}"};"doc.PutProperties(newProps); // Saves a new revision
  22. 22. Dealing with Changesdoc.Change += (sender, e) => "{"// e.g. if we get a conflict...!if (e.Change.IsConflict)"{"// we can resolve it!!}"};
  23. 23. DocumentChange MembersString"Boolean"Boolean"String"Uri"RevisionInternal""DocumentId"IsConflict"IsCurrentRevision"RevisionId"SourceUrl"WinningRevision"
  24. 24. Handling Conflictsvar current = doc.CurrentRevision;"foreach(var rev in doc.ConflictingRevisions)"{"var newRev = rev.CreateRevision();"if (rev.Equals(current)) {"newRev.SetProperties(mergedProps);"} else {"newRev.IsDeletion = true;"}"newRev.Save();"}
  25. 25. Auto-merging Properties using LINQDictionary mergedProps;"try"{"mergedProps = doc.ConflictingRevisions".SelectMany(rev => rev.UserProperties)".Distinct()".ToDictionary("kvp => kvp.Key, "kvp => kvp.Value");"} catch (ArgumentException ex) {"// Merge conflict requires manual resolution.!}
  26. 26. Getting Started with Queries
  27. 27. Create a Viewvar view = db.GetView("foo");"Debug.Assert(view != null);""var view = db.GetExistingView("foo");"Debug.Assert(view == null);"
  28. 28. Add a Map Functionview.SetMap((doc, emit) => "{"var foo = (String)doc["foo"];"if (foo.Equals("bar")) {"emit ("text", doc["text"]);"}"}, "1");"
  29. 29. Using a Reduce FunctionReduceDelegate reducer = (keys, values, rereduce) => "{"var i = 0; var wordCount = 0;"foreach(var key in keys) {"if (key == "text") {"var str = (String)values[i];"wordCount += str.Split(' ').Length;"}"}"return wordCount;"};"view.SetMapReduce(mapper, reducer, "1");"
  30. 30. Create a Queryvar query = view.CreateQuery();""// Options, like..."query.Completed += (sender, e) => {"Log("Results: {0}", e.Rows);"};"query.Descending = true;""// Get records..."var rows = await query.RunAsync();"rows.Where(row => ...);
  31. 31. Create a LiveQueryvar liveQuery = query.ToLiveQuery();""// Respond to changes in the view."liveQuery.Changed += (sender, e) =>"{"Log("Updates: {0}", e.Rows.Count);"};
  32. 32. Getting Started with Replication
  33. 33. Pull Replicatorvar uri = new Uri(syncGatewayUrl);"var pull = db.CreatePullReplication(uri);"pull.Continuous = true;"pull.Changed += ReplicationChanged;"pull.Start();
  34. 34. Push Replicatorvar uri = new Uri(syncGatewayUrl);"var push = db.CreatePushReplication(uri);"push.Continuous = true;"push.Changed += ReplicationChanged;"push.Start();
  35. 35. Next Steps
  36. 36. Couchbase Developer Portal!developer.couchbase.com/mobile
  37. 37. Xamarin Component Store!components.xamarin.com/view/couchbase-lite-net
  38. 38. .:[coming soon]:.!nuget.org
  39. 39. Couchbase Connect
  40. 40. Questions and Answers
  41. 41. Additional Resources
  42. 42. Mailing List!groups.google.com/d/forum/mobile-couchbase
  43. 43. Contact Me!zack@couchbase.com (email)@zgramana (twitter)

Recommended

View more >