|
@@ -41,8 +41,8 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
private DocumentClient CosmosClient { get; set; }
|
|
|
private DocumentCollection CosmosCollection { get; set; }
|
|
|
|
|
|
- private string _Database { get; set; }
|
|
|
- private int _CollectionThroughput { get; set; }
|
|
|
+ private string Database { get; set; }
|
|
|
+ private int CollectionThroughput { get; set; }
|
|
|
public AzureCosmosDBRepository(AzureCosmosDBOptions options)
|
|
|
{
|
|
|
try
|
|
@@ -65,9 +65,9 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
{
|
|
|
throw new BizException("请设置正确的AzureCosmosDB数据库配置信息!");
|
|
|
}
|
|
|
- _Database = options.Database;
|
|
|
- _CollectionThroughput = options.CollectionThroughput;
|
|
|
- CosmosClient.CreateDatabaseIfNotExistsAsync(new Database { Id = _Database });
|
|
|
+ Database = options.Database;
|
|
|
+ CollectionThroughput = options.CollectionThroughput;
|
|
|
+ CosmosClient.CreateDatabaseIfNotExistsAsync(new Database { Id = Database });
|
|
|
// _connectionString = options.ConnectionString;
|
|
|
}
|
|
|
catch (DocumentClientException de)
|
|
@@ -102,9 +102,9 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
collectionDefinition.PartitionKey.Paths.Add("/" + partitionKey);
|
|
|
|
|
|
}
|
|
|
- // CosmosCollection = await this.CosmosClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(_Database), collectionDefinition);
|
|
|
+ // CosmosCollection = await this.CosmosClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(Database), collectionDefinition);
|
|
|
CosmosCollection = await this.CosmosClient.CreateDocumentCollectionIfNotExistsAsync(
|
|
|
- UriFactory.CreateDatabaseUri(_Database), collectionDefinition, new RequestOptions { OfferThroughput = _CollectionThroughput }
|
|
|
+ UriFactory.CreateDatabaseUri(Database), collectionDefinition, new RequestOptions { OfferThroughput = CollectionThroughput }
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -151,7 +151,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
Type t = typeof(T);
|
|
|
DocumentCollection documentCollection = await InitializeCollection<T>();
|
|
|
ResourceResponse<Document> doc =
|
|
|
- await CosmosClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_Database, t.Name), entity);
|
|
|
+ await CosmosClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(Database, t.Name), entity);
|
|
|
//Console.WriteLine(doc.ActivityId);
|
|
|
|
|
|
|
|
@@ -163,7 +163,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
Type t = typeof(T);
|
|
|
await InitializeCollection<T>();
|
|
|
ResourceResponse<Document> doc =
|
|
|
- await CosmosClient.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(_Database, t.Name), entity);
|
|
|
+ await CosmosClient.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(Database, t.Name), entity);
|
|
|
return entity;
|
|
|
}
|
|
|
public async Task<string> ReplaceObject<T>(T entity, string key)
|
|
@@ -173,7 +173,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
try
|
|
|
{
|
|
|
ResourceResponse<Document> doc =
|
|
|
- await CosmosClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(_Database, t.Name, key), entity);
|
|
|
+ await CosmosClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, key), entity);
|
|
|
return key;
|
|
|
}
|
|
|
catch (Exception e)
|
|
@@ -192,7 +192,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
try
|
|
|
{
|
|
|
ResourceResponse<Document> doc =
|
|
|
- await CosmosClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(_Database, t.Name, key),
|
|
|
+ await CosmosClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, key),
|
|
|
entity,
|
|
|
new RequestOptions { PartitionKey = new PartitionKey(partitionKey) });
|
|
|
return key;
|
|
@@ -215,7 +215,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
//await InitializeCollection<T>();
|
|
|
//查询条数 -1是全部
|
|
|
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = open };
|
|
|
- var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(_Database, t.Name), queryOptions).AsDocumentQuery();
|
|
|
+ var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), queryOptions).AsDocumentQuery();
|
|
|
while (query.HasMoreResults)
|
|
|
{
|
|
|
foreach (T obj in await query.ExecuteNextAsync())
|
|
@@ -224,7 +224,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
}
|
|
|
}
|
|
|
return objs;
|
|
|
- //return CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(_Database, t.Name),sql);
|
|
|
+ //return CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name),sql);
|
|
|
|
|
|
}
|
|
|
|
|
@@ -236,11 +236,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
await InitializeCollection<T>();
|
|
|
//查询条数 -1是全部
|
|
|
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1 };
|
|
|
- var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(_Database, t.Name), queryOptions);
|
|
|
+ var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), queryOptions);
|
|
|
|
|
|
// query.Where();
|
|
|
return objs;
|
|
|
- //return CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(_Database, t.Name),sql);
|
|
|
+ //return CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name),sql);
|
|
|
|
|
|
}
|
|
|
|
|
@@ -249,7 +249,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
Type t = typeof(T);
|
|
|
List<T> objs = new List<T>();
|
|
|
await InitializeCollection<T>();
|
|
|
- var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(_Database, t.Name), sql);
|
|
|
+ var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql);
|
|
|
foreach (var item in query)
|
|
|
{
|
|
|
objs.Add(item);
|
|
@@ -266,7 +266,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
await InitializeCollection<T>();
|
|
|
//查询条数 -1是全部
|
|
|
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = open };
|
|
|
- var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(_Database, t.Name), sql, queryOptions);
|
|
|
+ var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql, queryOptions);
|
|
|
foreach (var item in query)
|
|
|
{
|
|
|
objs.Add(item);
|
|
@@ -286,7 +286,6 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
|
|
|
string PKname = "";
|
|
|
PropertyInfo[] properties = t.GetProperties();
|
|
|
- List<PropertyInfo> attrProperties = new List<PropertyInfo>();
|
|
|
foreach (PropertyInfo property in properties)
|
|
|
{
|
|
|
object[] attributes = property.GetCustomAttributes(true);
|
|
@@ -318,11 +317,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
await InitializeCollection<T>();
|
|
|
//查询条数 -1是全部
|
|
|
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = open };
|
|
|
- var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(_Database, t.Name), queryOptions);
|
|
|
+ var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), queryOptions);
|
|
|
|
|
|
List<T> list = DynamicLinq.GenerateFilter<T>(query, filters).ToList();
|
|
|
return list;
|
|
|
- //return CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(_Database, t.Name),sql);
|
|
|
+ //return CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name),sql);
|
|
|
|
|
|
}
|
|
|
public async Task<string> DeleteAsync<T>(string id)
|
|
@@ -330,7 +329,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
Type t = typeof(T);
|
|
|
await InitializeCollection<T>();
|
|
|
ResourceResponse<Document> doc =
|
|
|
- await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(_Database, t.Name, id));
|
|
|
+ await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, id));
|
|
|
//Console.WriteLine(doc.ActivityId);
|
|
|
return id;
|
|
|
}
|
|
@@ -344,13 +343,13 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
string pkValue = entity.GetType().GetProperty(PartitionKey).GetValue(entity).ToString();
|
|
|
string idValue = entity.GetType().GetProperty("id").GetValue(entity).ToString();
|
|
|
ResourceResponse<Document> doc =
|
|
|
- await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(_Database, t.Name, idValue), new RequestOptions { PartitionKey = new PartitionKey(pkValue) });
|
|
|
+ await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, idValue), new RequestOptions { PartitionKey = new PartitionKey(pkValue) });
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
string idValue = entity.GetType().GetProperty("id").GetValue(entity).ToString();
|
|
|
ResourceResponse<Document> doc =
|
|
|
- await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(_Database, t.Name, idValue));
|
|
|
+ await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, idValue));
|
|
|
}
|
|
|
|
|
|
|
|
@@ -364,7 +363,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
|
|
|
await InitializeCollection<T>();
|
|
|
ResourceResponse<Document> doc =
|
|
|
- await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(_Database, t.Name, id), new RequestOptions { PartitionKey = new PartitionKey(partitionKey) });
|
|
|
+ await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, id), new RequestOptions { PartitionKey = new PartitionKey(partitionKey) });
|
|
|
//Console.WriteLine(doc.ActivityId);
|
|
|
return id;
|
|
|
|
|
@@ -483,8 +482,8 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
string id = lists[j].GetType().GetProperty("id").GetValue(lists[j]) + "";
|
|
|
updateItemsInBatch.Add(new UpdateItem(id, partitionKeyValue, updateOperations));
|
|
|
}
|
|
|
- var tasks = new List<Task>();
|
|
|
- tasks.Add(Task.Run(async () =>
|
|
|
+ var tasks = new List<Task>
|
|
|
+ { Task.Run(async () =>
|
|
|
{
|
|
|
do
|
|
|
{
|
|
@@ -508,8 +507,9 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
|
totalRequestUnitsConsumed += bulkUpdateResponse.TotalRequestUnitsConsumed;
|
|
|
totalTimeTakenSec += bulkUpdateResponse.TotalTimeTaken.TotalSeconds;
|
|
|
},
|
|
|
- token));
|
|
|
- await Task.WhenAll(tasks);
|
|
|
+ token)
|
|
|
+ };
|
|
|
+ await Task.WhenAll(tasks);
|
|
|
}
|
|
|
return list;
|
|
|
}
|