我在使用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核心DI如何解析User
是BlogContext
的实体,因此IDbContext
应该是BlogContext
在我的EfRepository
实现中?
答案 0 :(得分:2)
英语不好,希望我能为您
我用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);
}
}