private VersionedDocumentReadResult <TDocument> ReadDocumentAsync <TDocument>(
            string id,
            DocumentTypeMapping <TDocument> mapping,
            IList <VersionedDbDocument> documents,
            VersionedDocumentReadOptions readOptions)
        {
            VersionedDbDocument document;
            DateTime            createdTime;
            DateTime            modifiedTime;

            if (!TryGetLatestDocument(documents, readOptions, out document, out createdTime, out modifiedTime))
            {
                return(null);
            }

            var metadata = new VersionedDocumentMetadata(document.Version, document.Deleted, createdTime, modifiedTime, document.Actor);

            TDocument content;
            DocumentReadFailureDetails failure;

            if (!TryGetDocumentContent(document, mapping, out content, out failure))
            {
                return(VersionedDocumentReadResult <TDocument> .CreateFailure(id, metadata, failure));
            }

            return(VersionedDocumentReadResult <TDocument> .CreateOkay(id, metadata, content));
        }
        /// <inheritdoc />
        public async Task <VersionedDocumentQueryResult <TDocument> > GetDocumentsAsync <TDocument>(
            string query,
            IEnumerable <DbParameter> parameters,
            DocumentTypeMapping <TDocument> mapping,
            VersionedDocumentReadOptions options)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (mapping == null)
            {
                throw new ArgumentNullException(nameof(mapping));
            }

            parameters = parameters ?? new DbParameter[0];

            var builtQuery = QueryClient.CreateQueryByLatest(mapping, query, parameters);
            var documents  = await ExecuteQueryAsync(builtQuery);

            if (documents.Count == 0)
            {
                return(VersionedDocumentQueryResult <TDocument> .Empty);
            }

            options = options ?? new VersionedDocumentReadOptions();

            List <VersionedDocumentReadResult <TDocument> > loaded = new List <VersionedDocumentReadResult <TDocument> >();
            List <VersionedDocumentReadResult <TDocument> > failed = new List <VersionedDocumentReadResult <TDocument> >();

            foreach (var docs in documents.GroupBy(x => x.DocumentId))
            {
                VersionedDbDocument doc;
                DateTime            createdTime;
                DateTime            modifiedTime;

                if (!TryGetLatestDocument(docs, options, out doc, out createdTime, out modifiedTime))
                {
                    // Document exists but the latest version is deleted.
                    continue;
                }

                var metadata = new VersionedDocumentMetadata(doc.Version, doc.Deleted, createdTime, modifiedTime, doc.Actor);

                TDocument content;
                DocumentReadFailureDetails failure;

                if (TryGetDocumentContent(doc, mapping, out content, out failure))
                {
                    loaded.Add(VersionedDocumentReadResult <TDocument> .CreateOkay(doc.DocumentId, metadata, content));
                }
                else
                {
                    failed.Add(VersionedDocumentReadResult <TDocument> .CreateFailure(doc.DocumentId, metadata, failure));
                }
            }

            return(new VersionedDocumentQueryResult <TDocument>(ImmutableList.CreateRange(loaded), ImmutableList.CreateRange(failed)));
        }
Exemplo n.º 3
0
        public static VersionedDocumentReadResult<TTo> ConvertType<TFrom, TTo>(
            VersionedDocumentReadResult<TFrom> recordResult,
            Func<TFrom, TTo> convertFunc)
        {
            if (recordResult.ResultType == DocumentReadResultType.Failed)
            {
                return VersionedDocumentReadResult<TTo>.CreateFailure(
                    recordResult.DocumentId, recordResult.Metadata, recordResult.FailureDetails);
            }

            return VersionedDocumentReadResult<TTo>.CreateOkay(
                recordResult.DocumentId, recordResult.Metadata, convertFunc(recordResult.Document));
        }
        /// <summary>
        /// Creates a read result with a converted document.
        /// </summary>
        /// <typeparam name="TFrom">The type to convert from.</typeparam>
        /// <typeparam name="TTo">The type to convert to.</typeparam>
        /// <param name="result">The read result.</param>
        /// <param name="convertFunc">The function to convert to the target type.</param>
        /// <returns>The converted read result.</returns>
        public static VersionedDocumentReadResult <TTo> WithType <TFrom, TTo>(
            this VersionedDocumentReadResult <TFrom> result,
            Func <TFrom, TTo> convertFunc)
        {
            if (result == null)
            {
                throw new ArgumentNullException(nameof(result));
            }
            if (convertFunc == null)
            {
                throw new ArgumentNullException(nameof(convertFunc));
            }

            return(VersionedResultUtils.ConvertType(result, convertFunc));
        }
        private VersionedDocumentReadResult <TDocument> CreateReadResult <TDocument>(
            string id,
            VersionedDbDocument document,
            DocumentTypeMapping <TDocument> mapping,
            DateTime createdTime,
            DateTime modifiedTime)
        {
            var metadata = new VersionedDocumentMetadata(document.Version, document.Deleted, createdTime, modifiedTime, document.Actor);

            TDocument content;
            DocumentReadFailureDetails failure;

            if (!TryGetDocumentContent(document, mapping, out content, out failure))
            {
                return(VersionedDocumentReadResult <TDocument> .CreateFailure(id, metadata, failure));
            }

            return(VersionedDocumentReadResult <TDocument> .CreateOkay(id, metadata, content));
        }
        /// <inheritdoc />
        public async Task <VersionedDocumentBatchReadResult <TDocument> > GetDocumentsAsync <TDocument>(
            IEnumerable <string> ids,
            DocumentTypeMapping <TDocument> mapping,
            VersionedDocumentReadOptions options)
        {
            if (ids == null)
            {
                throw new ArgumentNullException(nameof(ids));
            }
            if (mapping == null)
            {
                throw new ArgumentNullException(nameof(mapping));
            }

            var idSet = new HashSet <string>(ids);

            if (idSet.Count == 0)
            {
                return(VersionedDocumentBatchReadResult <TDocument> .Empty);
            }

            options = options ?? new VersionedDocumentReadOptions();

            var query     = QueryClient.CreateQueryByIds(idSet, mapping);
            var documents = await ExecuteQueriesAsync(query);

            List <VersionedDocumentReadResult <TDocument> > loaded = new List <VersionedDocumentReadResult <TDocument> >();
            List <VersionedDocumentReadResult <TDocument> > failed = new List <VersionedDocumentReadResult <TDocument> >();

            foreach (var docs in documents.GroupBy(x => x.DocumentId))
            {
                VersionedDbDocument doc;
                DateTime            createdTime;
                DateTime            modifiedTime;

                if (!TryGetLatestDocument(docs, options, out doc, out createdTime, out modifiedTime))
                {
                    // Document exists but the latest version is deleted.
                    continue;
                }

                var metadata = new VersionedDocumentMetadata(doc.Version, doc.Deleted, createdTime, modifiedTime, doc.Actor);

                TDocument content;
                DocumentReadFailureDetails failure;

                if (TryGetDocumentContent(doc, mapping, out content, out failure))
                {
                    loaded.Add(VersionedDocumentReadResult <TDocument> .CreateOkay(doc.DocumentId, metadata, content));
                }
                else
                {
                    failed.Add(VersionedDocumentReadResult <TDocument> .CreateFailure(doc.DocumentId, metadata, failure));
                }

                idSet.Remove(doc.DocumentId);
            }

            var missing = ImmutableList.CreateRange(idSet);

            return(new VersionedDocumentBatchReadResult <TDocument>(ImmutableList.CreateRange(loaded), missing, ImmutableList.CreateRange(failed)));
        }