Esempio n. 1
0
        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);
                }
            }
        }
Esempio n. 2
0
        private Tuple <CouchDocInfo, object>[] GetModifiedEntities()
        {
            CouchDocInfo[] modifiedDocInfos = DocumentManager.GetModifiedDocuments();
            var            modifiedEntities =
                from di in modifiedDocInfos
                select Tuple.Create(di, IdentityMap.GetEntityById(di.Id));

            return(modifiedEntities.ToArray());
        }