Exemple #1
0
        /// <summary>
        /// Creates an <c>InMemoryMultiTenantStore</c> from configured <c>InMemoryMultiTenantStoreOptions</c>.
        /// </summary>
        private InMemoryMultiTenantStore InMemoryStoreFactory(Action <InMemoryMultiTenantStoreOptions> config, bool ignoreCase, ILogger <InMemoryMultiTenantStore> logger)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var options = new InMemoryMultiTenantStoreOptions();

            config(options);
            var store = new InMemoryMultiTenantStore(ignoreCase, logger);

            try
            {
                foreach (var tenantConfig in options.TenantConfigurations ?? new InMemoryMultiTenantStoreOptions.TenantConfiguration[0])
                {
                    if (string.IsNullOrWhiteSpace(tenantConfig.Id) ||
                        string.IsNullOrWhiteSpace(tenantConfig.Identifier))
                    {
                        throw new MultiTenantException("Tenant Id and Identifer cannot be null or whitespace.");
                    }

                    var tenantContext = new TenantContext(tenantConfig.Id,
                                                          tenantConfig.Identifier,
                                                          tenantConfig.Name,
                                                          tenantConfig.ConnectionString ?? options.DefaultConnectionString,
                                                          null,
                                                          null);

                    // Add any other items from the config, but don't override
                    // the explicitly named items.
                    foreach (var item in tenantConfig.Items ?? new Dictionary <string, string>())
                    {
                        if (!tenantContext.Items.ContainsKey(item.Key))
                        {
                            tenantContext.Items.Add(item.Key, item.Value);
                        }
                    }

                    if (!store.TryAdd(tenantContext).Result)
                    {
                        throw new MultiTenantException($"Unable to add {tenantContext.Identifier} is already configured.");
                    }
                }
            }
            catch (Exception e)
            {
                throw new MultiTenantException
                          ("Unable to add tenant context to store.", e);
            }

            return(store);
        }
        /// <summary>
        /// Creates an <c>InMemoryMultiTenantStore</c> from configured <c>InMemoryMultiTenantStoreOptions</c>.
        /// </summary>
        /// <param name="sp"></param>
        /// <returns></returns>
        private InMemoryMultiTenantStore StoreFactory(IServiceProvider sp)
        {
            var optionsAccessor      = sp.GetService <IOptions <InMemoryMultiTenantStoreOptions> >();
            var tenantConfigurations = optionsAccessor?.Value.TenantConfigurations ?? new InMemoryMultiTenantStoreOptions.TenantConfiguration[0];

            var store = new InMemoryMultiTenantStore();

            try
            {
                foreach (var tenantConfig in tenantConfigurations)
                {
                    if (string.IsNullOrWhiteSpace(tenantConfig.Id) ||
                        string.IsNullOrWhiteSpace(tenantConfig.Identifier))
                    {
                        throw new MultiTenantException("Tenant Id and Identifer cannot be null or whitespace.");
                    }

                    var tc = new TenantContext(tenantConfig.Id,
                                               tenantConfig.Identifier,
                                               tenantConfig.Name,
                                               tenantConfig.ConnectionString ?? optionsAccessor.Value.DefaultConnectionString,
                                               null,
                                               null);

                    // Add any other items from the config, but don't override
                    // the explicitly named items.
                    foreach (var item in tenantConfig.Items ?? new Dictionary <string, string>())
                    {
                        if (!tc.Items.ContainsKey(item.Key))
                        {
                            tc.Items.Add(item.Key, item.Value);
                        }
                    }

                    if (!store.TryAdd(tc).Result)
                    {
                        throw new MultiTenantException($"Unable to add {tc.Identifier} is already configured.");
                    }
                }
            }
            catch (Exception e)
            {
                throw new MultiTenantException
                          ("Unable to add tenant context to store.", e);
            }

            return(store);
        }
 public void ThrowIfTenantContextIsNullWhenAdding()
 {
     var store = new InMemoryMultiTenantStore();
     var e     = Assert.Throws <ArgumentNullException>(() => store.TryAdd(null).Result);
 }
 public void ThrowIfTenantIdentifierIsNullWhenRemoving()
 {
     var store = new InMemoryMultiTenantStore();
     var e     = Assert.Throws <ArgumentNullException>(() => store.TryRemove(null).Result);
 }