我开发了.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");
}