private void LoadRelatedPostProcess <T>( IEnumerable <T> sourceEntities, LoadRelatedOptions options, LoadRelatedPreProcessInfo preProcessInfo) { JToken[] rows = ClientAdaper.GetDocuments(preProcessInfo.EntityIdsToLoad); EntitiesProcessResult processResult = Process(rows, new OdmViewProcessingOptions()); Dictionary <string, EntitiesProcessResult> viewsSelectResult = SelectToManyFromViews(preProcessInfo); foreach (T sourceEntity in sourceEntities) { string entityId = GetEntityInstanceId(sourceEntity); JToken document = DocumentManager.DocInfo(entityId).Document; // ToOne foreach (PropertyInfo toOneProp in options.ToOne) { string relatedEntityId = GetRelatedToOneEntityId(document, toOneProp); object relatedToOneEntity = processResult.GetEntity(relatedEntityId); // This line was comment out because it is possible to get null related entity // when the property is optional. This check needs to be done only for required properties. //if (relatedToOneEntity == null) throw new Exception("Fail to find ToOne related entity for property " + toOneProp); toOneProp.SetValue(sourceEntity, relatedToOneEntity); } // ToOne already loaded foreach (PropertyInfo toOneProp in options.ToOneExist) { string relatedEntityId = GetRelatedToOneEntityId(document, toOneProp); object relatedToOneEntity = identityMap.GetEntityById(relatedEntityId); if (relatedToOneEntity == null) { throw new Exception("Fail to find ToOneExist related entity for property " + toOneProp); } toOneProp.SetValue(sourceEntity, relatedToOneEntity); } // ToMany Direct foreach (PropertyInfo toManyDirectProp in options.ToManyDirect) { string[] relatedEntitiesIds = GetRelatedToManyEntitiesIds(document, toManyDirectProp); if (relatedEntitiesIds != null) { serializer.SetDirectAssoicationCollectionProperty(sourceEntity, toManyDirectProp, relatedEntitiesIds); } } // ToMany Inverse foreach (LoadRelatedWithViewInfo toManyViewInfo in options.ToManyView) { EntitiesProcessResult viewProcessResult = viewsSelectResult[toManyViewInfo.ViewName]; string[] relatedEntitiesIds = viewProcessResult.GetRelatedEntitiesIds(entityId); AssociationAttribute associationAttr = AssociationAttribute.GetSingle(toManyViewInfo.PropertyInfo); serializer.SetInverseAssociationCollectionInternal( sourceEntity, toManyViewInfo.PropertyInfo, associationAttr, relatedEntitiesIds); } } }
private EntitiesProcessResult ExecuteView(CouchViewOptions viewOptions) { JToken[] rows = couchDBContext.ClientAdaper.GetViewRows(viewName, viewOptions); var processingOptions = new OdmViewProcessingOptions(assoicateCollectionsToLoad); EntitiesProcessResult processResult = couchDBContext.Process(rows, processingOptions); return(processResult); }
/// <summary> /// Process document rows. /// </summary> /// <param name="rows"></param> /// <returns></returns> internal EntitiesProcessResult Process( JToken[] rows, OdmViewProcessingOptions processingOptions) { EntitiesProcessResult result = IdentityMap.Process(rows, processingOptions); DocumentManager.Add(result.NewEntities); return(result); }
private Dictionary <string, EntitiesProcessResult> SelectToManyFromViews(LoadRelatedPreProcessInfo preProcessInfo) { var viewSelectResult = new Dictionary <string, EntitiesProcessResult>(); foreach (KeyValuePair <string, string[]> viewSelectionInfo in preProcessInfo.ViewsToSelect) { CouchViewOptions viewOptions = new CouchViewOptions { IncludeDocs = true, Keys = viewSelectionInfo.Value.Cast <object>().ToList() }; JToken[] rows = clientAdapter.GetViewRows(viewSelectionInfo.Key, viewOptions); EntitiesProcessResult processResult = Process(rows, new OdmViewProcessingOptions()); viewSelectResult.Add(viewSelectionInfo.Key, processResult); } return(viewSelectResult); }