簡體   English   中英

ABP如何生成IRepository <TEntity> 對於實體

[英]How ABP generate IRepository<TEntity> for Entity

我試圖了解ABP如何做到以上幾點。

ABP中的正常工作流程是:

  1. 將繼承自基於實體的類的實體類添加到Core項目
  2. 將DbSet添加到EFCore項目的上下文中
  3. 向應用程序項目添加新的IEntityAppService接口和實現
  4. 在IEntityAppService實現中,將IRepository注入構造函數
  5. 請享用!!

我嘗試理解的內容:

  1. 我看了看Abp源代碼后,得到的印象是,這是由Open Generics在IRepository<T>EfCoreRepositoryBase<T>通過Factory魔術完成的。 但是,我試圖在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<>); } 

請有人可以向我解釋該機制嗎?

魔術在EfGenericRepositoryRegistrar.cs ,其中:

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

    iocManager.IocContainer.Register(
        Component
            .For(genericRepositoryType)
            .ImplementedBy(implType)
            .Named(Guid.NewGuid().ToString("N"))
            .LifestyleTransient()
}

請注意, EfCoreRepositoryBaseOfTEntityAndTPrimaryKey.cs不是抽象的。

可能是您要創建自定義存儲庫。 在以下位置查看文檔:

創建自定義存儲庫

您可以這樣替換應用程序的默認存儲庫:

[AutoRepositoryTypes(
    typeof(IRepository<>),
    typeof(IRepository<,>),
    typeof(SimpleTaskSystemEfRepositoryBase<>),
    typeof(SimpleTaskSystemEfRepositoryBase<,>)
)]
public class SimpleTaskSystemDbContext : AbpDbContext
{
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM