.net 如何配置Simple Injector IoC以使用RavenDB
我正在MVC3Web应用程序中使用IOC。我正在使用的数据存储。在mvc 3应用程序中使用RavenDB有几个考虑因素。我搜索了一些关于如何连接IoC以使用RavenDB的信息,但还没有找到如何连接简单的注入器以使用RavenDB。有人能解释一下如何在MVC3Web应用程序中连接简单的注射器来使用RavenDB吗 谢谢。根据,您的应用程序只需要一个.net 如何配置Simple Injector IoC以使用RavenDB,.net,asp.net-mvc-3,inversion-of-control,ravendb,simple-injector,.net,Asp.net Mvc 3,Inversion Of Control,Ravendb,Simple Injector,我正在MVC3Web应用程序中使用IOC。我正在使用的数据存储。在mvc 3应用程序中使用RavenDB有几个考虑因素。我搜索了一些关于如何连接IoC以使用RavenDB的信息,但还没有找到如何连接简单的注入器以使用RavenDB。有人能解释一下如何在MVC3Web应用程序中连接简单的注射器来使用RavenDB吗 谢谢。根据,您的应用程序只需要一个IDocumentStore实例(我假设每个数据库)。IDocumentStore是线程安全的。它生成IDocumentSession实例,它们在Ra
IDocumentStore
实例(我假设每个数据库)。IDocumentStore
是线程安全的。它生成IDocumentSession
实例,它们在RavenDB中表示a,而这些实例是非线程安全的。因此,您不应该在线程之间共享会话
如何设置用于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 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());
请注意,您需要(或将SimpleInjector.Integration.Web.dll包含到您的项目中,该项目包含在默认下载中)才能使用RegisterWebRequest
扩展方法
现在的问题是,在哪里调用session.SaveChanges()
有一个关于注册每个web请求的工作单元的问题,它也解决了关于SaveChanges
的问题。请仔细看看这个答案:。当您将DbContext
替换为IDocumentSession
并将DbContextFactory
替换为IDocumentStore
时,您将能够在RavenDb的上下文中读取它。请注意,在使用RavenDb时,业务事务或一般事务的概念可能没有那么重要,但我真的不知道。这是你必须自己去发现的