[英]Unable to inject IRepository<TEntity> due to “no implicit reference conversion”
[英]How ABP generate IRepository<TEntity> for Entity
我試圖了解ABP如何做到以上幾點。
ABP中的正常工作流程是:
我嘗試理解的內容:
我看了看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()
}
可能是您要創建自定義存儲庫。 在以下位置查看文檔:
您可以這樣替換應用程序的默認存儲庫:
[AutoRepositoryTypes(
typeof(IRepository<>),
typeof(IRepository<,>),
typeof(SimpleTaskSystemEfRepositoryBase<>),
typeof(SimpleTaskSystemEfRepositoryBase<,>)
)]
public class SimpleTaskSystemDbContext : AbpDbContext
{
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.