public static CouchDocument Dehydrate(object entity, PropertyInfo identityProperty, string revision, bool delete) { var couchDocument = new CouchDocument(); var entityType = entity.GetType(); if (delete) { couchDocument["_id"] = entityType.GetProperty(identityProperty.Name).GetValue(entity, null); couchDocument["_rev"] = revision; couchDocument["_deleted"] = true; } else { couchDocument["Type"] = entityType.Name; if (revision != String.Empty) { couchDocument["_rev"] = revision; } foreach (PropertyInfo property in entityType.GetProperties().Where(p => p.CanRead)) { var key = property.Name; if (key == identityProperty.Name) { key = "_id"; } var propertyValue = property.GetValue(entity, null); couchDocument[key] = propertyValue; } } return(couchDocument); }
public void SaveChanges() { var docs = new List <CouchDocument>(); var entities = new List <object>(); foreach (var entity in DeletedEntities) { EntityMetadata entityMetadata; if (EntityMetadataMap.TryGetValue(entity, out entityMetadata)) { EntityMetadataMap.Remove(entity); IdentityMap.Remove(entityMetadata.Key); Type entityType = entity.GetType(); PropertyInfo identityProperty = CouchDatabase.CouchDocumentConvention.GetIdentityPropertyFor(entityType); docs.Add(CouchDocument.Dehydrate(entity, identityProperty, entityMetadata.Revision, true)); entities.Add(entity); } } DeletedEntities.Clear(); foreach (var entity in EntityMetadataMap.Where(pair => IsEntityDirty(pair.Key, pair.Value))) { Type entityType = entity.Key.GetType(); PropertyInfo identityProperty = CouchDatabase.CouchDocumentConvention.GetIdentityPropertyFor(entityType); docs.Add(CouchDocument.Dehydrate(entity.Key, identityProperty, entity.Value.Revision, false)); entities.Add(entity.Key); } if (docs.Count == 0 && entities.Count == 0) { return; } var bulkDocsMessage = new BulkDocsMessage(docs); var bulkDocsCommand = new BulkDocsCommand(CouchDatabase.Name, bulkDocsMessage); BulkDocsResult[] results = CouchDatabase.CouchProxy.Execute <BulkDocsResult[]>(bulkDocsCommand); for (int index = 0; index < results.Length; index++) { BulkDocsResult result = results[index]; object entity = entities[index]; EntityMetadata entityMetadata; if (EntityMetadataMap.TryGetValue(entity, out entityMetadata) == false) { continue; } IdentityMap[result.Id] = entity; entityMetadata.Revision = result.Rev; entityMetadata.OriginalEntity = entity; } }
public T Load <T>(string id) where T : new() { object existingEntity; if (IdentityMap.TryGetValue(id, out existingEntity)) { return((T)existingEntity); } var getDocumentCommand = new GetDocumentCommand(CouchDatabase.Name, id); CouchDocument couchDocument = CouchDatabase.CouchProxy.Execute <CouchDocument>(getDocumentCommand); return(StalkEntity <T>(couchDocument)); }
private T StalkEntity <T>(CouchDocument couchDocument) where T : new() { PropertyInfo identityProperty = CouchDatabase.CouchDocumentConvention.GetIdentityPropertyFor(typeof(T)); T entity = couchDocument.Hydrate <T>(identityProperty); EntityMetadata entityMetadata = new EntityMetadata { Key = couchDocument["_id"].ToString(), Revision = couchDocument["_rev"].ToString(), OriginalEntity = entity }; EntityMetadataMap[entity] = entityMetadata; IdentityMap[entityMetadata.Key] = entity; return(entity); }