Skip to content

Instantly share code, notes, and snippets.

@iyhammad
Last active October 10, 2019 19:12
Show Gist options
  • Save iyhammad/99ffcebff9e452b55107687983c9404b to your computer and use it in GitHub Desktop.
Save iyhammad/99ffcebff9e452b55107687983c9404b to your computer and use it in GitHub Desktop.
TestDbContextResolver
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
});
}
}
}
}
@iyhammad
Copy link
Author

Not Tested yet

@iyhammad
Copy link
Author

iyhammad commented Nov 2, 2016

Tested and Working Fine

@venkatsm
Copy link

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