RavenDb tutorial에 따르면 응용 프로그램에는 정확히 하나의 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);
을 소비자는 다음과 같을 수 다음 IDocumentStore
를 주입
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();
}
}
}
때문에, 소비자는 자신이 책임이 있습니다 세션 관리 : 생성, 저장 및 삭제 작은 응용 프로그램이나 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로 작업 할 때 일반적으로 비즈니스 트랜잭션이나 트랜잭션의 개념이 중요하지는 않지만 솔직히 모르겠습니다. 이것은 당신이 스스로 알아야 할 것입니다.