如何配置简单喷油器的IoC使用RavenDB [英] How to configure Simple Injector IoC to use RavenDB

查看:176
本文介绍了如何配置简单喷油器的IoC使用RavenDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用简单的喷油器一个国际奥委会在MVC 3 Web应用程序。我使用 RavenDB 用于数据存储。有几个因素在一个MVC 3应用程序中使用RavenDB。我搜索了一些关于如何连接,建立一个IoC容器的使用RavenDB但还没有找到如何线了简单的注射器使用RavenDB。任何人都可以解释如何线了简单的注射器使用RavenDB在MVC 3 web应用程序?

I'm using Simple Injector for an IOC in an MVC 3 web application. I am using RavenDB for the data storage. There are several considerations on using RavenDB in an mvc 3 application. I've searched some on how to wire-up an IoC to use RavenDB but haven't found out how to wire-up simple injector to use RavenDB. Can anyone explain how to wire-up simple injector to use RavenDB in an MVC 3 web app?

感谢。

推荐答案

按照 RavenDb教程,你的应用需要一个 IDocumentStore 实例(每个数据库我假设)。 A IDocumentStore 是线程安全的。它产生 IDocumentSession 情况下,他们重新present一个工作单元在RavenDB,而这些都是不可以线程安全的。你因此应该的不可以线程之间共享会话。

According to the RavenDb tutorial, your application needs exactly oneIDocumentStore instance (per database I assume). A IDocumentStore is thread-safe. It produces IDocumentSession instances and they represent a unit of work in RavenDB, and those are not thread-safe. You should therefore not share sessions between threads.

如何设置你的容器使用与RavenDb主要取决于应用程序的设计。现在的问题是:你怎么想的注入给消费者?该 IDocumentStore IDocumentSession

How to set up your container for use with RavenDb mainly depends on the application design. The question is: what do you want to inject in to consumers? The IDocumentStore, or the IDocumentSession?

当你去与 IDocumentStore ,您的注册可能是这样的:

When you go with the IDocumentStore, your registration could look like this:

// Composition Root
IDocumentStore store = new DocumentStore
{
    ConnectionStringName = "http://localhost:8080"
 };

store.Initialize();

container.RegisterSingle<IDocumentStore>(store);

一个消费者可能是这样的:

A consumer might look like this:

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 注入,消费者自己负责管理会话:创建,保存和处理。这对于小型应用非常方便,或者例如当躲在,在那里你叫<$ c中的RavenDb数据库$ C> session.SaveChanges()的 repository.Save(实体)方法内。

Because the IDocumentStore is injected, consumers are themselves responsible for managing the session: creation, saving, and disposing. This is very convenient for small applications, or for instance when hiding the RavenDb database behind a repository, where you call session.SaveChanges() inside the repository.Save(entity) method.

不过,我发现这种类型的使用工作单元的有问题的大型应用程序。所以,你可以做什么,而是被注入 IDocumentSession 成的消费者。在这种情况下,您的注册可能是这样的:

However, I found this type of use of a unit of work to be problematic for larger applications. So what you can do instead, is injecting the IDocumentSession into consumers. In that case your registration could look like this:

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());

请注意,您所需要的简单的注射器ASP.NET集成的NuGet包(或包含的 SimpleInjector .Integration.Web.dll 的到你的项目,其中包括在默认的下载),以便能够使用 RegisterPerWebRequest 扩展方法。

Note that you need the Simple Injector ASP.NET Integration NuGet package (or include the SimpleInjector.Integration.Web.dll to your project, which is included in the default download) to be able to use the RegisterPerWebRequest extension method.

现在的问题是,从哪里调用 session.SaveChanges()

The question now becomes, where to call session.SaveChanges()?

有一个关于登记部每个Web请求,这也解决了约的SaveChanges 问题工程问题。请好好看看这个答案:每个Web请求一个的DbContext ......为什么?当您更换的话的DbContext IDocumentSession DbContextFactory IDocumentStore ,你将能够读取它RavenDb的范围内。需要注意的是与RavenDb工作时的商业交易或一般的交易也许是观念并不是那么重要,但我真的不知道。这是你必须发现为你自己。

There is a question about registering unit of works per web request, which also addresses the question about SaveChanges. Please take a good look at this answer: One DbContext per web request…why?. When you replace the words DbContext with IDocumentSession and DbContextFactory with IDocumentStore, you will be able read it in the context of RavenDb. Note that perhaps the notion of business transactions or transactions in general are not that important when working with RavenDb, but I honestly don't know. This is something you will have to find out for yourself.

这篇关于如何配置简单喷油器的IoC使用RavenDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆