Example #1
0
        protected bool EnsureTenantSwitched(ITenant tenant)
        {
            tenant.NotNull(nameof(tenant));

            if (CurrentTenant.Equals(tenant))
            {
                return(false);
            }

            CurrentTenant = tenant;
            Logger.LogTrace(InternalResource.SwitchNewTenantFormat.Format(tenant.ToString()));

            Dependency.Options.PostTenantSwitchedAction?.Invoke(this);

            return(true);
        }
Example #2
0
        protected virtual async Task <TResult> WritingSeparationOperationAsync <TResult>
            (Func <Task <TResult> > writingFunc, Func <Task <TResult> > dataSynchronizationFunc = null,
            CancellationToken cancellationToken = default)
        {
            writingFunc.NotNull(nameof(writingFunc));

            // 暂存当前租户与连接字符串
            var lastTenant           = CurrentTenant;
            var lastConnectionString = CurrentConnectionString;

            // 保存变化到写入连接字符串
            await ChangeConnectionStringAsync(tenant => tenant.WritingConnectionString,
                                              cancellationToken).ConfigureAwait();

            var value = await writingFunc.Invoke().ConfigureAwait();

            if (CurrentTenant.DataSynchronization)
            {
                dataSynchronizationFunc.NotNull(nameof(dataSynchronizationFunc));

                // 数据同步到默认连接字符串
                await ChangeConnectionStringAsync(tenant => tenant.DefaultConnectionString,
                                                  cancellationToken).ConfigureAwait();

                await dataSynchronizationFunc.Invoke().ConfigureAwait();
            }

            // 还原租户与连接字符串
            if (!CurrentTenant.Equals(lastTenant))
            {
                EnsureTenantSwitched(lastTenant);
            }

            if (!IsCurrentConnectionString(lastConnectionString))
            {
                EnsureDatabaseChanged(lastConnectionString);
            }

            return(value);
        }
Example #3
0
        protected virtual TResult WritingSeparationOperation <TResult>(Func <TResult> writingFunc,
                                                                       Func <TResult> dataSynchronizationFunc = null)
        {
            writingFunc.NotNull(nameof(writingFunc));

            // 暂存当前租户与连接字符串
            var lastTenant           = CurrentTenant;
            var lastConnectionString = CurrentConnectionString;

            // 保存变化到写入连接字符串
            ChangeConnectionString(tenant => tenant.WritingConnectionString);

            var result = writingFunc.Invoke();

            if (CurrentTenant.DataSynchronization)
            {
                dataSynchronizationFunc.NotNull(nameof(dataSynchronizationFunc));

                // 数据同步到默认连接字符串
                ChangeConnectionString(tenant => tenant.DefaultConnectionString);

                dataSynchronizationFunc.Invoke();
            }

            // 还原租户与连接字符串
            if (!CurrentTenant.Equals(lastTenant))
            {
                EnsureTenantSwitched(lastTenant);
            }

            if (!IsCurrentConnectionString(lastConnectionString))
            {
                EnsureDatabaseChanged(lastConnectionString);
            }

            return(result);
        }