コード例 #1
0
        /// <summary>
        /// Sets the default Account or Group store.
        /// </summary>
        /// <typeparam name="T">The parent resource type.</typeparam>
        /// <typeparam name="TMapping">The Account Store Mapping type.</typeparam>
        /// <param name="parent">The parent resource.</param>
        /// <param name="store">The new default Account or Group store.</param>
        /// <param name="isAccountStore">Determines whether this store should be the default Account (<see langword="true"/>) or Group (<see langword="false"/>) Store.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public static async Task SetDefaultStoreAsync <T, TMapping>(
            ISaveable <T> parent,
            IAccountStore store,
            bool isAccountStore,
            CancellationToken cancellationToken)
            where T : IResource, IAccountStoreContainer <TMapping>
            where TMapping : class, IAccountStoreMapping <TMapping>
        {
            if (string.IsNullOrEmpty(store?.Href))
            {
                throw new ArgumentNullException(nameof(store.Href));
            }

            var container = parent as IAccountStoreContainer <TMapping>;

            if (parent == null)
            {
                throw new ApplicationException("SetDefaultStore must be used with a supported AccountStoreContainer.");
            }

            IAccountStoreMapping <TMapping> newOrExistingMapping = null;
            await container.GetAccountStoreMappings().ForEachAsync(
                mapping =>
            {
                bool isPassedAccountStore = (mapping as AbstractAccountStoreMapping <TMapping>)?.AccountStore?.Href.Equals(store.Href) ?? false;
                if (isPassedAccountStore)
                {
                    newOrExistingMapping = mapping;
                }

                return(newOrExistingMapping != null);    // break if found
            }, cancellationToken).ConfigureAwait(false);

            if (newOrExistingMapping == null)
            {
                newOrExistingMapping = await container
                                       .AddAccountStoreAsync(store, cancellationToken).ConfigureAwait(false);
            }

            if (isAccountStore)
            {
                newOrExistingMapping.SetDefaultAccountStore(true);
            }
            else
            {
                newOrExistingMapping.SetDefaultGroupStore(true);
            }

            await newOrExistingMapping.SaveAsync(cancellationToken).ConfigureAwait(false);
        }