Use CouchDB with .NET

前端 未结 14 2413
渐次进展
渐次进展 2020-12-12 13:46

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

14条回答
  •  渐次进展
    2020-12-12 14:22

    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 users;
    var sts = new List { "ACTIVE", "LOCKED" };
    using (UserRepository db = new UserRepository())
    {
        var query = db.FindOf("list-status", new { id = "OwnerIdloop.user.7", statuses = sts });
        users = db.List(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); // 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("email@email.com");
        user.Name = user.Name + "::CHANGED";
    
        var result = db.Update(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("email@email.com");
    
        var result = db.Delete(user); // delete document from database. Return true case sucess or false case not deleted
        Console.WriteLine($"Sucesso: {result}");
    }
    

    Hope this helps!

提交回复
热议问题