ABP如何为实体生成IRepository <TEntity>

Pau*_*Lee 0 c# dependency-injection aspnetboilerplate

我试图了解ABP如何做到这一点.

ABP中的正常工作流程是:

  1. 将从基于实体的类继承的实体类添加到Core项目
  2. 将DbSet添加到EFCore项目中的上下文中
  3. 向Application项目添加新的IEntityAppService接口和实现
  4. 在IEntityAppService实现中,将IRepository注入构造函数
  5. 请享用!!

我做了什么试图理解:

  1. 我查看了Abp源代码,并得到了这样的印象,这是由Open Generics IRepository<T>EfCoreRepositoryBase<T>Factory magic 之间完成的.但是,我尝试在AspNetCore DI中执行此操作:

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        // adding DbContext
        // adding Mvc etc....
    
        // RepositoryBase => like EfCoreRepositoryBase in ABP
        // Error on this line, DI can not instantiate RepositoryBase as it is abstract
        services.AddTransient<IRepository<>, RepositoryBase<>);
    }
    
    Run Code Online (Sandbox Code Playgroud)

请有人向我解释这个机制吗?

aar*_*ron 5

神奇之处在于EfGenericRepositoryRegistrar.cs:

foreach (var entityTypeInfo in _dbContextEntityFinder.GetEntityTypeInfos(dbContextType))
{
    // ...

    iocManager.IocContainer.Register(
        Component
            .For(genericRepositoryType)
            .ImplementedBy(implType)
            .Named(Guid.NewGuid().ToString("N"))
            .LifestyleTransient()
}
Run Code Online (Sandbox Code Playgroud)

请注意,这EfCoreRepositoryBaseOfTEntityAndTPrimaryKey.cs不是抽象的.