我有一个通用IDocumentDbRepository存储库,用于为 DocumentDB 提供基本的 CRUD 操作,并为具有特定实体的附加操作提供特定存储库,这些实体继承或实现了此接口和类。
public interface IDocumentDbRepository<T> where T : class
{
//IEnumerable<T> GetItems();
Task<IEnumerable<T>> GetItemsAsync();
Task<T> GetItemAsync(T id);
Task<T> AddDocumentAsync(T item);
Task<T> UpdateDocumentAsync(T id, T item);
Task DeletedocumentAsync(T id);
}
public class DocumentDbRepository<T> : IDocumentDbRepository<T> where T : class
{
private readonly string AuthKey;
private readonly string EndpointUri;
private readonly string DatabaseId;
private readonly string CollectionId;
private static DocumentClient client;
public DocumentDbRepository(IOptions<DocumentDbSettings> DocumentDbConfig)
{
this.DatabaseId = DocumentDbConfig.Value.DatabaseName;
this.CollectionId = DocumentDbConfig.Value.CollectionName;
this.AuthKey = DocumentDbConfig.Value.AuthKey;
this.EndpointUri = DocumentDbConfig.Value.EndpointUri;
client = new DocumentClient(new Uri(EndpointUri), AuthKey);
}
public async Task<IEnumerable<T>> GetItemsAsync()
{
IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId))
.AsDocumentQuery();
List<T> results = new List<T>();
while (query.HasMoreResults)
{
results.AddRange(await query.ExecuteNextAsync<T>());
}
return results;
}
// public IEnumerable<T> GetItems()
// {
// var results = client.CreateDocumentQuery<T>//(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)).ToList();
// return results;
// }
//methods
在我的特定Employee存储库中,我的目标是仅返回类型的文档Employee
public interface IEmployeeRepository : IDocumentDbRepository<Employee>
{
List<Employee> GetAllEmployees();
}
www说
相关分类