Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#CouchDB的库?_C#_.net_Couchdb - Fatal编程技术网

C#CouchDB的库?

C#CouchDB的库?,c#,.net,couchdb,C#,.net,Couchdb,我已经一年多没有看到被问/被回答的问题了,我相信这段时间发生了很多变化 如果您在.Net/C#环境(生产环境)中使用CouchDB,我想知道您使用的是哪个库,以及您使用它的经验 我发现至少有四个图书馆:吊床、放松、沙发和沙发。然而,当我用谷歌搜索他们的名字+“CouchDB”时,我在教程、博客帖子、推荐信、文档等方面几乎找不到什么。它们似乎都还没有二进制版本(都是“拉源代码并构建”) 这些库是否仍然相当新/不成熟?它们是否如此简单以至于不需要真正的文档?使用CouchDB的.Net开发人员太少了

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

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

我发现至少有四个图书馆:吊床、放松、沙发和沙发。然而,当我用谷歌搜索他们的名字+“CouchDB”时,我在教程、博客帖子、推荐信、文档等方面几乎找不到什么。它们似乎都还没有二进制版本(都是“拉源代码并构建”)

这些库是否仍然相当新/不成熟?它们是否如此简单以至于不需要真正的文档?使用CouchDB的.Net开发人员太少了,以至于没有人在这个领域谈论它吗


感谢您的帮助。

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

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);
        }
    }
类程序
{
静态void Main(字符串[]参数)
{
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=新的cClaseBase();
cBase=JsonConvert.DeserializeObject(sRespuesta);
foreach(cBase.rows中的行str)
{
sDireccion=@”http://localhost:5984/base_principal/“+str.id;
sRespuesta=client.DownloadString(sDireccion);
cClaseDetalle cd=新的cClaseDetalle();
cd=JsonConvert.DeserializeObject(sRespuesta);
}
}

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

我在.net中寻找了一些类似的东西,这些东西有助于couchDb的使用,但我没有找到,它们都是你提到的,下载并编译你的…这让我很不高兴

因此,对于一个个人应用程序项目,我从jkniv获得灵感,自己创建了一个库,它简化了couchDb的使用,而不必直接在代码中处理http请求。它封装了http请求的处理,甚至允许我将couchDb的“查找”分开命令使我的C#代码更干净,更易于理解和维护

此外,我放置这些命令的XML文件包含基于属性值的动态语法​​在这里,我可以根据逻辑测试决定是否向命令添加代码段。例如:如果“Person”对象的“isfemal”属性等于true,则向将发送给couchDb的“find”命令添加一个mango query代码段

此外,这些命令允许您使用基于参数对象属性的文本分析。例如:命令{“选择器”:{“名称”:{“$regex”::过滤器}}将使用传递参数的此属性值替换参数“:过滤器”

优点是这个库只是一个促进者,“find”和“view”命令使用couchDb的本地语法,因此您可以在couchDb本身上构建查询,测试它,如果它是您想要的方式,只需在XML文件中填写它,给出一个id并在C#代码中引用这个id

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

下面是一些如何对其进行示例的示例。有关如何配置和使用它的详细说明,请参阅

链接:| |

表示文档的类的示例。必须从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}]";
    }

}
公共枚举DocType
{
用户,
团体
}
公共枚举状态
{
PRE_ACTIVE,ACTIVE,INACTIVE,LOCKED,PRE_CANCEL,CANCEL
}
/// 
///表示文档的对象必须继承自AbstractDocument
///并将泛型设置为对象本身的类型
///对象不能包含“\u id”和“\u rev”属性,因为继承的
///类包含这些实现以及与这些实现相关的服务
///两个属性。
///只有使用[JsonProperty]属性映射的方法才会在
///文档以及自动读取和填写。
/// 
公共类用户:AbstractDocument
{
[JsonProperty(“sourceId”)]//Newtonsoft
公共字符串SourceId{get;set;}
[JsonProperty(“ownerId”)]//Newtonsoft
公共字符串所有者ID{get;set;}
[JsonProperty(“name”)]//Newtonsoft
公共字符串名称{get;set;}
[JsonProperty(“电子邮件”)]//Newtonsoft
公共字符串电子邮件{get;set;}
[JsonProperty(“acctId”)]//Newtonsoft
公共字符串AcctId{get;set;}
[JsonProperty(“docType”)]//Newtonsoft
公共DocType DocType{get;set;}
[JsonProperty(“status”)]//Newtonsoft
公共状态状态{get;set;}
[JsonProperty(“assetIam”)]//Newtonsoft
公共字符串AssetIam{get;set;}
[JsonProperty(“serial”)]//Newtonsoft
公共字符串序列{get;set;}
公共重写字符串ToString()
{
return$“用户数据:[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);
}
    <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");
}