.net – 如何配置Simple Injector IoC以使用RavenDB
我在MVC 3 Web应用程序中使用
Simple Injector作为IOC.我使用
RavenDB进行数据存储.在mvc 3应用程序中使用RavenDB有几个注意事项.我搜索了一些关于如何连接IoC以使用RavenDB,但还没有找到如何连接简单的注入器来使用RavenDB.任何人都可以解释如何连接简单的注入器在MVC 3 Web应用程序中使用RavenDB?
谢谢. 解决方法
根据
RavenDb tutorial,您的应用程序只需要oneIDocumentStore实例(我假设每个数据库). 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,因此消费者自己负责管理会话:创建,保存和处置.这对于小型应用程序非常方便,或者例如将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时,商业交易或交易的概念可能并不重要,但老实说我不知道??.这是你必须自己找到的东西. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 在asp.net mvc中的视图中设置页面标题,元信
- asp.net – WebResource.axd
- asp.net – 如何在运行时动态地在另一个ASPX的DIV中显示ASP
- ASP.NET框架中的异步页面 – 其他线程在哪里,如何重新连接?
- asp.net-mvc-3 – WebAPI PUT / POST中的部分实体更新
- asp.net – 当主内容没有填满页面时,如何在主页面上获取“页
- asp.net – Linq更新查询生成哪里0 = 1?
- 剃刀 – 从另一个TagHelper调用TagHelpers?
- ASP.NET Handler(ashx)vs MVC Controller Action用于下载文
- asp.net-mvc – 发布webgrid asp.net mvc3的项目