0
在我的項目,我得到了下面的基類爲我所有的庫:Autofac - 掃描和批量註冊通用類型
public abstract class RepositoryBase<TContext, TEntity> : Disposable, IRepository<TEntity>
where TEntity : class
where TContext : DbContext, new()
{...}
儲存庫繼承形式的基類不指定DB上下文類型(因爲上下文並不適用於所有應用程序相同):
public class UserRepository<TContext> : RepositoryBase<TContext, User>
where TContext : BaseContext, new()
{
public UserRepository(IDatabaseFactory<TContext> databaseFactory, ILoggerFactory loggerFactory)
: base(databaseFactory, loggerFactory)
{...}
}
public class ProductRepository<TContext> : RepositoryBase<TContext, Product>
where TContext : BaseContext, new()
{...}
我知道autofac能夠記錄所有類型的匹配某些標準是這樣的:
builder.RegisterAssemblyTypes(assembly)
.Where(...)
.AsImplementedInterfaces();
是否有一種方法可以爲從特定上下文的RepositoryBase繼承的所有類型執行相同的操作? 喜歡的東西:
public class MyContext : BaseContext { }
builder.RegisterAssemblyTypes(assembly)
.Where(...)
.AsImplementedInterfaces(/*specify MyContext as TContext*/);
TContext不是已知的存儲庫類型,也不知道解析和使用存儲庫的類型。 (對於不同的上下文,相同的存儲庫用於多個應用程序)。 TContext僅在註冊期間是已知的。我無法在文檔中找到有關我的問題的任何信息。 – musium
您提到'相同的存儲庫用於不同的上下文的多個應用程序'。在給定的應用程序中,你有多個上下文還是一個? –
每個應用程序一個 – musium