public ICollection <LoadedObjectDataWithDataSourceData> GetOrLoadFetchQueryResult(
            IQuery query,
            LoadedObjectDataPendingRegistrationCollector pendingRegistrationCollector)
        {
            ArgumentUtility.CheckNotNull("query", query);
            ArgumentUtility.CheckNotNull("pendingRegistrationCollector", pendingRegistrationCollector);

            var loadedObjectDataWithSource = _persistenceStrategy.ExecuteFetchQuery(query, LoadedObjectDataProvider).ConvertToCollection();

            var loadedObjectDataAfterConsolidation = LoadedObjectDataRegistrationAgent
                                                     .BeginRegisterIfRequired(
                loadedObjectDataWithSource.Select(data => data.LoadedObjectData),
                true,
                pendingRegistrationCollector)
                                                     .ToList();

            Assertion.IsTrue(loadedObjectDataWithSource.Count == loadedObjectDataAfterConsolidation.Count);
            var loadedObjectDataWithSourceAfterConsolidation = loadedObjectDataWithSource
                                                               .Select((d, i) => new LoadedObjectDataWithDataSourceData(loadedObjectDataAfterConsolidation[i], d.DataSourceData))
                                                               .ConvertToCollection();

            _eagerFetcher.PerformEagerFetching(loadedObjectDataAfterConsolidation, query.EagerFetchQueries, this, pendingRegistrationCollector);

            return(loadedObjectDataWithSourceAfterConsolidation);
        }
        public void EndRegisterIfRequired(LoadedObjectDataPendingRegistrationCollector pendingLoadedObjectDataCollector)
        {
            ArgumentUtility.CheckNotNull("pendingLoadedObjectDataCollector", pendingLoadedObjectDataCollector);

            if (pendingLoadedObjectDataCollector.DataPendingRegistration.Count == 0)
            {
                return;
            }

            // Note: After this event, OnAfterObjectRegistration _must_ be raised for the same ObjectIDs! Otherwise, we'll leak "objects currently loading".
            var objectIDs = pendingLoadedObjectDataCollector.DataPendingRegistration.Select(data => data.ObjectID).ToList().AsReadOnly();

            _registrationListener.OnBeforeObjectRegistration(objectIDs);

            var loadedDomainObjects = new List <DomainObject> (pendingLoadedObjectDataCollector.DataPendingRegistration.Count);

            try
            {
                foreach (var data in pendingLoadedObjectDataCollector.DataPendingRegistration)
                {
                    var dataContainer = data.FreshlyLoadedDataContainer;
                    Assertion.IsTrue(dataContainer.HasDomainObject);

                    _dataManager.RegisterDataContainer(dataContainer);
                    loadedDomainObjects.Add(dataContainer.DomainObject);
                }
            }
            finally
            {
                _registrationListener.OnAfterObjectRegistration(objectIDs, loadedDomainObjects.AsReadOnly());
            }
        }
        public IEnumerable <ILoadedObjectData> BeginRegisterIfRequired(
            IEnumerable <ILoadedObjectData> loadedObjects, bool throwOnNotFound, LoadedObjectDataPendingRegistrationCollector pendingLoadedObjectDataCollector)
        {
            ArgumentUtility.CheckNotNull("loadedObjects", loadedObjects);

            var visitor = new RegisteredDataContainerGatheringVisitor(pendingLoadedObjectDataCollector, _clientTransaction);

            foreach (var loadedObject in loadedObjects)
            {
                loadedObject.Accept(visitor);
            }

            if (visitor.NotFoundObjectIDs.Any())
            {
                _registrationListener.OnObjectsNotFound(visitor.NotFoundObjectIDs);

                // Note: If this exception is thrown, we have already set the DomainObjects of the freshly loaded DataContainer, and we've also added them to
                // the collector. This shouldn't make any difference, and it's easier to implement.
                if (throwOnNotFound)
                {
                    throw new ObjectsNotFoundException(visitor.NotFoundObjectIDs);
                }
            }

            return(visitor.LoadedObjectData);
        }
        public IEnumerable <ILoadedObjectData> RegisterIfRequired(IEnumerable <ILoadedObjectData> loadedObjects, bool throwOnNotFound)
        {
            var collector = new LoadedObjectDataPendingRegistrationCollector();
            var result    = BeginRegisterIfRequired(loadedObjects, throwOnNotFound, collector);

            EndRegisterIfRequired(collector);
            return(result);
        }
            public RegisteredDataContainerGatheringVisitor(
                LoadedObjectDataPendingRegistrationCollector dataPendingRegistrationCollector,
                ClientTransaction clientTransaction)
            {
                ArgumentUtility.CheckNotNull("dataPendingRegistrationCollector", dataPendingRegistrationCollector);
                ArgumentUtility.CheckNotNull("clientTransaction", clientTransaction);

                _dataPendingRegistrationCollector = dataPendingRegistrationCollector;
                _clientTransaction = clientTransaction;
            }
        public override ICollection <ILoadedObjectData> GetOrLoadCollectionQueryResult(IQuery query)
        {
            ArgumentUtility.CheckNotNull("query", query);

            var pendingRegistrationCollector = new LoadedObjectDataPendingRegistrationCollector();

            var loadedObjectData = _persistenceStrategy.ExecuteCollectionQuery(query, LoadedObjectDataProvider);

            var loadedObjectDataAfterConsolidation = LoadedObjectDataRegistrationAgent
                                                     .BeginRegisterIfRequired(loadedObjectData, true, pendingRegistrationCollector)
                                                     .ConvertToCollection();

            try
            {
                _eagerFetcher.PerformEagerFetching(loadedObjectDataAfterConsolidation, query.EagerFetchQueries, this, pendingRegistrationCollector);
            }
            finally
            {
                // Even with an exception during eager fetching, go ahead and register everything.
                LoadedObjectDataRegistrationAgent.EndRegisterIfRequired(pendingRegistrationCollector);
            }

            return(loadedObjectDataAfterConsolidation);
        }