【发布时间】:2018-09-06 07:57:34
【问题描述】:
在我的 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> 时,.net core DI 如何解析User 是BlogContext 的实体,所以IDbContext 在我的EfRepository 实现中应该是BlogContext?
【问题讨论】:
-
我好像听到 Walter Sobchak “你正在进入一个痛苦的世界”
-
个人意见:存储库模式不会给你带来太多的 EF,除了更多的工作。
标签: entity-framework repository asp.net-core-2.0 repository-pattern