![](/img/trans.png)
[英]Entity Framework 5 - multiple DbContext and a Generic Repository class
[英]Getting generic repository with generic dbContext (multiple dbContext)
我在使用實體框架構建通用存儲庫時迷路了,該框架在我的 asp.net core 2 api 項目中有多個上下文。
我需要的是將我的存儲庫注入控制器而不知道它正在使用哪個上下文。
public interface IDbContext
{
}
public interface IBlogContext : IDbContext
{
}
public interface IBlogLogContext : IDbContext
{
}
public class EfRepository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity
{
private readonly IDbContext _context;
private readonly DbSet<TEntity> _dbSet;
public EfRepository(IDbContext context)
{
_context = context;
_dbSet = _context.Set<TEntity>();
}
/* other stuff */
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BlogContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
.UseLoggerFactory(_logger);
});
services.AddDbContext<BlogLogContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
services.AddScoped<IBlogContext, BlogContext>();
services.AddScoped<IBlogLogContext, BlogLogContext>();
services.AddUnitOfWork<BlogContext, BlogLogContext>();
}
public class UserController : Controller
{
private readonly IRepository<User> _repository;
private readonly IAuthorService _authorService;
private readonly ILogger<UserController> _logger;
public UserController(IRepository<User> repository, IAuthorService authorService, ILogger<UserController> logger)
{
_repository = repository;
_authorService = authorService;
_logger = logger;
}
}
當我注入IRepository<User>
中的控制器構造,核心DI怎么解決.NET是User
是BlogContext
的實體,所以IDbContext
應該BlogContext
我里面EfRepository
實施?
我使用 DbContext 通用參數定義了我的 EfRepository,如下所示:
public class EfRepository<TDbContext, TEntity> : IRepository<TEntity>
where TDbContext : DbContext
where TEntity : class, IEntity
{
public EfRepository(IDbContextProvider<TDbContext> dbContextProvider)
{
}
}
並得到這樣的 DbContext-Entity 信息:
//EntityTypeInfo is a class has EntityType & DeclaringType props
private IList<EntityTypeInfo> GetEntityTypeInfos()
{
var result = new List<EntityTypeInfo>();
var dbContextTypes = this.GetType().Assembly.GetTypes().Where(t => (typeof(DbContext).IsAssignableFrom(t)));
foreach (var dbContextType in dbContextTypes)
{
result.AddRange(
from property in dbContextType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
where
Common.IsAssignableToGenericType(property.PropertyType, typeof(DbSet<>)) &&
Common.IsAssignableToGenericType(property.PropertyType.GenericTypeArguments[0], typeof(IEntity))
select new EntityTypeInfo(
property.PropertyType.GenericTypeArguments[0],
property.DeclaringType
);
}
return result;
}
所以我可以使用 EntityTypeInfo 列表添加相關服務:
var entityTypeInfos = GetEntityTypeInfos();
foreach (var entityTypeInfo in entityTypeInfos)
{
//
//Add IDbContextProvider and DbContext services Here
//
//Add IRepository services like this
var repositoryInterface =typeof(IRepository<>);
var repositoryImplementation =typeof(EfRepository<,>);
var genericRepositoryType = repositoryInterface.MakeGenericType(entityTypeInfo.EntityType);
if (!Common.IocHasRegister(genericRepositoryType))
{
var implRepositoryType = repositoryImplementation.MakeGenericType(entityTypeInfo.DeclaringType, entityTypeInfo.EntityType);
services.AddScoped(genericRepositoryType, implRepositoryType);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.