Beispiel #1
0
        public async Task Get_Tenant_Should_Cached()
        {
            var acme = await _tenantRepository.FindByNameAsync("acme");

            acme.ShouldNotBeNull();

            (await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(acme.Id, null))).ShouldBeNull();
            (await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(null, acme.Name))).ShouldBeNull();

            await _tenantStore.FindAsync(acme.Id);

            (await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(acme.Id, null))).ShouldNotBeNull();

            await _tenantStore.FindAsync(acme.Name);

            (await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(null, acme.Name))).ShouldNotBeNull();


            var volosoft = _tenantRepository.FindByName("volosoft");

            volosoft.ShouldNotBeNull();

            (_cache.Get(TenantCacheItem.CalculateCacheKey(volosoft.Id, null))).ShouldBeNull();
            (_cache.Get(TenantCacheItem.CalculateCacheKey(null, volosoft.Name))).ShouldBeNull();

            _tenantStore.Find(volosoft.Id);
            (_cache.Get(TenantCacheItem.CalculateCacheKey(volosoft.Id, null))).ShouldNotBeNull();

            _tenantStore.Find(volosoft.Name);
            (_cache.Get(TenantCacheItem.CalculateCacheKey(null, volosoft.Name))).ShouldNotBeNull();
        }
Beispiel #2
0
        public async Task FindDefaultConnectionString()
        {
            var acme = await _tenantRepository.FindByNameAsync("acme");

            acme.ShouldNotBeNull();
            acme.FindDefaultConnectionString().ShouldBe("DefaultConnString-Value");
        }
        public virtual async Task CreateAsync_UpdateAsync_DelteAsync_Test()
        {
            var tenant1 = await _tenantRepository.FindByNameAsync("tenant1");

            var tenant2 = await _tenantRepository.FindByNameAsync("tenant2");

            var id = await _mapTenantAppService.CreateAsync(new CreateMapTenantDto("100", tenant1.Id, "200"));

            var mapTenant1 = await _mapTenantAppService.GetAsync(id);

            var mapTenant1_2 = await _mapTenantAppService.FindByCodeAsync("100");

            var mapTenant1_3 = await _mapTenantAppService.FindByMapCodeAsync("200");

            Assert.Equal(mapTenant1.Code, mapTenant1_2.Code);
            Assert.Equal(mapTenant1.TenantId, mapTenant1_2.TenantId);
            Assert.Equal(mapTenant1.MapCode, mapTenant1_2.MapCode);
            Assert.Equal("200", mapTenant1.MapCode);

            Assert.Equal(mapTenant1.Code, mapTenant1_3.Code);
            Assert.Equal(mapTenant1.TenantId, mapTenant1_3.TenantId);
            Assert.Equal(mapTenant1.MapCode, mapTenant1_3.MapCode);

            await _mapTenantAppService.UpdateAsync(id, new UpdateMapTenantDto("300", tenant1.Id, "400"));

            var mapTenant2 = await _mapTenantAppService.FindByCodeAsync("300");

            var mapTenant2_2 = await _mapTenantAppService.FindByMapCodeAsync("400");

            Assert.Equal("300", mapTenant2.Code);
            Assert.Equal("400", mapTenant2.MapCode);
            Assert.Equal(tenant1.Id, mapTenant2.TenantId);

            Assert.Equal(mapTenant2.Code, mapTenant2_2.Code);
            Assert.Equal(mapTenant2.MapCode, mapTenant2_2.MapCode);
            Assert.Equal(tenant1.Id, mapTenant2_2.TenantId);


            //Update code
            await _mapTenantAppService.UpdateAsync(id, new UpdateMapTenantDto("300", tenant2.Id, "500"));

            var mapTenant3 = await _mapTenantAppService.GetAsync(id);

            await Assert.ThrowsAsync <AbpException>(() =>
            {
                return(_mapTenantAppService.CreateAsync(new CreateMapTenantDto("3001", tenant2.Id, "500")));
            });

            var mapTenancyConfiguration = await _mapTenancyConfigurationProvider.GetAsync("300");

            Assert.Equal(mapTenancyConfiguration.TenantId, tenant2.Id);

            await _mapTenantAppService.DeleteAsync(id);

            var mapTenant5 = await _mapTenantAppService.FindByCodeAsync("300");

            Assert.Null(mapTenant5);
        }
Beispiel #4
0
        public async Task ChangeNameAsync()
        {
            var tenant = await _tenantRepository.FindByNameAsync("volosoft").ConfigureAwait(false);

            tenant.ShouldNotBeNull();

            await _tenantManager.ChangeNameAsync(tenant, "newVolosoft").ConfigureAwait(false);

            tenant.Name.ShouldBe("newVolosoft");
        }
Beispiel #5
0
    public async Task FindAsyncById()
    {
        var acme = await _tenantRepository.FindByNameAsync("acme");

        acme.ShouldNotBeNull();

        (await _tenantStore.FindAsync(acme.Id)).ShouldNotBeNull();
    }
        protected virtual async Task ValidateNameAsync(string name, Guid?expectedId = null)
        {
            var tenant = await _tenantRepository.FindByNameAsync(name);

            if (tenant != null && tenant.Id != expectedId)
            {
                throw new UserFriendlyException("Duplicate tenancy name: " + name); //TODO: A domain exception would be better..?
            }
        }
Beispiel #7
0
        public async Task <TenantConfiguration> FindAsync(string name)
        {
            using (_currentTenant.Change(null)) //TODO: No need this if we can implement to define host side (or tenant-independent) entities!
            {
                var tenant = await _tenantRepository.FindByNameAsync(name).ConfigureAwait(false);

                if (tenant == null)
                {
                    return(null);
                }

                return(_objectMapper.Map <Tenant, TenantConfiguration>(tenant));
            }
        }
Beispiel #8
0
        protected virtual async Task <TenantConfigurationCacheItem> GetCacheItemByNameAsync(string name)
        {
            // 需要切换到最外层以解决查询无效的bug
            using (_currentTenant.Change(null))
            {
                var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name);

                Logger.LogDebug($"TenantStore.GetCacheItemByNameAsync: {cacheKey}");

                var cacheItem = await _cache.GetAsync(cacheKey);

                if (cacheItem != null)
                {
                    Logger.LogDebug($"Found in the cache: {cacheKey}");
                    return(cacheItem);
                }
                Logger.LogDebug($"Not found in the cache, getting from the repository: {cacheKey}");

                var tenant = await _tenantRepository.FindByNameAsync(name);

                if (tenant == null)
                {
                    Logger.LogWarning($"Can not found tenant by name: {name}");
                    // throw new AbpException($"Can not found tenant by name: {name}");
                    return(null);
                }
                var connectionStrings = new ConnectionStrings();
                foreach (var tenantConnectionString in tenant.ConnectionStrings)
                {
                    connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value;
                }
                cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings);

                Logger.LogDebug($"Setting the cache item: {cacheKey}");

                await _cache.SetAsync(cacheKey, cacheItem);

                // 用租户标识再次缓存,以便通过标识查询也能命中缓存
                await _cache.SetAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenant.Id.ToString()), cacheItem);

                Logger.LogDebug($"Finished setting the cache item: {cacheKey}");

                return(cacheItem);
            }
        }
Beispiel #9
0
        protected virtual async Task <TenantConfigurationCacheItem> GetCacheItemByNameAsync(string name)
        {
            var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name);

            Logger.LogDebug($"TenantStore.GetCacheItemByNameAsync: {cacheKey}");

            var cacheItem = await _cache.GetAsync(cacheKey);

            if (cacheItem != null)
            {
                Logger.LogDebug($"Found in the cache: {cacheKey}");
                return(cacheItem);
            }
            Logger.LogDebug($"Not found in the cache, getting from the repository: {cacheKey}");

            using (_dataFilter.Disable <IMultiTenant>())
            {
                var tenant = await _tenantRepository.FindByNameAsync(name);

                if (tenant == null)
                {
                    Logger.LogWarning($"Can not found tenant by name: {name}");
                    throw new AbpException($"Can not found tenant by name: {name}");
                }
                var connectionStrings = new ConnectionStrings();
                foreach (var tenantConnectionString in tenant.ConnectionStrings)
                {
                    connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value;
                }
                cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings);

                Logger.LogDebug($"Setting the cache item: {cacheKey}");
                await _cache.SetAsync(cacheKey, cacheItem);

                Logger.LogDebug($"Finished setting the cache item: {cacheKey}");

                return(cacheItem);
            }
        }