58

Can .NET (managed code) read and write to CouchDB?

I would like to build a part of my project that does document management using CouchDB

Developer
  • 17,809
  • 26
  • 66
  • 92

15 Answers15

22

Its a late answer, but do check out Hammock. It's active and going into production use on several projects soon and is receiving regular updates and fixes. Besides basic object persistence, Hammock gives you:

  • True POCO. You don't even need an 'id' property; Hammock tracks that internally.
  • Robust support for views, including an easy to use fluent API that both generates AND executes views, and support for creating custom map/reduce views.
  • Attachments support.
  • A generic Repository<> class that helps bring your queries/views (i.e. _Design doc) together with your c# application code. Repositories are responsible for maintaining _design docs, and this helps keep CouchDB views from feeling like stored procs.
  • A full unit test suite. This is not prototype software.
Pierre Arnaud
  • 10,212
  • 11
  • 77
  • 108
nicknystrom
  • 749
  • 6
  • 15
18

Take a look at the SharpCouch utility class.

It is a simple wrapper class for the CouchDB HTTP API.

BengtBe
  • 4,465
  • 1
  • 26
  • 19
  • It seems as though SharpCouch uses LitJson for Json tasks instead of rewriting the wheel... – ChrisFletcher Sep 10 '13 at 14:10
  • 2
    Look like that answer is outdated. SharpCouch: _last change Aug 5, 2008_ - possible that code is outdated. To the answer below about Hammock: _last commit March 2011. Project status: archived_. Looks like both projects are dead. There is other one mentioned by @Daniel https://github.com/danielwertheim/mycouch which is quite alive and even has an Aspnet.Identity CoachDB implementation. – angularrocks.com Nov 16 '15 at 04:45
  • Which is best NoSQL DB for Win CE using OS 6.5 and .Net 3.5.*.* LiteDB breaks due to incorrect bind to System.Core (Linq and other stuff) Any Ideas, Urgent. SQLite has concurrency issue big time. – Dean Van Greunen Dec 14 '16 at 08:34
  • 2
    CouchDB is accessed via HTTP. better to find an HTTP request library rather than a db-handler per say. Such a wrapper is so thin as to be not necessary, stops you learning CouchDB, and makes it hard to use Couch's API to it's full extent – Zach Smith Jan 03 '17 at 12:19
11

Little late to the game but there's an open source project for a new driver written in C# over the async HTTP Client and by default Newtonsoft's JSON.Net for serialization (switchable). MyCouch - Simple async CouchDb client for .Net.

A Getting started post exists here: http://danielwertheim.se/get-up-and-running-with-couchdb-and-c-using-mycouch-on-windows/

Daniel
  • 8,133
  • 5
  • 36
  • 51
9

Given that you generally communicate with CouchDB over REST and JSON, I'd imagine it wouldn't be too hard to use JSON.NET and the normal WebClient/HttpWebRequest classes to do it. I haven't tried it myself, mind you...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Commenting a little off topic here - how has your experience been with JSON.NET? Doesn't .net 3.5 sp1 have methods to serialize data to JSON? – Developer Jun 26 '09 at 17:07
  • I haven't used the built-in serializer, and I've only used JSON.NET a little bit - but the latter has worked fine with the StackOverflow JSON. – Jon Skeet Jun 26 '09 at 17:16
  • 1
    JSON.NET's LINQ support is very handy if you want to parse JSON without deserializing it into a fixed C# class - it's fairly similar to using LINQ-to-XML. – Joel Mueller Jun 26 '09 at 17:47
9

Yes.

See here for instructions on installing CouchDB on windows.

Here is a link to the getting started with C# wiki article. There is a link to a .Net wrapper in that article. It would be pretty easy to roll your own as well.

ScottS
  • 8,455
  • 3
  • 30
  • 50
  • 1
    I want to do a little update here: I installed couchDb on my windwos laptop, and it was very easy. Just to go couchbase site, download and install. You will be playing with futon in notime – NicoGranelli Jul 11 '11 at 02:55
  • @NicoGranelli, thanks for the update. Couchdb on windows has come a long way in the last two years. – ScottS Jul 11 '11 at 16:20
  • Found this question in a search, used the link from the wiki article and I ended up using [LoveSeat](https://github.com/soitgoes/LoveSeat) – Chris Jan 12 '12 at 03:46
6

Keep an eye on Ottoman an open-source project written in C#. It is still in heavy development, but I wanted to point it out as an option for projects in the planning stages. I will update this post as it gets closer to completion. You can view the source on github. Read the README to see examples and what is possible with the current version.

  • Unlike SharpCouch, which uses strings heavily, Ottoman uses generics for automatic serialization and desrialization for mapping an object from a JSON string and vice versa. All this without your objects needing to inherit off an interface or base class.
  • It uses Json.Net underneath to handle the JSON serialization and deserialization.
  • Maps the RESTFul error codes that CouchDB returns into Exceptions.
  • Planned Feature: Id generators for generating Id's for the objects being persisted
  • Planned Feature: Implicit Offline Optimistic Lock via Document Revisions, Ottoman will use an Identity Map to keep track for you and will throw an exception when a conflict occurs.
  • Planned Feature: It will be Mono compatible.
  • Planned Feature: This is down the road, but I do plan on having LINQ expressions for Map/Reduce functions which you express to CouchDB using JavaScript.

It is a fairly new project, but very active. Of course, I'm a bit biased also. ;-)

Matt
  • 41,216
  • 30
  • 109
  • 147
Dale Ragan
  • 18,202
  • 3
  • 54
  • 70
5

also take a look at divan - a light-weight wrapper for the cdb api

kolosy
  • 3,029
  • 3
  • 29
  • 48
  • 1
    I need to store POCO in couchDB. I tested Divan, DreamSeat, LoveSeat, Relax. I had problems with those except Divan. I am on VS 2010 + .NET 4. – PokerIncome.com Oct 04 '11 at 00:58
3

I have used JSON.NET in conjunction with the MS REST starter kit's http client class to integrate to CouchDB and it works really well.

Dokie
  • 304
  • 3
  • 9
3

CouchDB's wiki has a list of API for C# (with their features) if you want the latest status : http://wiki.apache.org/couchdb/Getting_started_with_C%23

Peter
  • 56
  • 2
2

The options (ordered by latest update):

Daniel
  • 8,133
  • 5
  • 36
  • 51
JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
2

Very easy to use .NET API for CouchDB included into WDK10 that you can grab from http://code.google.com/p/skitsanoswdk/ it is very flexible and allows you to deal with your data on objects and pure json level.

2

I know I am VERY late, but maybe it's helpful for the future. In the last few years, I worked on a library for CouchDB.

The biggest feature is converting LINQ (EF Core like) to mango queries, but also DB Changes Feed, Views, and Table Splitting.

https://github.com/matteobortolazzo/couchdb-net

var skywalkers = await context.Rebels
    .Where(r => 
        r.Surname == "Skywalker" && 
        (
            r.Battles.All(b => b.Planet == "Naboo") ||
            r.Battles.Any(b => b.Planet == "Death Star")
        )
    )
    .OrderByDescending(r => r.Name)
    .ThenByDescending(r => r.Age)
    .Take(2)
    .Select(
        r => r.Name,
        r => r.Age
    })
    .ToListAsync();
1

One more - CouchDBClient: https://github.com/ttutisani/CouchDBClient

Disclaimer: I'm the author.

I tried to make CouchDBClient more friendly to work with custom objects - that's what usually applications store in the database.

Back when I started writing this framework, all others that I found and tried were making an impression of non-standardized method signatures (named using various conventions and harder to guess what they did), not unified interfaces (some operations around string docs, while others around json docs), not very flexible (some worked only with strings, others only with objects), or not very abstract (had to know CouchDB REST API first to use them correctly). I'm assuming and hoping they have matured since then, but at least this one is designed up to my taste.

I'm hoping that CouchDBClient can create a feeling of comfortable abstraction without worrying about underlying API too much, and just focusing on your application, and how it stores and retrieves objects it needs.

Tengiz
  • 8,011
  • 30
  • 39
0

Check out Relax http://www.sharplearningcurve.com/wiki/Symbiote-Relax.ashx. Relax is a ".Net API abstraction of CouchDB's (excellent) RESTful API. It includes a repository-based interface for document interaction and a server controller for administrative type tasks."

Daniel Mohl
  • 116
  • 3
0

I recommend the CouchDb.Repository.Helper package. It is comprehensive and allows you to create your queries in XML files with parse of dynamic parameters according to values ​​of variables or properties of objects.

I had the same need and after evaluating the options available, to meet the requirements of my application, I created these components that helped me a lot and maybe they can help you and also others. I make it clear that I have no intention of promoting myself here, just sharing something that may be useful.

The detailed explanation of how to configure and use it is on github.

Link: Nuget Package | Github

Example of use for retrieving documents with mango-querie:

IList<User> users;
var sts = new List<String> { "ACTIVE", "LOCKED" };
using (UserRepository db = new UserRepository())
{
    var query = db.FindOf("list-status", new { id = "OwnerIdloop.user.7", statuses = sts });
    users = db.List<User>(query);
}
Array.ForEach(users.ToArray(), Console.WriteLine);

Example of adding documents:

User user = createUser("email@email.com");
using (UserRepository db = new UserRepository())
{
    var result = db.Insert<User>(user); // add document and return instance changed with operation revision id
    Console.WriteLine(result.Revision);
}

Example of changing documents:

using (UserRepository db = new UserRepository())
{
    // Load document data by ID
    var user = db.Get<User>("email@email.com");
    user.Name = user.Name + "::CHANGED";

    var result = db.Update<User>(user); // update document and return instance changed with operation revision id
    Console.WriteLine(result.Revision);
}

Example of deleting documents:

using (UserRepository db = new UserRepository())
{
    // Load document data by ID
    var user = db.Get<User>("email@email.com");

    var result = db.Delete<User>(user); // delete document from database. Return true case sucess or false case not deleted
    Console.WriteLine($"Sucesso: {result}");
}

Hope this helps!