Esempio n. 1
0
        /// <summary>
        /// If the document with the specified <paramref name="key"/> does not exist,
        /// adds it using the specified <paramref name="addData"/> delegate.
        /// Else, returns it.
        /// </summary>
        public static Task <IResult <TData> > GetOrAddAsync <TData>(
            this IDocumentTopic <TData> source, DocumentKey key,
            Func <TData> addData) where TData : class
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (addData is null)
            {
                throw new ArgumentNullException(nameof(addData));
            }

            return(source.GetOrAddAsync(
                       key: key,
                       addDataAsync: _ => Task.FromResult(addData())
                       ));
        }
Esempio n. 2
0
        /// <summary>
        /// If the document with the specified <paramref name="key"/> does not exist,
        /// adds it using the specified <paramref name="addData"/> delegate.
        /// Else, Updates it using the specified <paramref name="updateData"/> delegate.
        /// </summary>
        public static Task <IResult <TData> > AddOrUpdateAsync <TData>(
            this IDocumentTopic <TData> source, DocumentKey key,
            Func <TData> addData, Func <TData, TData> updateData) where TData : class
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (addData is null)
            {
                throw new ArgumentNullException(nameof(addData));
            }
            if (updateData is null)
            {
                throw new ArgumentNullException(nameof(updateData));
            }

            return(source.AddOrUpdateAsync(
                       key: key,
                       addDataAsync: _ => Task.FromResult(addData()),
                       updateDataAsync: (_, data) => Task.FromResult(updateData(data))
                       ));
        }
Esempio n. 3
0
        /// <summary>
        /// Returns instances of <typeparamref name="TData"/> contained within
        /// documents associated to this instance of <see cref="IDocumentTopic{TData}"/>
        /// based on the specified <paramref name="predicate"/>.
        /// </summary>
        public static async Task <IReadOnlyDictionary <DocumentKey, TData> > GetAllAsync <TData>(
            this IDocumentTopic <TData> source,
            Func <DocumentKey, bool> predicate) where TData : class
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (predicate is null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            var keys = await source
                       .GetKeysAsync()
                       .ConfigureAwait(false);

            var filteredKeys = keys.Where(predicate);

            async Task <KeyValuePair <DocumentKey, IResult <TData> > > GetAsync(DocumentKey key) =>
            new KeyValuePair <DocumentKey, IResult <TData> >(
                key: key,
                value: await source.GetAsync(key).ConfigureAwait(false)
                );

            var results = await Task
                          .WhenAll(filteredKeys.Select(GetAsync))
                          .ConfigureAwait(false);

            return(results
                   .Where(r => r.Value.Success)
                   .ToDictionary(
                       keySelector: kvp => kvp.Key,
                       elementSelector: kvp => kvp.Value.Validate()
                       ));
        }
Esempio n. 4
0
 /// <summary>
 /// Creates a channel for the document with the specified <paramref name="key"/>
 /// </summary>
 public static IDocumentChannel <TData> ToChannel <TData>(
     this IDocumentTopic <TData> source, DocumentKey key) where TData : class =>
 new DocumentChannel <TData>(source, key);
Esempio n. 5
0
 /// <summary>
 /// Returns instances of <typeparamref name="TData"/> contained within
 /// documents associated to this instance of <see cref="IDocumentTopic{TData}"/>.
 /// </summary>
 public static Task <IReadOnlyDictionary <DocumentKey, TData> > GetAllAsync <TData>(
     this IDocumentTopic <TData> source) where TData : class =>
 source.GetAllAsync(_ => true);
 public void SetUp()
 {
     topicMock = Mock.Of <IDocumentTopic <string> >();
 }
Esempio n. 7
0
 public DocumentChannel(IDocumentTopic <TData> topic, DocumentKey key)
 {
     this.topic = topic ?? throw new ArgumentNullException(nameof(topic));
     this.key   = key;
 }