2016-03-16 30 views
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*/); 

回答

1

是TContext已知類型從RepositoryBase繼承?如果你知道UserRepository將使用MyContext,並ProductRepository將使用MyOtherContext,你可以將它們定義爲這樣的和有Autofac自動去接他們:

public class UserRepository : RepositoryBase<MyContext, User> 
{...} 

public class ProductRepository : RepositoryBase<MyOtherContext, Product> 
{...} 

否則我假設你有一個要求,允許從多個上下文數據?

public class MyContextUserRepository : RepositoryBase<MyContext, User> 
{...} 

public class MyOtherContextUserRepository : RepositoryBase<MyOtherContext, User> 
{...} 

在這種情況下,你可以將它們定義爲以上,但決心你必須在你想要的背景下通過在特定的情況下

public class MyClass 
{ 
    readonly IRepository<User> _userRepository; 

    public MyClass(IRepository<User> userRepository) 
    { 
     _userRepository = userRepository; 
    } 
} 

var myClass = _container.Resolve<MyClass>(
       new ResolvedParameter(
        (x, y) => x.ParameterType == typeof (IRepository<User>), 
        (x, y) => y.Resolve<MyContextUserRepository>())); 

或者你可以做到這一點上註冊,在這種情況下,您可能會發現Autofac documentation有用。

評論

好了,你可以得到共享包含接口,實現和實體共​​同庫中的多個應用程序後編輯。給定的應用程序可以定義它們的實現和實體,但只有一個特定於該應用程序的上下文。

// your application specific context 
public class MySpecificApplicationContext : DbContext { } 

// get your entity types that a given application uses 
var entityTypes = GetEntityTypes(); 

// register a repository implementation for each entity type 
// note RepositoryBase is not abstract for this example 
for (var i = 0; i < entityTypes.Count; i++) 
{ 
    var entityType = entityTypes[i]; 
    builder 
     .RegisterType(typeof (RepositoryBase<,>) 
     .MakeGenericType(typeof (MySpecificApplicationContext), entityType)) 
     .As(typeof (IRepository<>).MakeGenericType(entityType)) 
     .InstancePerLifetimeScope(); 
} 

// resolve an entity repository 
var userRepository = container.Resolve<IRepository<User>>(); 
+0

TContext不是已知的存儲庫類型,也不知道解析和使用存儲庫的類型。 (對於不同的上下文,相同的存儲庫用於多個應用程序)。 TContext僅在註冊期間是已知的。我無法在文檔中找到有關我的問題的任何信息。 – musium

+0

您提到'相同的存儲庫用於不同的上下文的多個應用程序'。在給定的應用程序中,你有多個上下文還是一個? –

+0

每個應用程序一個 – musium