Last active
October 10, 2019 19:12
-
-
Save iyhammad/99ffcebff9e452b55107687983c9404b to your computer and use it in GitHub Desktop.
TestDbContextResolver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data.Common; | |
using Abp.Dependency; | |
using Abp.Domain.Uow; | |
using Abp.EntityFramework; | |
using Abp.MultiTenancy; | |
using Abp.Runtime.Session; | |
using Effort; | |
using Abp.Collections.Extensions; | |
namespace MyApp.Shared.Test | |
{ | |
public class TestDbContextResolver : IDbContextResolver | |
{ | |
private readonly IIocResolver iocResolver; | |
private readonly IDbContextTypeMatcher dbContextTypeMatcher; | |
private readonly ICurrentUnitOfWorkProvider currentUnitOfWorkProvider; | |
private readonly IAbpSession abpSession; | |
private DbConnection hostDb; | |
private Dictionary<string, DbConnection> tenantDbs; | |
public TestDbContextResolver(IIocResolver iocResolver, IDbContextTypeMatcher dbContextTypeMatcher, ICurrentUnitOfWorkProvider currentUnitOfWorkProvider, IAbpSession abpSession) | |
{ | |
this.iocResolver = iocResolver; | |
this.dbContextTypeMatcher = dbContextTypeMatcher; | |
this.currentUnitOfWorkProvider = currentUnitOfWorkProvider; | |
this.abpSession = abpSession; | |
tenantDbs = new Dictionary<string, DbConnection>(); | |
} | |
private DbConnection GetOrCreateDbConnection(Type dbContextType, int? tenantId) | |
{ | |
string dbConnectionKey; | |
if (tenantId == null || IsHostDbContext(dbContextType)) | |
dbConnectionKey = dbContextType.FullName + 0; | |
else | |
dbConnectionKey = dbContextType.FullName + tenantId; | |
if (!tenantDbs.ContainsKey(dbConnectionKey)) | |
{ | |
tenantDbs[dbConnectionKey] = DbConnectionFactory.CreateTransient(); | |
} | |
return tenantDbs[dbConnectionKey]; | |
} | |
private bool IsHostDbContext(Type dbContextType) | |
{ | |
var attrs = dbContextType.GetCustomAttributes(typeof(MultiTenancySideAttribute), true); | |
if (attrs.IsNullOrEmpty()) | |
{ | |
return false; | |
} | |
if (((MultiTenancySideAttribute) attrs[0]).Side.HasFlag(MultiTenancySides.Host)) | |
return true; | |
else | |
return false; | |
} | |
public TDbContext Resolve<TDbContext>(string connectionString) | |
{ | |
var dbContextType = typeof(TDbContext); | |
var currentUow = currentUnitOfWorkProvider.Current; | |
var tenantId = currentUow != null ? currentUow.GetTenantId() : abpSession.TenantId; | |
if (!dbContextType.IsAbstract) | |
{ | |
var dbConnection = GetOrCreateDbConnection(dbContextType, tenantId); | |
return iocResolver.Resolve<TDbContext>(new | |
{ | |
Connection = dbConnection | |
}); | |
} | |
else | |
{ | |
var concreteType = dbContextTypeMatcher.GetConcreteType(dbContextType); | |
var dbConnection = GetOrCreateDbConnection(concreteType, tenantId); | |
return (TDbContext)iocResolver.Resolve(concreteType, new | |
{ | |
Connection = dbConnection | |
}); | |
} | |
} | |
} | |
} |
Tested and Working Fine
Can you help me share the test project once? I'm having a similar situation as yours, but couldn't implement the integration testing with in-memory database
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not Tested yet