RavenDb tutorialによれば、アプリケーションにはちょうど1つのIDocumentStore
インスタンスが必要です(データベースごとに仮定します)。 IDocumentStore
はスレッドセーフです。 IDocumentSession
インスタンスを生成し、RavenDBでunit of workを表し、ではなくスレッドセーフです。したがって、ではなく、スレッド間でのセッションを共有する必要があります。
RavenDbで使用するコンテナの設定方法は、主にアプリケーションの設計によって異なります。問題は、あなたは消費者に何を投入したいのですか? IDocumentStore
、またはIDocumentSession
?
あなたがIDocumentStore
で行く場合は、あなたの登録は次のようになります。
// Composition Root
IDocumentStore store = new DocumentStore
{
ConnectionStringName = "http://localhost:8080"
};
store.Initialize();
container.RegisterSingle<IDocumentStore>(store);
消費者は、次のようになります。
public class ProcessLocationCommandHandler
: ICommandHandler<ProcessLocationCommand>
{
private readonly IDocumentStore store;
public ProcessLocationCommandHandler(IDocumentStore store)
{
this.store = store;
}
public void Handle(ProcessLocationCommand command)
{
using (var session = this.store.OpenSession())
{
session.Store(command.Location);
session.SaveChanges();
}
}
}
をIDocumentStore
が注入されているので、消費者は責任そのものですセッションの管理:作成、保存、および廃棄。小規模なアプリケーションや、repositoryの背後にあるRavenDbデータベースを隠す場合など、repository.Save(entity)
メソッドの中でsession.SaveChanges()
と呼ぶ場合など、これは非常に便利です。
しかし、このタイプの作業単位の使用は、より大きなアプリケーションでは問題になることがわかりました。あなたが代わりにできることは、IDocumentSession
を消費者に注入することです。その場合は、あなたの登録は、次のようになります。
IDocumentStore store = new DocumentStore
{
ConnectionStringName = "http://localhost:8080"
};
store.Initialize();
// Register the IDocumentSession per web request
// (will automatically be disposed when the request ends).
container.RegisterPerWebRequest<IDocumentSession>(
() => store.OpenSession());
注意が必要Simple Injector ASP.NET Integration NuGet package(またはデフォルトのダウンロードに含まれているプロジェクトにSimpleInjector.Integration.Web.dllを含む)にすること拡張メソッドRegisterPerWebRequest
を使用できます。
質問は今どこになるのですか、session.SaveChanges()
と電話をかけますか?
ウェブリクエストごとに作品単位を登録する際には、SaveChanges
についての質問にもお答えします。この回答をよく見てください:One DbContext per web request…why?。単語DbContext
をIDocumentSession
およびDbContextFactory
に置き換えた場合、IDocumentStore
と置き換えると、RavenDbのコンテキストで読むことができます。 RavenDbを使って作業する場合、ビジネストランザクションやトランザクションの概念はそれほど重要ではないかもしれませんが、私は正直に分かりません。これはあなた自身で見つけなければならないものです。