代码之家  ›  专栏  ›  技术社区  ›  Phil Sandler

CouchDB的C库?

  •  4
  • Phil Sandler  · 技术社区  · 15 年前

    我已经有一年多没有看到有人问/回答这个问题了,我相信这段时间发生了很多变化。

    如果您在.Net/C环境(生产环境)中使用CouchDB,我想知道您使用的是哪个库,以及您使用它的经验。

    这些图书馆还很新/不成熟吗?它们是否如此简单以至于不需要文档?使用CouchDB的.Net开发人员太少了,以至于没有人在这个领域谈论它吗?

    谢谢你的真知灼见。

    2 回复  |  直到 15 年前
        1
  •  3
  •   alexpopescu    15 年前

    所有CouchDB功能都是通过httpapi公开的,所以您实际上只需要一个好的HTTP库和一些错误代码处理。我想这就是为什么你找不到很多CouchDB库的原因:协议/API非常简单,你可以马上开始。

        2
  •  0
  •   JAGJ jdfoxito    9 年前

    class Program
    {
        static void Main(string[] args)
        {
            var sDireccion = @"http://localhost:5984/base_principal/_all_docs";
            var client = new WebClient { Credentials = new NetworkCredential("zzz", "zzz + zzz"), Encoding = Encoding.UTF8 };
            var sRespuesta = client.DownloadString(sDireccion);
            cClaseBase cBase = new cClaseBase();
            cBase = JsonConvert.DeserializeObject<cClaseBase>(sRespuesta);
            foreach (Row str in cBase.rows)
            {
                sDireccion = @"http://localhost:5984/base_principal/"+str.id;
                sRespuesta = client.DownloadString(sDireccion);
                cClaseDetalle cd = new cClaseDetalle();
                cd = JsonConvert.DeserializeObject<cClaseDetalle>(sRespuesta);
            }
        }
    
        3
  •  0
  •   Wedson Quintanilha da Silva    5 年前

    我开发了.Net和Java。在.Net中,我真的没有找到像我在Java中用来访问couchDb的东西。在java中,我使用了一个名为jkniv的库,它更像是一个轻量级ORM,允许我将同一个域映射到关系DBMS、NoSQL和Cassandra,只更改存储在一个或多个XML文件中的查询文件,并动态解析内容。

    我在.net中找了一些类似的东西,方便了couchDb的使用,但我没有找到,它们都是你提到的,下载并编译你的。。。我不太喜欢这个。

    所以,对于个人申请项目, 最后我从jkniv和 图书馆 我自己 这简化了couchDb的使用,而不必直接在代码中处理http请求。它封装了对HTTP请求的处理,甚至允许我分离couchDb的“find”命令来生成C代码,从而使代码更清晰、更易于理解和维护。

    啊,这个库已经通过Nuget编译和安装了。

    下面是一些如何举例的例子。有关如何配置和使用它的详细说明,请参见 Github .

    Nuget Package | Github | Example of use

    表示文档的类的示例。必须从AbstractDocument继承:

    public enum DocType
    {
        USER,
        GROUP
    }
    
    public enum Status
    {
        PRE_ACTIVE, ACTIVE, INACTIVE, LOCKED, PRE_CANCEL, CANCEL
    }
    
    /// <summary>
    /// The objects that represent a document must inherit from AbstractDocument 
    /// and set the generic to the type of the object itself. With this, this 
    /// object must not contain the "_id" and "_rev" properties since the inherited 
    /// class contains these implementations and the services related to these 
    /// two attributes.
    /// Only the methods mapped with [JsonProperty] attribute will be persisted in the 
    /// document as well as read and filled in automatically.
    /// </summary>
    public class User: AbstractDocument<User>
    {
    
        [JsonProperty("sourceId")] //Newtonsoft
        public String SourceId { get; set; }
    
        [JsonProperty("ownerId")] //Newtonsoft
        public String OwnerId { get; set; }
    
        [JsonProperty("name")] //Newtonsoft
        public String Name { get; set; }
    
        [JsonProperty("email")] //Newtonsoft
        public String Email { get; set; }
    
        [JsonProperty("acctId")] //Newtonsoft
        public String AcctId { get; set; }
    
        [JsonProperty("docType")] //Newtonsoft
        public DocType DocType { get; set; }
    
        [JsonProperty("status")] //Newtonsoft
        public Status Status { get; set; }
    
        [JsonProperty("assetIam")] //Newtonsoft
        public String AssetIam { get; set; }
    
        [JsonProperty("serial")] //Newtonsoft
        public String Serial { get; set; }
    
        public override string ToString()
        {
            return $"User data: [SourceID: {SourceId}, OwnerID: {OwnerId}, Name: {Name}, Email: {Email}. AcctId: {AcctId}, docType: {DocType} Type: {TypeDocument}, Status: {Status}, AssetIam: {AssetIam}, Serial: {Serial}]";
        }
    
    }
    

    创建表示数据库的类,扩展CouchRepository类:

    /// <summary>
    /// Create a repository to represent the database informed
    /// in context.Extend the CouchDb Helper repository and tell 
    /// the constructor what context will be used for this created repository.
    /// </summary>
    public class UserRepository: CouchRepository
    {
        public UserRepository() : base("users-db") { } //users-db is context name defined in appsettings.json
    }
    

    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);
    }
    

    更改文档示例:

    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);
    }
    

    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}");
    }
    

    var sts = new List<String> { "ACTIVE", "LOCKED" }; // param filter
    using (UserRepository db = new UserRepository())
    {
        var query = db.FindOf("list-status", new { id = "OwnerIdloop.user.7", statuses = sts, filterStatus = true});
        var users = db.List<User>(query);
    }
    

    通过标识符“list status”在上述代码中使用的xml中编写的find命令是:

        <find id="list-status">
        {
          "selector": {
          "$and": [
              {
              "docType": {"$eq": "USER"},
              "ownerId": {"$eq": :id }
              <if test="filterStatus == true">
                , "status": {"$in": :in:statuses }
              </if>
              }        
           ]
          },
          "fields": [ "_id", "_rev", "ownerId", "sourceId", "docType", "name", "email","status","acctId","assetIam","serial"]
        }
    </find>
    

    获取以下类型的所有文档:

    using (UserRepository db = new UserRepository()){
        /* The document must contain the 'typeDoc' attribute with the same value as the type entered. */
        users = db.GetAllOf<User>(); //Get all doc from type parametized.
    }
    

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