DbContextをコアドメインハンドラに1回アクティブ化するスコープを含めるには、アプリケーション内にNinjectカスタムスコープを使用したいと思います。しかし、CommandScopeオブジェクトがNinjectの一部であるINotifyWhenDisposedインターフェイスを実装しているため、私のドメイン内のNinjectに依存したくないので、問題があります。決定的な処理のためにNinjectスコープオブジェクトを挿入する
Ninjectファクトリを使用してIScopeFactoryを公開することやFuncの依存関係など、コードに依存することを成功させるために、さまざまな方法を試しました。後者の場合、問題は、バインディングターゲットがIDisposable自体ではなくFuncであるため、NinjectはINotifyWhenDisposed.Disposeイベントを終了させないということです。
とにかく、私が達成しようとしているコードです。
のIoC
自分のドメイン内Kernel
.Bind<MyDbContext>()
.ToSelf()
.InScope(x => CommandScope.Current)
.OnDeactivation(x => x.SaveChanges());
CommandScope
public class CommandScope : INotifyWhenDisposed
{
public event Dispose;
public bool IsDisposed { get; private set; }
public static CommandScope Current { get; private set; }
public static CommandScope Create()
{
CommandScope result = new CommandScope();
Current = result;
return result;
}
public void Dispose()
{
IsDisposed = true;
Current = null;
Dispose?.Invoke(this, EventArgs.Empty);
}
}
...
public class Pipeline<TRequest, TResponse>
{
readonly IRequestHandler<TRequest, TResponse> innerHandler;
public Pipeline(IRequestHandler<TRequest, TResponse> handler)
{
innerHandler = handler;
}
public TResponse Handle(TRequest request)
{
using(CommandScope.Create())
{
handler.Handle(request);
}
}
}
しかし、Pipelineクラスはインフラストラクチャコードなので、ドメインロジックを含むプロジェクトとは異なるプロジェクトに展開することができます。それとも別の問題がありますか? –