Following is a generic repository we can use in order to do data manipulations in Azure document DB. This can be used to save and retrieve POCO objects and generic streamed objects in Azure Document DB.
I have used MEF to do the dependency injections. This repository is used only to deal with one collection. More generic implementation is on its way and will update soon.
Following is the consumer class.
I have used MEF to do the dependency injections. This repository is used only to deal with one collection. More generic implementation is on its way and will update soon.
public interface IDocumentDb { dynamic GetRecord(string id); IEnumerable<object> GetRecords(); Task Add(object entity); Task Update(object entity,string id); Task Delete(string id); void Dispose(); }The implementation class is as follows.
[Export(typeof(IDocumentDb))] [PartCreationPolicy(CreationPolicy.NonShared)] public class DocumentDb : IDocumentDb { private static DocumentClient _client; private Database _database; private DocumentCollection _collection; private static readonly string DadatabaseId = ConfigurationManager.AppSettings["DatabaseId"]; private static readonly string EndpointUrl = ConfigurationManager.AppSettings["EndPointUrl"]; private static readonly string AuthorizationKey = ConfigurationManager.AppSettings["AuthorizationKey"]; private static readonly string CollectionId = ConfigurationManager.AppSettings["CollectionId"]; public DocumentDb() { Init(); } private async Task Init() { _client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey); try { this._database = _client.CreateDatabaseQuery().Where(db => db.Id == DadatabaseId).ToArray().FirstOrDefault(); if (_database == null) { this._database = await _client.CreateDatabaseAsync(new Database { Id = DadatabaseId }); } this._collection = _client.CreateDocumentCollectionQuery(_database.SelfLink).Where(c => c.Id == CollectionId).ToArray().FirstOrDefault(); if (_collection == null) { _collection = await _client.CreateDocumentCollectionAsync(_database.SelfLink, new DocumentCollection { Id = CollectionId }); } } catch (Exception ex) { throw; } } public async Task Add(object entity) { try { await _client.CreateDocumentAsync(_collection.SelfLink, entity); } catch (Exception ex) { throw; } } public async Task Update(object entity,string id) { try { var doc = _client.CreateDocumentQuery(_collection.SelfLink).Where(d => d.Id == id).AsEnumerable().FirstOrDefault(); if (doc != null) await _client.ReplaceDocumentAsync(doc.SelfLink, entity); } catch (Exception ex) { throw; } } public dynamic GetRecord(string id) { try { dynamic doc = _client.CreateDocumentQuery (_collection.SelfLink).Where(d => d.Id == id).AsEnumerable().FirstOrDefault(); return doc; } catch (Exception ex) { throw; } } public IEnumerable<object> GetRecords() { try { var obj = _client.CreateDocumentQuery(_collection.SelfLink, "SELECT * FROM " + CollectionId).AsEnumerable().ToList(); return obj; } catch (Exception ex) { throw; } } public async Task Delete(string id) { try { var doc = _client.CreateDocumentQuery(_collection.SelfLink) .Where(d => d.Id == id) .AsEnumerable() .FirstOrDefault(); if (doc != null) { var resourceResponse = await _client.DeleteDocumentAsync(doc.SelfLink); } } catch (Exception ex) { throw; } } public void Dispose() { if (_client == null) { return; } _client.Dispose(); } }
public class Consumer { [Import(typeof(IDocumentDb))] public IDocumentDb DocumentDb { get; set; } public Consumer() { } public void TempAdd() { DocDbCustomFieldViewModel docDbEntity = new DocDbCustomFieldViewModel(); docDbEntity.Id = model.ID; docDbEntity.ClientId = CurrentClientID; docDbEntity.ProjectId = CurrentProjectID; ListlCusFields = new List (); foreach (var cusField in cusFields) { CustomFieldViewModel newCusField = CustomFieldProvider.Get(cusField.CustomFieldID); CustomFieldViewViewModel tmpModel = new CustomFieldViewViewModel(); tmpModel.DisplayName = newCusField.DisplayName; tmpModel.FieldName = newCusField.FieldName; tmpModel.Value = string.Empty; lCusFields.Add(tmpModel); } docDbEntity.CustomFields = lCusFields; DocumentDb.Add(docDbEntity); } public void TempSearch(string id) }
No comments:
Post a Comment