Skip to content

Instantly share code, notes, and snippets.

@danielwertheim
Created April 13, 2013 12:14
Show Gist options
  • Select an option

  • Save danielwertheim/5378155 to your computer and use it in GitHub Desktop.

Select an option

Save danielwertheim/5378155 to your computer and use it in GitHub Desktop.
Early draft of my CouchDb client
using System;
using System.Collections.Generic;
using MyCouch;
namespace CouchDb
{
public class Program
{
static void Main(string[] args)
{
using (var db = new DbClient("http://127.0.0.1:5984/test"))
{
UsingNonTypedApi(db);
UsingEntityApi(db);
}
Console.ReadKey();
}
private static void UsingNonTypedApi(IDbClient db)
{
Console.WriteLine("***** ***** USING NON TYPED API ***** *****");
var post1 = db.Documents.PostAsync(SampleData.Doc1).Result;
Console.WriteLine(post1);
var post2 = db.Documents.Post(SampleData.Doc2);
Console.WriteLine(post2);
var get1 = db.Documents.GetAsync(post1.Id).Result;
Console.WriteLine(get1);
var get2 = db.Documents.Get(post2.Id);
Console.WriteLine(get2);
var kv1 = db.Serializer.Deserialize<IDictionary<string, dynamic>>(get1.Content);
kv1["year"] = 2000;
var docUpd1 = db.Serializer.Serialize(kv1);
var put1 = db.Documents.PutAsync(get1.Id, docUpd1).Result;
var kv2 = db.Serializer.Deserialize<IDictionary<string, dynamic>>(get2.Content);
kv2["year"] = 2001;
var docUpd2 = db.Serializer.Serialize(kv2);
var put2 = db.Documents.Put(get2.Id, docUpd2);
Console.WriteLine(db.Documents.DeleteAsync(put1.Id, put1.Rev).Result);
Console.WriteLine(db.Documents.Delete(put2.Id, put2.Rev));
}
private static void UsingEntityApi(IDbClient db)
{
Console.WriteLine("***** ***** USING Entity API ***** *****");
var post3 = db.Documents.PostAsync(SampleData.Doc3).Result;
Console.WriteLine(post3);
var post4 = db.Documents.Post(SampleData.Doc4);
Console.WriteLine(post4);
var get3 = db.Documents.GetAsync<Artist>(post3.Id).Result;
Console.WriteLine(get3);
var get4 = db.Documents.Get<Artist>(post4.Id);
Console.WriteLine(get4);
get3.Entity.Albums = new List<Album>(get3.Entity.Albums) { new Album { Name = "Test" } }.ToArray();
var put3 = db.Documents.PutAsync(get3.Entity).Result;
get4.Entity.Albums = new List<Album>(get4.Entity.Albums) { new Album { Name = "Test" } }.ToArray();
var put4 = db.Documents.Put(get4.Entity);
Console.WriteLine(db.Documents.DeleteAsync(put3.Entity).Result);
Console.WriteLine(db.Documents.Delete(put4.Entity));
}
}
public class Artist
{
//Could be _id, ArtistId, EntityId, Id (you can change this convention)
public string ArtistId { get; set; }
//Could be _rev, ArtistRev, EntityRev, Rev (you can change this convention)
public string ArtistRev { get; set; }
public string Name { get; set; }
public Album[] Albums { get; set; }
}
public class Album
{
public string Name { get; set; }
}
public static class SampleData
{
public const string Doc1 = "{\"_id\": \"1\", \"name\": \"Fake artist 1\", \"albums\":[{\"name\": \"Greatest fakes #1\"}]}";
public const string Doc2 = "{\"_id\": \"2\", \"name\": \"Fake artist 2\", \"albums\":[{\"name\": \"Greatest fakes #2\"}]}";
public static readonly Artist Doc3 = new Artist
{
ArtistId = "3",
Name = "Fake artist 3",
Albums = new[]
{
new Album { Name = "Greatest fakes #3" }
}
};
public static readonly Artist Doc4 = new Artist
{
ArtistId = "4",
Name = "Fake artist 4",
Albums = new[]
{
new Album { Name = "Greatest fakes #4" }
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment