谢谢.
解决方法
如何设置容器以便与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,因此消费者自己负责管理会话:创建,保存和处置.这对于小型应用程序非常方便,或者例如将Ravendb数据库隐藏在repository后面,您可以在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()?
关于为每个Web请求注册作品单元存在一个问题,该问题还解决了有关SaveChanges的问题.请仔细看看这个答案:One DbContext per web request…why?.当您使用IDocumentStore替换带有IDocumentSession和DbContextFactory的单词DbContext时,您将能够在Ravendb的上下文中读取它.请注意,在使用Ravendb时,商业交易或交易的概念可能并不重要,但老实说我不知道.这是你必须自己找到的东西.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。