あなたが実際にジェネリックとしてEntityFrameworkSagaRepository<T>
登録することができますが、それは登録する必要がします DbContext
。 SagaDbContext
を使用する場合は、アプリケーションに複数のサガを使用することはできません。そこに行く唯一の方法はカスタムリポジトリ実装とSagaDbContext
を汎用タイプのパラメータでバインドすることです。これは@slinzerthegodが彼の例で行っていることです。
また、すべてのタイプを登録する独自のコンテキストを作成することもできます。たとえば、次のように
public class AssemblyScanningSagaDbContext : DbContext
{
readonly Assembly _mappingAssembly;
public AssemblyScanningSagaDbContext(Assembly mappingAssembly, string nameOrConnectionString)
: base(nameOrConnectionString)
{
_mappingAssembly = mappingAssembly;
}
public AssemblyScanningSagaDbContext(Assembly mappingAssembly, ObjectContext objectContext, bool dbContextOwnsObjectContext)
: base(objectContext, dbContextOwnsObjectContext)
{
_mappingAssembly = mappingAssembly;
}
public AssemblyScanningSagaDbContext(Assembly mappingAssembly, DbConnection existingConnection, bool contextOwnsConnection)
: base(existingConnection, contextOwnsConnection)
{
_mappingAssembly = mappingAssembly;
}
public AssemblyScanningSagaDbContext(Assembly mappingAssembly, string nameOrConnectionString, DbCompiledModel model)
: base(nameOrConnectionString, model)
{
_mappingAssembly = mappingAssembly;
}
public AssemblyScanningSagaDbContext(Assembly mappingAssembly, DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection)
: base(existingConnection, model, contextOwnsConnection)
{
_mappingAssembly = mappingAssembly;
}
protected AssemblyScanningSagaDbContext(Assembly mappingAssembly)
{
_mappingAssembly = mappingAssembly;
}
protected AssemblyScanningSagaDbContext(Assembly mappingAssembly, DbCompiledModel model)
: base(model)
{
_mappingAssembly = mappingAssembly;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder) =>
modelBuilder.Configurations.AddFromAssembly(_mappingAssembly);
}
あなたは、このようなクラスを持っている場合は、次の操作を行うことができます
builder.Register(c => new AssemblyScanningSagaDbContext(typeof(MySagaMapping).Assembly,
connectionString).As<DbContext>();
builder.RegisterGeneric(typeof(EntityFrameworkSagaRepository<>))
.As(typeof(ISagaRepository<>))
.SingleInstance();
builder.RegisterStateMachineSagas(typeof(MySaga).Assembly);
Autofacは、自動的に工場出荷時のデリゲートを構成することが可能であり、リポジトリコンストラクタにそれを注入します。
Here is the scanning context as gist。
GitHubで問題を開くと修正されます。 –
追加:https://github.com/MassTransit/MassTransit/issues/790 –
これは実際のドキュメントの問題ではありません。 1つのインスタンス/マッピング/リポジトリ登録は簡単ですが、NH 1と同じレベルの一般化ではありません。 –