Exemple #1
0
        public static Task <string> ResolveAsync(this IConnectionStringResolver resolver, Type type)
        {
            var attribute = type.GetTypeInfo().GetCustomAttribute <ConnectionStringNameAttribute>();

            if (attribute != null)
            {
                return(resolver.ResolveAsync(attribute.Name));
            }

            return(resolver.ResolveAsync(type.FullName));
        }
    private async Task <string> ResolveConnectionStringAsync(string connectionStringName)
    {
        // Multi-tenancy unaware contexts should always use the host connection string
        if (typeof(TDbContext).IsDefined(typeof(IgnoreMultiTenancyAttribute), false))
        {
            using (_currentTenant.Change(null))
            {
                return(await _connectionStringResolver.ResolveAsync(connectionStringName));
            }
        }

        return(await _connectionStringResolver.ResolveAsync(connectionStringName));
    }
        public async Task <TDbContext> GetDbContextAsync()
        {
            var unitOfWork = _unitOfWorkManager.Current;

            if (unitOfWork == null)
            {
                throw new AbpException("A DbContext can only be created inside a unit of work!");
            }

            var connectionStringName = ConnectionStringNameAttribute.GetConnStringName <TDbContext>();
            var connectionString     = await _connectionStringResolver.ResolveAsync(connectionStringName);

            var dbContextKey = $"{typeof(TDbContext).FullName}_{connectionString}";

            var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey);

            if (databaseApi == null)
            {
                databaseApi = new EfCoreDatabaseApi <TDbContext>(
                    await CreateDbContextAsync(unitOfWork, connectionStringName, connectionString)
                    );

                unitOfWork.AddDatabaseApi(dbContextKey, databaseApi);
            }

            return(((EfCoreDatabaseApi <TDbContext>)databaseApi).DbContext);
        }
    public async Task <IMemoryDatabase> GetDatabaseAsync()
    {
        var unitOfWork = _unitOfWorkManager.Current;

        if (unitOfWork == null)
        {
            throw new AbpException($"A {nameof(IMemoryDatabase)} instance can only be created inside a unit of work!");
        }

        var connectionString = await _connectionStringResolver.ResolveAsync <TMemoryDbContext>();

        var dbContextKey = $"{typeof(TMemoryDbContext).FullName}_{connectionString}";

        var databaseApi = unitOfWork.GetOrAddDatabaseApi(
            dbContextKey,
            () => new MemoryDbDatabaseApi(
                _memoryDatabaseManager.Get(connectionString)
                ));

        return(((MemoryDbDatabaseApi)databaseApi).Database);
    }
Exemple #5
0
        public async Task <TMongoDbContext> GetDbContextAsync(CancellationToken cancellationToken = default)
        {
            var unitOfWork = _unitOfWorkManager.Current;

            if (unitOfWork == null)
            {
                throw new AbpException(
                          $"A {nameof(IMongoDatabase)} instance can only be created inside a unit of work!");
            }

            var connectionString = await _connectionStringResolver.ResolveAsync <TMongoDbContext>();

            var dbContextKey = $"{typeof(TMongoDbContext).FullName}_{connectionString}";

            var mongoUrl     = new MongoUrl(connectionString);
            var databaseName = mongoUrl.DatabaseName;

            if (databaseName.IsNullOrWhiteSpace())
            {
                databaseName = ConnectionStringNameAttribute.GetConnStringName <TMongoDbContext>();
            }

            //TODO: Create only single MongoDbClient per connection string in an application (extract MongoClientCache for example).
            var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey);

            if (databaseApi == null)
            {
                databaseApi = new MongoDbDatabaseApi <TMongoDbContext>(
                    await CreateDbContextAsync(
                        unitOfWork,
                        mongoUrl,
                        databaseName,
                        cancellationToken
                        )
                    );

                unitOfWork.AddDatabaseApi(dbContextKey, databaseApi);
            }

            return(((MongoDbDatabaseApi <TMongoDbContext>)databaseApi).DbContext);
        }
        public async Task All_Tests()
        {
            //No tenant in current context
            (await _connectionResolver.ResolveAsync()).ShouldBe("default-value");
            (await _connectionResolver.ResolveAsync("db1")).ShouldBe("db1-default-value");

            //Overriden connection strings for tenant1
            using (_currentTenant.Change(_tenant1Id))
            {
                (await _connectionResolver.ResolveAsync()).ShouldBe("tenant1-default-value");
                (await _connectionResolver.ResolveAsync("db1")).ShouldBe("tenant1-db1-value");
            }

            //No tenant in current context
            (await _connectionResolver.ResolveAsync()).ShouldBe("default-value");
            (await _connectionResolver.ResolveAsync("db1")).ShouldBe("db1-default-value");

            //Undefined connection strings for tenant2
            using (_currentTenant.Change(_tenant2Id))
            {
                (await _connectionResolver.ResolveAsync()).ShouldBe("default-value");
                (await _connectionResolver.ResolveAsync("db1")).ShouldBe("db1-default-value");
            }
        }
Exemple #7
0
        public static Task <string> ResolveAsync <T>(this IConnectionStringResolver resolver)
        {
            var type = typeof(T);

            return(resolver.ResolveAsync(type));
        }
 public static Task <string> ResolveAsync <T>(this IConnectionStringResolver resolver)
 {
     return(resolver.ResolveAsync(ConnectionStringNameAttribute.GetConnStringName <T>()));
 }
Exemple #9
0
 public static Task <string> ResolveAsync(this IConnectionStringResolver resolver, Type type)
 {
     return(resolver.ResolveAsync(ConnectionStringNameAttribute.GetConnStringName(type)));
 }